C++ Algorithms
Mohammed Sikander
Team Lead
CranesVarsity
Mohammed.sikander@cranessoftware.com
Mohammed Sikander www.cranessoftware.com
 Container adapters, are interfaces created by
limiting functionality in a pre-existing container and
providing a different set of functionality.When you
declare the container adapters, you have an option
of specifying which sequence containers form the
underlying container.
 These are:
 Stack
 Queue
 priority_queue
Mohammed Sikander www.cranessoftware.com
 The stack class is a container adapter that gives the
programmer the functionality of a stack (LIFO) data
structure.
 The class template acts as a wrapper to the
underlying container - only a specific set of
functions is provided.
 The stack pushes and pops the element from the
back of the underlying container, known as the top
of the stack.
Mohammed Sikander www.cranessoftware.com
Function Name Prototype Description
push void push (const value_type& val); Insert element
pop void pop(); Remove top element
top value_type& top(); Returns a reference to the top
element in the stack.
empty bool empty() const; Test whether container is
empty
Mohammed Sikander www.cranessoftware.com
#include <stack>
int main( ) {
stack<int> s;
int ele , ch;
for(;;) {
cout <<"1. Insert 2. Delete 3. Terminate n";
cin >> ch;
switch(ch)
{case 1 : cout <<"Enter the element to insert : ";
cin >> ele;
s.push(ele);
break;
case 2 : if(s.empty() == false) {
cout <<"The deleted element is " << s.top()<<endl;
s.pop();
}
break;
default : return 0;
}
}
}
Mohammed Sikander www.cranessoftware.com
#include <stack>
int main( )
{
string str;
cout << “Enter the string : “;
cin >> str;
stack <char> stk;
for(int i = 0 ; i < str.length() ; i++)
stk.push(str[i]);
cout <<“The reverse string is n”;
while(stk.empty() == false )
{
cout <<stk.top() ;
stk.pop();
}
}
Mohammed Sikander www.cranessoftware.com
Given a sequence consisting of parentheses, determine whether the
expression is balanced. A sequence of parentheses is balanced if every open
parentheses can be paired uniquely with a closed parentheses that occurs
after the former. Also, the interval between them must be balanced.You will
be given three types of parentheses: (, {, and [.
{[()]}
{[(])}
{{[[(())]]}}
Mohammed Sikander www.cranessoftware.com
bool checkparenthesis(string s) {
stack <char> stk;
for(int i = 0 ; i < s.length() ; i++) {
if(s[i] == '[' || s[i] == '(' || s[i] == '{')
stk.push(s[i]);
else {
if(stk.empty() == true)
return false;
if(s[i] == ']' && stk.top() == '[‘) {
stk.pop(); continue;
}
else if(s[i] == '}' && stk.top() == '{‘) {
stk.pop(); continue;
}
if(s[i] == ')' && stk.top() == '(‘) {
stk.pop(); continue;
}
return false;
}
}
return stk.empty() ;
}
Mohammed Sikander www.cranessoftware.com
 The queue class is a container adapter that
gives the programmer the functionality of a
queue (FIFO) data structure.
 The queue pushes element from back (rear)
and pops the element from front of the
underlying container.
Mohammed Sikander www.cranessoftware.com
Function Name Prototype Description
push void push (const value_type& val); Insert element
pop void pop(); Remove next element
front value_type& front(); Returns a reference to the first
element in the queue.
back value_type& back(); Returns a reference to the last
element in the queue.
empty bool empty() const; Test whether container is
empty
Mohammed Sikander www.cranessoftware.com
#include <queue>
int main( ) {
queue<int> q;
int ele , ch;
for(;;) {
cout <<"1. Insert 2. Delete 3. Terminate n";
cin >> ch;
switch(ch)
{
case 1 : cout <<"Enter the element to insert : ";
cin >> ele;
q.push(ele);
break;
case 2 : if(q.empty() == false)
{
cout <<"The deleted element is " << q.front()<<endl;
q.pop();
}
break;
default : return 0;
}
}
}
Mohammed Sikander www.cranessoftware.com
 A priority_queue is a container providing
sorted-order access to elements.
 You can insert elements in any order, and
then retrieve the "lowest" of these values at
any time.
 Priority queues in C++ STL use a heap
structure internally.
Mohammed Sikander www.cranessoftware.com
Function Name Prototype Description
push void push (const value_type& val); Insert element
pop void pop(); Remove next element
top value_type& top(); Returns a reference to the top
element in the priority queue.
empty bool empty() const; Test whether container is
empty
Mohammed Sikander www.cranessoftware.com
#include <queue>
int main( ) {
priority_queue<int> pq;
int ele , ch;
for(;;) {
cout <<"1. Insert 2. Delete 3.Terminate n";
cin >> ch;
switch(ch)
{
case 1 : cout <<"Enter the element to insert : ";
cin >> ele;
pq.push(ele);
break;
case 2 : if(pq.empty() == false) {
cout <<"The deleted element is " << pq.top()<<endl;
pq.pop();
} break;
default : return 0;
}
}
}
Mohammed Sikander www.cranessoftware.com
#include <queue>
int main( )
{
priority_queue <int> pq;
int ele;
for(int i = 0 ; i < 5 ; i++)
{
cin >> ele;
pq.push(ele);
}
while(pq.empty() == false)
{
cout << pq.top() <<" ";
pq.pop();
}
}
#include <queue>
int main( )
{
priority_queue <int , vector<int> , greater<int>> pq;
int ele;
for(int i = 0 ; i < 5 ; i++)
{
cin >> ele;
pq.push(ele);
}
while(pq.empty() == false)
{
cout << pq.top() <<" ";
pq.pop( );
}
}
Mohammed Sikander www.cranessoftware.com
 Jesse loves cookies. He wants the sweetness of all his cookies to be greater than
value K.To do this, Jesse repeatedly mixes two cookies with the least
sweetness. He creates a special combined cookie with:
 sweetness =(1×=(1× Least sweet cookie + 2× 2nd least sweet cookie).
 He repeats this procedure until all the cookies in his collection have a
sweetness ≥K.
You are given Jesse's cookies. Print the number of operations required to give
the cookies a sweetness ≥K. Print −1 if this isn't possible.
 Input Format
 The first line consists of integers N, the number of cookies and K, the minimum
required sweetness, separated by a space.
The next line contains N integers describing the array A where Ai is the
sweetness of the ith cookie in Jesse's collection.
 Output Format
 Output the number of operations that are needed to increase the cookie's
sweetness ≥K≥K.
Output −1−1 if this isn't possible.
Mohammed Sikander www.cranessoftware.com
 Sample Input
6 7
1 2 3 9 10 12
 SampleOutput
2
 Explanation
 Combine the first two cookies to create a cookie
with sweetness =1×1+2×2 = 5
After this operation, the cookies are 3,5,9,10,12.
Then, combine the cookies with sweetness 3 and sweetness 5, to create a
cookie with resulting sweetness =1×3+2×5= 13
Now, the cookies are 9,10,12,13.
All the cookies have a sweetness ≥7.
Thus, 2 operations are required to increase the sweetness.
Mohammed Sikander www.cranessoftware.com
int main() {
int n , k , ele ;
priority_queue<int , std::vector<int>,
std::greater<int>> pq;
cin >> n >> k;
for(int i = 0 ; i < n ; i++)
{
cin >> ele;
pq.push(ele);
}
int count = 0;
while(pq.top()< k)
{
count++;
int x = pq.top();
pq.pop();
if(pq.empty()== true){
cout << -1 << endl;
return 0;
}
int y = pq.top();
pq.pop();
int res = x * 1 + y * 2;
pq.push(res);
} // End ofWhile
cout << count << endl;
return 0;
}

Container adapters

  • 1.
    C++ Algorithms Mohammed Sikander TeamLead CranesVarsity Mohammed.sikander@cranessoftware.com
  • 2.
    Mohammed Sikander www.cranessoftware.com Container adapters, are interfaces created by limiting functionality in a pre-existing container and providing a different set of functionality.When you declare the container adapters, you have an option of specifying which sequence containers form the underlying container.  These are:  Stack  Queue  priority_queue
  • 3.
    Mohammed Sikander www.cranessoftware.com The stack class is a container adapter that gives the programmer the functionality of a stack (LIFO) data structure.  The class template acts as a wrapper to the underlying container - only a specific set of functions is provided.  The stack pushes and pops the element from the back of the underlying container, known as the top of the stack.
  • 4.
    Mohammed Sikander www.cranessoftware.com FunctionName Prototype Description push void push (const value_type& val); Insert element pop void pop(); Remove top element top value_type& top(); Returns a reference to the top element in the stack. empty bool empty() const; Test whether container is empty
  • 5.
    Mohammed Sikander www.cranessoftware.com #include<stack> int main( ) { stack<int> s; int ele , ch; for(;;) { cout <<"1. Insert 2. Delete 3. Terminate n"; cin >> ch; switch(ch) {case 1 : cout <<"Enter the element to insert : "; cin >> ele; s.push(ele); break; case 2 : if(s.empty() == false) { cout <<"The deleted element is " << s.top()<<endl; s.pop(); } break; default : return 0; } } }
  • 6.
    Mohammed Sikander www.cranessoftware.com #include<stack> int main( ) { string str; cout << “Enter the string : “; cin >> str; stack <char> stk; for(int i = 0 ; i < str.length() ; i++) stk.push(str[i]); cout <<“The reverse string is n”; while(stk.empty() == false ) { cout <<stk.top() ; stk.pop(); } }
  • 7.
    Mohammed Sikander www.cranessoftware.com Givena sequence consisting of parentheses, determine whether the expression is balanced. A sequence of parentheses is balanced if every open parentheses can be paired uniquely with a closed parentheses that occurs after the former. Also, the interval between them must be balanced.You will be given three types of parentheses: (, {, and [. {[()]} {[(])} {{[[(())]]}}
  • 8.
    Mohammed Sikander www.cranessoftware.com boolcheckparenthesis(string s) { stack <char> stk; for(int i = 0 ; i < s.length() ; i++) { if(s[i] == '[' || s[i] == '(' || s[i] == '{') stk.push(s[i]); else { if(stk.empty() == true) return false; if(s[i] == ']' && stk.top() == '[‘) { stk.pop(); continue; } else if(s[i] == '}' && stk.top() == '{‘) { stk.pop(); continue; } if(s[i] == ')' && stk.top() == '(‘) { stk.pop(); continue; } return false; } } return stk.empty() ; }
  • 9.
    Mohammed Sikander www.cranessoftware.com The queue class is a container adapter that gives the programmer the functionality of a queue (FIFO) data structure.  The queue pushes element from back (rear) and pops the element from front of the underlying container.
  • 10.
    Mohammed Sikander www.cranessoftware.com FunctionName Prototype Description push void push (const value_type& val); Insert element pop void pop(); Remove next element front value_type& front(); Returns a reference to the first element in the queue. back value_type& back(); Returns a reference to the last element in the queue. empty bool empty() const; Test whether container is empty
  • 11.
    Mohammed Sikander www.cranessoftware.com #include<queue> int main( ) { queue<int> q; int ele , ch; for(;;) { cout <<"1. Insert 2. Delete 3. Terminate n"; cin >> ch; switch(ch) { case 1 : cout <<"Enter the element to insert : "; cin >> ele; q.push(ele); break; case 2 : if(q.empty() == false) { cout <<"The deleted element is " << q.front()<<endl; q.pop(); } break; default : return 0; } } }
  • 12.
    Mohammed Sikander www.cranessoftware.com A priority_queue is a container providing sorted-order access to elements.  You can insert elements in any order, and then retrieve the "lowest" of these values at any time.  Priority queues in C++ STL use a heap structure internally.
  • 13.
    Mohammed Sikander www.cranessoftware.com FunctionName Prototype Description push void push (const value_type& val); Insert element pop void pop(); Remove next element top value_type& top(); Returns a reference to the top element in the priority queue. empty bool empty() const; Test whether container is empty
  • 14.
    Mohammed Sikander www.cranessoftware.com #include<queue> int main( ) { priority_queue<int> pq; int ele , ch; for(;;) { cout <<"1. Insert 2. Delete 3.Terminate n"; cin >> ch; switch(ch) { case 1 : cout <<"Enter the element to insert : "; cin >> ele; pq.push(ele); break; case 2 : if(pq.empty() == false) { cout <<"The deleted element is " << pq.top()<<endl; pq.pop(); } break; default : return 0; } } }
  • 15.
    Mohammed Sikander www.cranessoftware.com #include<queue> int main( ) { priority_queue <int> pq; int ele; for(int i = 0 ; i < 5 ; i++) { cin >> ele; pq.push(ele); } while(pq.empty() == false) { cout << pq.top() <<" "; pq.pop(); } } #include <queue> int main( ) { priority_queue <int , vector<int> , greater<int>> pq; int ele; for(int i = 0 ; i < 5 ; i++) { cin >> ele; pq.push(ele); } while(pq.empty() == false) { cout << pq.top() <<" "; pq.pop( ); } }
  • 16.
    Mohammed Sikander www.cranessoftware.com Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K.To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with:  sweetness =(1×=(1× Least sweet cookie + 2× 2nd least sweet cookie).  He repeats this procedure until all the cookies in his collection have a sweetness ≥K. You are given Jesse's cookies. Print the number of operations required to give the cookies a sweetness ≥K. Print −1 if this isn't possible.  Input Format  The first line consists of integers N, the number of cookies and K, the minimum required sweetness, separated by a space. The next line contains N integers describing the array A where Ai is the sweetness of the ith cookie in Jesse's collection.  Output Format  Output the number of operations that are needed to increase the cookie's sweetness ≥K≥K. Output −1−1 if this isn't possible.
  • 17.
    Mohammed Sikander www.cranessoftware.com Sample Input 6 7 1 2 3 9 10 12  SampleOutput 2  Explanation  Combine the first two cookies to create a cookie with sweetness =1×1+2×2 = 5 After this operation, the cookies are 3,5,9,10,12. Then, combine the cookies with sweetness 3 and sweetness 5, to create a cookie with resulting sweetness =1×3+2×5= 13 Now, the cookies are 9,10,12,13. All the cookies have a sweetness ≥7. Thus, 2 operations are required to increase the sweetness.
  • 18.
    Mohammed Sikander www.cranessoftware.com intmain() { int n , k , ele ; priority_queue<int , std::vector<int>, std::greater<int>> pq; cin >> n >> k; for(int i = 0 ; i < n ; i++) { cin >> ele; pq.push(ele); } int count = 0; while(pq.top()< k) { count++; int x = pq.top(); pq.pop(); if(pq.empty()== true){ cout << -1 << endl; return 0; } int y = pq.top(); pq.pop(); int res = x * 1 + y * 2; pq.push(res); } // End ofWhile cout << count << endl; return 0; }