codeparrot/xlcost-text-to-code huggingface dataset:
The Stock Span Problem | C ++ program for brute force method to calculate stock span values ; Fills array S [ ] with span values ; Span value of first day is always 1 ; Calculate span value of remaining days by linearly checking previous days ; Initialize span value ; Traverse left while the next element on left is smaller than price [ i ] ; A utility function to print elements of array ; Driver code ; Fill the span values in array S [ ] ; print the calculated span values
#include <bits/stdc++.h> NEW_LINE using namespace std ; void calculateSpan ( int price [ ] , int n , int S [ ] ) { S [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { S [ i ] = 1 ; for ( int j = i - 1 ; ( j >= 0 ) && ( price [ i ] >= price [ j ] ) ; j -- ) S [ i ] ++ ; } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int price [ ] = { 10 , 4 , 5 , 90 , 120 , 80 } ; int n = sizeof ( price ) / sizeof ( price [ 0 ] ) ; int S [ n ] ; calculateSpan ( price , n , S ) ; printArray ( S , n ) ; return 0 ; }
The Stock Span Problem | C ++ linear time solution for stock span problem ; A stack based efficient method to calculate stock span values ; Create a stack and push index of first element to it ; Span value of first element is always 1 ; Calculate span values for rest of the elements ; Pop elements from stack while stack is not empty and top of stack is smaller than price [ i ] ; If stack becomes empty , then price [ i ] is greater than all elements on left of it , i . e . , price [ 0 ] , price [ 1 ] , . . price [ i - 1 ] . Else price [ i ] is greater than elements after top of stack ; Push this element to stack ; A utility function to print elements of array ; Driver program to test above function ; Fill the span values in array S [ ] ; print the calculated span values
#include <iostream> NEW_LINE #include <stack> NEW_LINE using namespace std ; void calculateSpan ( int price [ ] , int n , int S [ ] ) { stack < int > st ; st . push ( 0 ) ; S [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { while ( ! st . empty ( ) && price [ st . top ( ) ] <= price [ i ] ) st . pop ( ) ; S [ i ] = ( st . empty ( ) ) ? ( i + 1 ) : ( i - st . top ( ) ) ; st . push ( i ) ; } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int price [ ] = { 10 , 4 , 5 , 90 , 120 , 80 } ; int n = sizeof ( price ) / sizeof ( price [ 0 ] ) ; int S [ n ] ; calculateSpan ( price , n , S ) ; printArray ( S , n ) ; return 0 ; }
The Stock Span Problem | C ++ program for a linear time solution for stock span problem without using stack ; An efficient method to calculate stock span values implementing the same idea without using stack ; Span value of first element is always 1 ; Calculate span values for rest of the elements ; A utility function to print elements of array ; Driver program to test above function ; Fill the span values in array S [ ] ; print the calculated span values
#include <iostream> NEW_LINE #include <stack> NEW_LINE using namespace std ; void calculateSpan ( int A [ ] , int n , int ans [ ] ) { ans [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int counter = 1 ; while ( ( i - counter ) >= 0 && A [ i ] >= A [ i - counter ] ) { counter += ans [ i - counter ] ; } ans [ i ] = counter ; } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int price [ ] = { 10 , 4 , 5 , 90 , 120 , 80 } ; int n = sizeof ( price ) / sizeof ( price [ 0 ] ) ; int S [ n ] ; calculateSpan ( price , n , S ) ; printArray ( S , n ) ; return 0 ; }
Next Greater Element | Simple C ++ program to print next greater elements in a given array ; prints element and NGE pair for all elements of arr [ ] of size n ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void printNGE ( int arr [ ] , int n ) { int next , i , j ; for ( i = 0 ; i < n ; i ++ ) { next = -1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { next = arr [ j ] ; break ; } } cout << arr [ i ] << " ▁ - - ▁ " << next << endl ; } } int main ( ) { int arr [ ] = { 11 , 13 , 21 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printNGE ( arr , n ) ; return 0 ; }
Convert a Binary Tree into its Mirror Tree | C ++ program to convert a binary tree to its mirror ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; do the subtrees ; swap the pointers in this node ; Helper function to print Inorder traversal . ; Driver Code ; Print inorder traversal of the input tree ; Convert tree to its mirror ; Print inorder traversal of the mirror tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; struct Node * newNode ( int data ) { struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return ( node ) ; } void mirror ( struct Node * node ) { if ( node == NULL ) return ; else { struct Node * temp ; mirror ( node -> left ) ; mirror ( node -> right ) ; temp = node -> left ; node -> left = node -> right ; node -> right = temp ; } } void inOrder ( struct Node * node ) { if ( node == NULL ) return ; inOrder ( node -> left ) ; cout << node -> data << " ▁ " ; inOrder ( node -> right ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; cout << " Inorder ▁ traversal ▁ of ▁ the ▁ constructed " << " ▁ tree ▁ is " << endl ; inOrder ( root ) ; mirror ( root ) ; cout << " Inorder traversal of the mirror tree " << " ▁ is ▁ STRNEWLINE " ; inOrder ( root ) ; return 0 ; }
Number of NGEs to the right |  ; array to store the next greater element index ; use of stl stack in c ++ ; push the 0 th index to the stack ; traverse in the loop from 1 - nth index ; iterate till loop is empty ; get the topmost index in the stack ; if the current element is greater then the top index - th element , then this will be the next greatest index of the top index - th element ; initialize the cur index position 's  next greatest as index ; pop the cur index as its greater element has been found ; if not greater then break ; push the i index so that its next greatest can be found ; iterate for all other index left inside stack ; mark it as - 1 as no element in greater then it in right ; Function to count the number of next greater numbers to the right ; initializes the next array as 0 ; calls the function to pre - calculate the next greatest element indexes ; if the i - th element has no next greater element to right ; Count of next greater numbers to right . ; answers all queries in O ( 1 ) ; returns the number of next greater elements to the right . ; driver program to test the above function ; calls the function to count the number of greater elements to the right for every element . ; query 1 answered ; query 2 answered ; query 3 answered
#include <bits/stdc++.h> NEW_LINE using namespace std ; void fillNext ( int next [ ] , int a [ ] , int n ) { stack < int > s ; s . push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( ! s . empty ( ) ) { int cur = s . top ( ) ; if ( a [ cur ] < a [ i ] ) { next [ cur ] = i ; s . pop ( ) ; } else break ; } s . push ( i ) ; } while ( ! s . empty ( ) ) { int cur = s . top ( ) ; next [ cur ] = -1 ; s . pop ( ) ; } } void count ( int a [ ] , int dp [ ] , int n ) { int next [ n ] ; memset ( next , 0 , sizeof ( next ) ) ; fillNext ( next , a , n ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( next [ i ] == -1 ) dp [ i ] = 0 ; else dp [ i ] = 1 + dp [ next [ i ] ] ; } } int answerQuery ( int dp [ ] , int index ) { return dp [ index ] ; } int main ( ) { int a [ ] = { 3 , 4 , 2 , 7 , 5 , 8 , 10 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int dp [ n ] ; count ( a , dp , n ) ; cout << answerQuery ( dp , 3 ) << endl ; cout << answerQuery ( dp , 6 ) << endl ; cout << answerQuery ( dp , 1 ) << endl ; return 0 ; }
Number of NGEs to the right |  ; use of stl stack in c ++ ; push the ( n - 1 ) th index to the stack ; traverse in reverse order ; if no element is greater than arr [ i ] the number of NGEs to right is 0 ; number of NGEs to right of arr [ i ] is one greater than the number of NGEs to right of higher number to its right ; reverse again because values are in reverse order ; returns the vector of number of next greater elements to the right of each index . ; Driver code ; query 1 answered ; query 2 answered ; query 3 answered
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > no_NGN ( int arr [ ] , int n ) { vector < int > nxt ; stack < int > s ; nxt . push_back ( 0 ) ; s . push ( n - 1 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { while ( ! s . empty ( ) && arr [ i ] >= arr [ s . top ( ) ] ) s . pop ( ) ; if ( s . empty ( ) ) nxt . push_back ( 0 ) ; else nxt . push_back ( nxt [ n - s . top ( ) - 1 ] + 1 ) ; s . push ( i ) ; } reverse ( nxt . begin ( ) , nxt . end ( ) ) ; return nxt ; } int main ( ) { int n = 8 ; int arr [ ] = { 3 , 4 , 2 , 7 , 5 , 8 , 10 , 6 } ; vector < int > nxt = no_NGN ( arr , n ) ; cout << nxt [ 3 ] << endl ; cout << nxt [ 6 ] << endl ; cout << nxt [ 1 ] << endl ; return 0 ; }
Maximum product of indexes of next greater on left and right | C ++ program to find the max LRproduct [ i ] among all i ; function to find just next greater element in left side ; checking if current element is greater than top ; on index of top store the current element index which is just greater than top element ; else push the current element in stack ; function to find just next greater element in right side ; checking if current element is greater than top ; on index of top store the current element index which is just greater than top element stored index should be start with 1 ; else push the current element in stack ; Function to find maximum LR product ; for each element storing the index of just greater element in left side ; for each element storing the index of just greater element in right side ; finding the max index product ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000 NEW_LINE vector < int > nextGreaterInLeft ( int a [ ] , int n ) { vector < int > left_index ( MAX , 0 ) ; stack < int > s ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( ! s . empty ( ) && a [ i ] > a [ s . top ( ) - 1 ] ) { int r = s . top ( ) ; s . pop ( ) ; left_index [ r - 1 ] = i + 1 ; } s . push ( i + 1 ) ; } return left_index ; } vector < int > nextGreaterInRight ( int a [ ] , int n ) { vector < int > right_index ( MAX , 0 ) ; stack < int > s ; for ( int i = 0 ; i < n ; ++ i ) { while ( ! s . empty ( ) && a [ i ] > a [ s . top ( ) - 1 ] ) { int r = s . top ( ) ; s . pop ( ) ; right_index [ r - 1 ] = i + 1 ; } s . push ( i + 1 ) ; } return right_index ; } int LRProduct ( int arr [ ] , int n ) { vector < int > left = nextGreaterInLeft ( arr , n ) ; vector < int > right = nextGreaterInRight ( arr , n ) ; int ans = -1 ; for ( int i = 1 ; i <= n ; i ++ ) { ans = max ( ans , left [ i ] * right [ i ] ) ; } return ans ; } int main ( ) { int arr [ ] = { 5 , 4 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 1 ] ) ; cout << LRProduct ( arr , n ) ; return 0 ; }
The Celebrity Problem | C ++ program to find celebrity ; Max # of persons in the party ; Person with 2 is celebrity ; Returns - 1 if celebrity is not present . If present , returns id ( value from 0 to n - 1 ) . ; the graph needs not be constructed as the edges can be found by using knows function degree array ; ; query for all edges ; set the degrees ; find a person with indegree n - 1 and out degree 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <list> NEW_LINE using namespace std ; #define N  8 NEW_LINE bool MATRIX [ N ] [ N ] = { { 0 , 0 , 1 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 } } ; bool knows ( int a , int b ) { return MATRIX [ a ] [ b ] ; } int findCelebrity ( int n ) { int indegree [ n ] = { 0 } , outdegree [ n ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int x = knows ( i , j ) ; outdegree [ i ] += x ; indegree [ j ] += x ; } } for ( int i = 0 ; i < n ; i ++ ) if ( indegree [ i ] == n - 1 && outdegree [ i ] == 0 ) return i ; return -1 ; } int main ( ) { int n = 4 ; int id = findCelebrity ( n ) ; id == -1 ? cout << " No ▁ celebrity " : cout << " Celebrity ▁ ID ▁ " << id ; return 0 ; }
The Celebrity Problem | C ++ program to find celebrity ; Max # of persons in the party ; Person with 2 is celebrity ; Returns - 1 if a ' potential ▁ celebrity ' is not present . If present , returns id ( value from 0 to n - 1 ) . ; base case - when n reaches 0 , returns - 1 since n represents the number of people , 0 people implies no celebrity ( = - 1 ) ; find the celebrity with n - 1 persons ; if there are no celebrities ; if the id knows the nth person then the id cannot be a celebrity , but nth person could be one ; if the nth person knows the id , then the nth person cannot be a celebrity and the id could be one ; if there is no celebrity ; Returns - 1 if celebrity is not present . If present , returns id ( value from 0 to n - 1 ) . a wrapper over findCelebrity ; find the celebrity ; check if the celebrity found is really the celebrity ; check the id is really the celebrity ; if the person is known to everyone . ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <list> NEW_LINE using namespace std ; #define N  8 NEW_LINE bool MATRIX [ N ] [ N ] = { { 0 , 0 , 1 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 } } ; bool knows ( int a , int b ) { return MATRIX [ a ] [ b ] ; } int findPotentialCelebrity ( int n ) { if ( n == 0 ) return -1 ; int id = findPotentialCelebrity ( n - 1 ) ; if ( id == -1 ) return n - 1 ; else if ( knows ( id , n - 1 ) ) { return n - 1 ; } else if ( knows ( n - 1 , id ) ) { return id ; } return -1 ; } int Celebrity ( int n ) { int id = findPotentialCelebrity ( n ) ; if ( id == -1 ) return id ; else { int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( i != id ) { c1 += knows ( id , i ) ; c2 += knows ( i , id ) ; } if ( c1 == 0 && c2 == n - 1 ) return id ; return -1 ; } } int main ( ) { int n = 4 ; int id = Celebrity ( n ) ; id == -1 ? cout << " No ▁ celebrity " : cout << " Celebrity ▁ ID ▁ " << id ; return 0 ; }
Convert a Binary Tree into its Mirror Tree | Iterative CPP program to convert a Binary Tree to its mirror ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; Do BFS . While doing BFS , keep swapping left and right children ; pop top node from queue ; swap left child with right child ; push left and right children ; Helper function to print Inorder traversal . ; Driver program to test mirror ( ) ; Print inorder traversal of the input tree ; Convert tree to its mirror ; Print inorder traversal of the mirror tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } void mirror ( Node * root ) { if ( root == NULL ) return ; queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { Node * curr = q . front ( ) ; q . pop ( ) ; swap ( curr -> left , curr -> right ) ; if ( curr -> left ) q . push ( curr -> left ) ; if ( curr -> right ) q . push ( curr -> right ) ; } } void inOrder ( struct Node * node ) { if ( node == NULL ) return ; inOrder ( node -> left ) ; cout << node -> data << " ▁ " ; inOrder ( node -> right ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; cout << " Inorder traversal of the " STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL " constructed tree is " ; inOrder ( root ) ; mirror ( root ) ; cout << " Inorder traversal of the " STRNEWLINE TABSYMBOL TABSYMBOL " mirror tree is " ; inOrder ( root ) ; return 0 ; }
Delete middle element of a stack | C ++ code to delete middle of a stack without using additional data structure . ; Deletes middle of stack of size n . Curr is current item number ; If stack is empty or all items are traversed ; Remove current item ; Remove other items ; Put all items back except middle ; Driver function to test above functions ; push elements into the stack ; Printing stack after deletion of middle .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void deleteMid ( stack < char > & st , int n , int curr = 0 ) { if ( st . empty ( ) curr == n ) return ; char x = st . top ( ) ; st . pop ( ) ; deleteMid ( st , n , curr + 1 ) ; if ( curr != n / 2 ) st . push ( x ) ; } int main ( ) { stack < char > st ; st . push ( '1' ) ; st . push ( '2' ) ; st . push ( '3' ) ; st . push ( '4' ) ; st . push ( '5' ) ; st . push ( '6' ) ; st . push ( '7' ) ; deleteMid ( st , st . size ( ) ) ; while ( ! st . empty ( ) ) { char p = st . top ( ) ; st . pop ( ) ; cout << p << " ▁ " ; } return 0 ; }
Sorting array using Stacks | C ++ program to sort an array using stack ; This function return the sorted stack ; pop out the first element ; while temporary stack is not empty and top of stack is smaller than temp ; pop from temporary stack and push it to the input stack ; push temp in tempory of stack ; Push array elements to stack ; Sort the temporary stack ; Put stack elements in arrp [ ] ; main function
#include <bits/stdc++.h> NEW_LINE using namespace std ; stack < int > sortStack ( stack < int > input ) { stack < int > tmpStack ; while ( ! input . empty ( ) ) { int tmp = input . top ( ) ; input . pop ( ) ; while ( ! tmpStack . empty ( ) && tmpStack . top ( ) < tmp ) { input . push ( tmpStack . top ( ) ) ; tmpStack . pop ( ) ; } tmpStack . push ( tmp ) ; } return tmpStack ; } void sortArrayUsingStacks ( int arr [ ] , int n ) { stack < int > input ; for ( int i = 0 ; i < n ; i ++ ) input . push ( arr [ i ] ) ; stack < int > tmpStack = sortStack ( input ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = tmpStack . top ( ) ; tmpStack . pop ( ) ; } } int main ( ) { int arr [ ] = { 10 , 5 , 15 , 45 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArrayUsingStacks ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Reverse individual words | C ++ program to reverse individual words in a given string using STL list ; reverses individual words of a string ; Traverse given string and push all characters to stack until we see a space . ; When we see a space , we print contents of stack . ; Since there may not be space after last word . ; Driver program to test function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reverseWords ( string str ) { stack < char > st ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( str [ i ] != ' ▁ ' ) st . push ( str [ i ] ) ; else { while ( st . empty ( ) == false ) { cout << st . top ( ) ; st . pop ( ) ; } cout << " ▁ " ; } } while ( st . empty ( ) == false ) { cout << st . top ( ) ; st . pop ( ) ; } } int main ( ) { string str = " Geeks ▁ for ▁ Geeks " ; reverseWords ( str ) ; return 0 ; }
Count subarrays where second highest lie before highest | C ++ program to count number of distinct instance where second highest number lie before highest number in all subarrays . ; Finding the next greater element of the array . ; Finding the previous greater element of the array . ; Wrapper Function ; Finding previous largest element ; Finding next largest element ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAXN  100005 NEW_LINE using namespace std ; void makeNext ( int arr [ ] , int n , int nextBig [ ] ) { stack < pair < int , int > > s ; for ( int i = n - 1 ; i >= 0 ; i -- ) { nextBig [ i ] = i ; while ( ! s . empty ( ) && s . top ( ) . first < arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) nextBig [ i ] = s . top ( ) . second ; s . push ( pair < int , int > ( arr [ i ] , i ) ) ; } } void makePrev ( int arr [ ] , int n , int prevBig [ ] ) { stack < pair < int , int > > s ; for ( int i = 0 ; i < n ; i ++ ) { prevBig [ i ] = -1 ; while ( ! s . empty ( ) && s . top ( ) . first < arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) prevBig [ i ] = s . top ( ) . second ; s . push ( pair < int , int > ( arr [ i ] , i ) ) ; } } int wrapper ( int arr [ ] , int n ) { int nextBig [ MAXN ] ; int prevBig [ MAXN ] ; int maxi [ MAXN ] ; int ans = 0 ; makePrev ( arr , n , prevBig ) ; makeNext ( arr , n , nextBig ) ; for ( int i = 0 ; i < n ; i ++ ) if ( nextBig [ i ] != i ) maxi [ nextBig [ i ] - i ] = max ( maxi [ nextBig [ i ] - i ] , i - prevBig [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) ans += maxi [ i ] ; return ans ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << wrapper ( arr , n ) << endl ; return 0 ; }
Largest Rectangular Area in a Histogram | Set 2 | C ++ program to find maximum rectangular area in linear time ; The main function to find the maximum rectangular area under given histogram with n bars ; Create an empty stack . The stack holds indexes of hist [ ] array . The bars stored in stack are always in increasing order of their heights . ; Initialize max area ; To store top of stack ; To store area with top bar as the smallest bar ; Run through all bars of given histogram ; If this bar is higher than the bar on top stack , push it to stack ; If this bar is lower than top of stack , then calculate area of rectangle with stack top as the smallest ( or minimum height ) bar . ' i ' is ' right ▁ index ' for the top and element before top in stack is ' left ▁ index ' ; store the top index ; pop the top ; Calculate the area with hist [ tp ] stack as smallest bar ; update max area , if needed ; Now pop the remaining bars from stack and calculate area with every popped bar as the smallest bar ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMaxArea ( int hist [ ] , int n ) { stack < int > s ; int max_area = 0 ; int tp ; int area_with_top ; int i = 0 ; while ( i < n ) { if ( s . empty ( ) || hist [ s . top ( ) ] <= hist [ i ] ) s . push ( i ++ ) ; else { tp = s . top ( ) ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . empty ( ) ? i : i - s . top ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } } while ( s . empty ( ) == false ) { tp = s . top ( ) ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . empty ( ) ? i : i - s . top ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } return max_area ; } int main ( ) { int hist [ ] = { 6 , 2 , 5 , 4 , 5 , 1 , 6 } ; int n = sizeof ( hist ) / sizeof ( hist [ 0 ] ) ; cout << " Maximum ▁ area ▁ is ▁ " << getMaxArea ( hist , n ) ; return 0 ; }
Program for Tower of Hanoi | C ++ recursive function to solve tower of hanoi puzzle ; Driver code ; Number of disks ; A , B and C are names of rods
#include <bits/stdc++.h> NEW_LINE using namespace std ; void towerOfHanoi ( int n , char from_rod , char to_rod , char aux_rod ) { if ( n == 1 ) { cout << " Move ▁ disk ▁ 1 ▁ from ▁ rod ▁ " << from_rod << " ▁ to ▁ rod ▁ " << to_rod << endl ; return ; } towerOfHanoi ( n - 1 , from_rod , aux_rod , to_rod ) ; cout << " Move ▁ disk ▁ " << n << " ▁ from ▁ rod ▁ " << from_rod << " ▁ to ▁ rod ▁ " << to_rod << endl ; towerOfHanoi ( n - 1 , aux_rod , to_rod , from_rod ) ; } int main ( ) { int n = 4 ; towerOfHanoi ( n , ' A ' , ' C ' , ' B ' ) ; return 0 ; }