codeparrot/xlcost-text-to-code huggingface dataset:
Find the Nth Pure number | CPP program to find the Nth pure num ; Function to check if it is a power of 2 or not ; if a number belongs to 4 series it should lie between 2 ^ blocks - 1 to 2 ^ blocks + 2 ^ ( blocks - 1 ) - 1 ; Method to find pure number ; Iterate from 1 to N ; Check if number is power of two ; Distance to previous block numbers ; Distance to previous block numbers ; Driver Code ; Function call to find the Nth pure number
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( int N ) { double number = log ( N ) / log ( 2 ) ; int checker = int ( number ) ; return number - checker == 0 ; } bool isSeriesFour ( int N , int digits ) { int upperBound = int ( pow ( 2 , digits ) + pow ( 2 , digits - 1 ) - 1 ) ; int lowerBound = int ( pow ( 2 , digits ) - 1 ) ; return ( N >= lowerBound ) && ( N < upperBound ) ; } string getPureNumber ( int N ) { string numbers [ N + 1 ] ; numbers [ 0 ] = " " ; int blocks = 0 ; int displacement = 0 ; for ( int i = 1 ; i < N + 1 ; i ++ ) { if ( isPowerOfTwo ( i + 1 ) ) { blocks = blocks + 1 ; } if ( isSeriesFour ( i , blocks ) ) { displacement = int ( pow ( 2 , blocks - 1 ) ) ; numbers [ i ] = "4" + numbers [ i - displacement ] + "4" ; } else { displacement = int ( pow ( 2 , blocks ) ) ; numbers [ i ] = "5" + numbers [ i - displacement ] + "5" ; } } return numbers [ N ] ; } int main ( ) { int N = 5 ; string pure = getPureNumber ( N ) ; cout << pure << endl ; }
Count numbers less than N containing digits from the given set : Digit DP | C ++ implementation to find the count of numbers possible less than N , such that every digit is from the given set of digits ; Function to convert integer into the string ; Recursive function to find the count of numbers possible less than N , such that every digit is from the given set of digits ; Base case ; Condition when the subproblem is computed previously ; Condition when the number chosen till now is definietly smaller than the given number N ; Loop to traverse all the digits of the given set ; Loop to traverse all the digits from the given set ; Store the solution for current subproblem ; Function to count the numbers less then N from given set of digits ; Converting the number to string ; Initially no subproblem is solved till now ; Find the solution of all the number equal to the length of the given number N ; Loop to find the number less in in the length of the given number ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 15 ] [ 2 ] ; string convertToString ( int num ) { stringstream ss ; ss << num ; string s = ss . str ( ) ; return s ; } int calculate ( int pos , int tight , int D [ ] , int sz , string & num ) { if ( pos == num . length ( ) ) return 1 ; if ( dp [ pos ] [ tight ] != -1 ) return dp [ pos ] [ tight ] ; int val = 0 ; if ( tight == 0 ) { for ( int i = 0 ; i < sz ; i ++ ) { if ( D [ i ] < ( num [ pos ] - '0' ) ) { val += calculate ( pos + 1 , 1 , D , sz , num ) ; } else if ( D [ i ] == num [ pos ] - '0' ) val += calculate ( pos + 1 , tight , D , sz , num ) ; } } else { for ( int i = 0 ; i < sz ; i ++ ) { val += calculate ( pos + 1 , tight , D , sz , num ) ; } } return dp [ pos ] [ tight ] = val ; } int countNumbers ( int D [ ] , int N , int sz ) { string num = convertToString ( N ) ; int len = num . length ( ) ; memset ( dp , -1 , sizeof ( dp ) ) ; int ans = calculate ( 0 , 0 , D , sz , num ) ; for ( int i = 1 ; i < len ; i ++ ) ans += calculate ( i , 1 , D , sz , num ) ; return ans ; } int main ( ) { int sz = 3 ; int D [ sz ] = { 1 , 4 , 9 } ; int N = 10 ; cout << countNumbers ( D , N , sz ) ; return 0 ; }
Find Union and Intersection of two unsorted arrays | C ++ program for the above approach ; Defining map container mp ; Inserting array elements in mp ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printUnion ( int * a , int n , int * b , int m ) { map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp . insert ( { a [ i ] , i } ) ; for ( int i = 0 ; i < m ; i ++ ) mp . insert ( { b [ i ] , i } ) ; cout << " The ▁ union ▁ set ▁ of ▁ both ▁ arrays ▁ is ▁ : " << endl ; for ( auto itr = mp . begin ( ) ; itr != mp . end ( ) ; itr ++ ) cout << itr -> first << " ▁ " ; } int main ( ) { int a [ 7 ] = { 1 , 2 , 5 , 6 , 2 , 3 , 5 } ; int b [ 9 ] = { 2 , 4 , 5 , 6 , 8 , 9 , 4 , 6 , 5 } ; printUnion ( a , 7 , b , 9 ) ; }
Maximum sum of elements divisible by K from the given array |  ; Function to return the maximum sum divisible by k from elements of v ; check if sum of elements excluding the current one is divisible by k ; check if sum of elements including the current one is divisible by k ; Store the maximum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 1001 ] [ 1001 ] ; int find_max ( int i , int sum , vector < int > & v , int k ) { if ( i == v . size ( ) ) return 0 ; if ( dp [ i ] [ sum ] != -1 ) return dp [ i ] [ sum ] ; int ans = 0 ; if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) ans = find_max ( i + 1 , sum , v , k ) ; if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) ans = max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) ; return dp [ i ] [ sum ] = ans ; } int main ( ) { vector < int > arr = { 43 , 1 , 17 , 26 , 15 } ; int k = 16 ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << find_max ( 0 , 0 , arr , k ) ; }
Find Union and Intersection of two unsorted arrays | A C ++ program to print union and intersection of two unsorted arrays ; Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding union , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort the first array and print its elements ( these two steps can be swapped as order in output is not important ) ; Search every element of bigger array in smaller array and print the element if not found ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding intersection , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort smaller array arr1 [ 0. . m - 1 ] ; Search every element of bigger array in smaller array and print the element if found ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be presen in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver program to test above function ; Function call
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int binarySearch ( int arr [ ] , int l , int r , int x ) ; void printUnion ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { if ( m > n ) { int * tempp = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } sort ( arr1 , arr1 + m ) ; for ( int i = 0 ; i < m ; i ++ ) cout << arr1 [ i ] << " ▁ " ; for ( int i = 0 ; i < n ; i ++ ) if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) == -1 ) cout << arr2 [ i ] << " ▁ " ; } void printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { if ( m > n ) { int * tempp = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } sort ( arr1 , arr1 + m ) ; for ( int i = 0 ; i < n ; i ++ ) if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) != -1 ) cout << arr2 [ i ] << " ▁ " ; } int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return -1 ; } int main ( ) { int arr1 [ ] = { 7 , 1 , 5 , 2 , 3 , 6 } ; int arr2 [ ] = { 3 , 8 , 6 , 20 , 7 } ; int m = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int n = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << " Union ▁ of ▁ two ▁ arrays ▁ is ▁ n " ; printUnion ( arr1 , arr2 , m , n ) ; cout << " nIntersection ▁ of ▁ two ▁ arrays ▁ is ▁ n " ; printIntersection ( arr1 , arr2 , m , n ) ; return 0 ; }
Find the maximum sum leaf to root path in a Binary Tree | CPP program to find maximum sum leaf to root path in Binary Tree ; A tree node structure ; A utility function that prints all nodes on the path from root to target_leaf ; base case ; return true if this node is the target_leaf or target leaf is present in one of its descendants ; This function Sets the target_leaf_ref to refer the leaf node of the maximum path sum . Also , returns the max_sum using max_sum_ref ; Update current sum to hold sum of nodes on path from root to this node ; If this is a leaf node and path to this node has maximum sum so far , then make this node target_leaf ; If this is not a leaf node , then recur down to find the target_leaf ; Returns the maximum sum and prints the nodes on max sum path ; base case ; find the target leaf and maximum sum ; print the path from root to the target leaf ; return maximum sum ; Utility function to create a new Binary Tree node ; Driver function to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; bool printPath ( node * root , node * target_leaf ) { if ( root == NULL ) return false ; if ( root == target_leaf || printPath ( root -> left , target_leaf ) || printPath ( root -> right , target_leaf ) ) { cout << root -> data << " ▁ " ; return true ; } return false ; } void getTargetLeaf ( node * Node , int * max_sum_ref , int curr_sum , node * * target_leaf_ref ) { if ( Node == NULL ) return ; curr_sum = curr_sum + Node -> data ; if ( Node -> left == NULL && Node -> right == NULL ) { if ( curr_sum > * max_sum_ref ) { * max_sum_ref = curr_sum ; * target_leaf_ref = Node ; } } getTargetLeaf ( Node -> left , max_sum_ref , curr_sum , target_leaf_ref ) ; getTargetLeaf ( Node -> right , max_sum_ref , curr_sum , target_leaf_ref ) ; } int maxSumPath ( node * Node ) { if ( Node == NULL ) return 0 ; node * target_leaf ; int max_sum = INT_MIN ; getTargetLeaf ( Node , & max_sum , 0 , & target_leaf ) ; printPath ( Node , target_leaf ) ; return max_sum ; } node * newNode ( int data ) { node * temp = new node ; temp -> data = data ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } int main ( ) { node * root = NULL ; root = newNode ( 10 ) ; root -> left = newNode ( -2 ) ; root -> right = newNode ( 7 ) ; root -> left -> left = newNode ( 8 ) ; root -> left -> right = newNode ( -4 ) ; cout << " Following ▁ are ▁ the ▁ nodes ▁ on ▁ the ▁ maximum ▁ " " sum ▁ path ▁ STRNEWLINE " ; int sum = maxSumPath ( root ) ; cout << " Sum of the nodes is " return 0 ; }
Find Union and Intersection of two unsorted arrays | C ++ code to find intersection when elements may not be distinct ; Function to find intersection ; when both are equal ; Driver Code ; sort ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void intersection ( int a [ ] , int b [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( a [ i ] > b [ j ] ) { j ++ ; } else if ( b [ j ] > a [ i ] ) { i ++ ; } else { cout << a [ i ] << " ▁ " ; i ++ ; j ++ ; } } } int main ( ) { int a [ ] = { 1 , 3 , 2 , 3 , 3 , 4 , 5 , 5 , 6 } ; int b [ ] = { 3 , 3 , 5 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = sizeof ( b ) / sizeof ( b [ 0 ] ) ; sort ( a , a + n ) ; sort ( b , b + m ) ; intersection ( a , b , n , m ) ; }
Count of 1 's in any path in a Binary Tree | C ++ implementation of the above approach ; A binary tree node ; A utility function to allocate a new node ; This function updates overall count of 1 in ' res ' And returns count 1 s going through root . ; Base Case ; l and r store count of 1 s going through left and right child of root respectively ; maxCount represents the count of 1 s when the Node under consideration is the root of the maxCount path and no ancestors of the root are there in maxCount path ; if the value at node is 1 then its count will be considered including the leftCount and the rightCount ; Store the Maximum Result . ; if the value at node is 1 then its count will be considered including the maximum of leftCount or the rightCount ; Returns maximum count of 1 in any path in tree with given root ; Initialize result ; Compute and return result ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * newNode = new Node ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return ( newNode ) ; } int countUntil ( Node * root , int & res ) { if ( root == NULL ) return 0 ; int l = countUntil ( root -> left , res ) ; int r = countUntil ( root -> right , res ) ; int maxCount ; if ( root -> data == 1 ) maxCount = l + r + 1 ; else maxCount = l + r ; res = max ( res , maxCount ) ; if ( root -> data == 1 ) return max ( l , r ) + 1 ; else return max ( l , r ) ; } int findMaxCount ( Node * root ) { int res = INT_MIN ; countUntil ( root , res ) ; return res ; } int main ( void ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 0 ) ; root -> right = newNode ( 1 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 1 ) ; root -> left -> right -> left = newNode ( 1 ) ; root -> left -> right -> right = newNode ( 0 ) ; cout << findMaxCount ( root ) ; return 0 ; }
Find Union and Intersection of two unsorted arrays | CPP program to find union and intersection using sets ; Prints union of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; Prints intersection of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; Driver Program ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printUnion ( int arr1 [ ] , int arr2 [ ] , int n1 , int n2 ) { set < int > hs ; for ( int i = 0 ; i < n1 ; i ++ ) hs . insert ( arr1 [ i ] ) ; for ( int i = 0 ; i < n2 ; i ++ ) hs . insert ( arr2 [ i ] ) ; for ( auto it = hs . begin ( ) ; it != hs . end ( ) ; it ++ ) cout << * it << " ▁ " ; cout << endl ; } void printIntersection ( int arr1 [ ] , int arr2 [ ] , int n1 , int n2 ) { set < int > hs ; for ( int i = 0 ; i < n1 ; i ++ ) hs . insert ( arr1 [ i ] ) ; for ( int i = 0 ; i < n2 ; i ++ ) if ( hs . find ( arr2 [ i ] ) != hs . end ( ) ) cout << arr2 [ i ] << " ▁ " ; } int main ( ) { int arr1 [ ] = { 7 , 1 , 5 , 2 , 3 , 6 } ; int arr2 [ ] = { 3 , 8 , 6 , 20 , 7 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; printUnion ( arr1 , arr2 , n1 , n2 ) ; printIntersection ( arr1 , arr2 , n1 , n2 ) ; return 0 ; }
Minimum number of coins that can generate all the values in the given range | C ++ implementation of the approach ; Function to return the count of minimum coins required ; To store the required sequence ; Creating list of the sum of all previous bit values including that bit value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int index ( vector < int > vec , int value ) { vector < int > :: iterator it ; it = find ( vec . begin ( ) , vec . end ( ) , value ) ; return ( it - vec . begin ( ) ) ; } int findCount ( int N ) { vector < int > list ; int sum = 0 ; int i ; for ( i = 0 ; i < 20 ; i ++ ) { sum += pow ( 2 , i ) ; list . push_back ( sum ) ; } for ( i = 0 ; i < 20 ; i ++ ) { if ( list [ i ] >= N ) { return ( index ( list , list [ i ] ) + 1 ) ; } } } int main ( ) { int N = 10 ; cout << findCount ( N ) << endl ; return 0 ; }
Sort an array of 0 s , 1 s and 2 s | C ++ program to sort an array with 0 , 1 and 2 in a single pass ; Function to sort the input array , the array is assumed to have values in { 0 , 1 , 2 } ; Function to print array arr [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sort012 ( int a [ ] , int arr_size ) { int lo = 0 ; int hi = arr_size - 1 ; int mid = 0 ; while ( mid <= hi ) { switch ( a [ mid ] ) { case 0 : swap ( a [ lo ++ ] , a [ mid ++ ] ) ; break ; case 1 : mid ++ ; break ; case 2 : swap ( a [ mid ] , a [ hi -- ] ) ; break ; } } } void printArray ( int arr [ ] , int arr_size ) { for ( int i = 0 ; i < arr_size ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sort012 ( arr , n ) ; cout << " array ▁ after ▁ segregation ▁ " ; printArray ( arr , n ) ; return 0 ; }
Calculate the number of set bits for every number from 0 to N | C ++ implementation of the approach ; Function to find the count of set bits in all the integers from 0 to n ; dp [ i ] will store the count of set bits in i ; Count of set bits in 0 is 0 ; For every number starting from 1 ; If current number is even ; Count of set bits in i is equal to the count of set bits in ( i / 2 ) ; If current element is odd ; Count of set bits in i is equal to the count of set bits in ( i / 2 ) + 1 ; Print the count of set bits in i ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSetBits ( int n ) { int dp [ n + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; cout << dp [ 0 ] << " ▁ " ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) { dp [ i ] = dp [ i / 2 ] ; } else { dp [ i ] = dp [ i / 2 ] + 1 ; } cout << dp [ i ] << " ▁ " ; } } int main ( ) { int n = 5 ; findSetBits ( n ) ; return 0 ; }
Sort an array of 0 s , 1 s and 2 s | C ++ implementation of the approach ; Utility function to print the contents of an array ; Function to sort the array of 0 s , 1 s and 2 s ; Count the number of 0 s , 1 s and 2 s in the array ; Update the array ; Store all the 0 s in the beginning ; Then all the 1 s ; Finally all the 2 s ; Print the sorted array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void sortArr ( int arr [ ] , int n ) { int i , cnt0 = 0 , cnt1 = 0 , cnt2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { switch ( arr [ i ] ) { case 0 : cnt0 ++ ; break ; case 1 : cnt1 ++ ; break ; case 2 : cnt2 ++ ; break ; } } i = 0 ; while ( cnt0 > 0 ) { arr [ i ++ ] = 0 ; cnt0 -- ; } while ( cnt1 > 0 ) { arr [ i ++ ] = 1 ; cnt1 -- ; } while ( cnt2 > 0 ) { arr [ i ++ ] = 2 ; cnt2 -- ; } printArr ( arr , n ) ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; sortArr ( arr , n ) ; return 0 ; }
Count of subsets with sum equal to X |  ; Initializing the matrix ; Initializing the first value of matrix ; if the value is greater than the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subsetSum ( int a [ ] , int n , int sum ) { int tab [ n + 1 ] [ sum + 1 ] ; tab [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= sum ; i ++ ) tab [ 0 ] [ i ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) tab [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( a [ i - 1 ] > j ) tab [ i ] [ j ] = tab [ i - 1 ] [ j ] ; else { tab [ i ] [ j ] = tab [ i - 1 ] [ j ] + tab [ i - 1 ] [ j - a [ i - 1 ] ] ; } } } return tab [ n ] [ sum ] ; } int main ( ) { int n = 4 ; int a [ ] = { 3 , 3 , 3 , 3 } ; int sum = 6 ; cout << ( subsetSum ( a , n , sum ) ) ; }
Largest sub | C ++ implementation of the approach ; 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 the stack ; To store area with top 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 ; Function to find largest sub matrix with all equal elements ; To find largest sub matrix with all elements 1 ; Fill dp [ ] [ ] by traversing each column from bottom to up ; Maintain the histogram array ; Find maximum area rectangle in Histogram ; To fill dp [ ] [ ] for finding largest sub matrix with all elements 0 ; Maintain the histogram array ; Find maximum area rectangle in Histogram ; Driver code
#include <bits/stdc++.h> NEW_LINE #define row  6 NEW_LINE #define col  8 NEW_LINE using namespace std ; int cal ( 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 largestMatrix ( int a [ ] [ col ] ) { int dp [ row ] [ col ] ; for ( int i = 0 ; i < col ; i ++ ) { int cnt = 0 ; for ( int j = row - 1 ; j >= 0 ; j -- ) { dp [ j ] [ i ] = 0 ; if ( a [ j ] [ i ] == 1 ) { cnt ++ ; dp [ j ] [ i ] = cnt ; } else { cnt = 0 ; } } } int ans = -1 ; for ( int i = 0 ; i < row ; i ++ ) { int hist [ col ] ; for ( int j = 0 ; j < col ; j ++ ) { hist [ j ] = dp [ i ] [ j ] ; } ans = max ( ans , cal ( hist , col ) ) ; } for ( int i = 0 ; i < col ; i ++ ) { int cnt = 0 ; for ( int j = row - 1 ; j >= 0 ; j -- ) { dp [ j ] [ i ] = 0 ; if ( a [ j ] [ i ] == 0 ) { cnt ++ ; dp [ j ] [ i ] = cnt ; } else { cnt = 0 ; } } } for ( int i = 0 ; i < row ; i ++ ) { int hist [ col ] ; for ( int j = 0 ; j < col ; j ++ ) { hist [ j ] = dp [ i ] [ j ] ; } ans = max ( ans , cal ( hist , col ) ) ; } return ans ; } int main ( ) { int a [ row ] [ col ] = { { 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 } , { 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 } , { 1 , 0 , 0 , 1 , 1 , 1 , 0 , 0 } , { 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 , 1 , 1 , 1 , 0 } , { 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 } } ; cout << largestMatrix ( a ) ; return 0 ; }
Find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted | C ++ program to find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted ; step 1 ( a ) of above algo ; step 1 ( b ) of above algo ; step 2 ( a ) of above algo ; step 2 ( b ) of above algo ; step 2 ( c ) of above algo ; step 3 of above algo
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printUnsorted ( int arr [ ] , int n ) { int s = 0 , e = n - 1 , i , max , min ; for ( s = 0 ; s < n - 1 ; s ++ ) { if ( arr [ s ] > arr [ s + 1 ] ) break ; } if ( s == n - 1 ) { cout << " The ▁ complete ▁ array ▁ is ▁ sorted " ; return ; } for ( e = n - 1 ; e > 0 ; e -- ) { if ( arr [ e ] < arr [ e - 1 ] ) break ; } max = arr [ s ] ; min = arr [ s ] ; for ( i = s + 1 ; i <= e ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; if ( arr [ i ] < min ) min = arr [ i ] ; } for ( i = 0 ; i < s ; i ++ ) { if ( arr [ i ] > min ) { s = i ; break ; } } for ( i = n - 1 ; i >= e + 1 ; i -- ) { if ( arr [ i ] < max ) { e = i ; break ; } } cout << " The ▁ unsorted ▁ subarray ▁ which " << " ▁ makes ▁ the ▁ given ▁ array " << endl << " sorted ▁ lies ▁ between ▁ the ▁ indees ▁ " << s << " ▁ and ▁ " << e ; return ; } int main ( ) { int arr [ ] = { 10 , 12 , 20 , 30 , 25 , 40 , 32 , 31 , 35 , 50 , 60 } ; int arr_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printUnsorted ( arr , arr_size ) ; getchar ( ) ; return 0 ; }