codeparrot/xlcost-text-to-code huggingface dataset:
Largest subset of rectangles such that no rectangle fit in any other rectangle | C ++ implementation of the approach ; Recursive function to get the largest subset ; Base case when it exceeds ; If the state has been visited previously ; Initialize ; No elements in subset yet ; First state which includes current index ; Second state which does not include current index ; If the rectangle fits in , then do not include the current index in subset ; First state which includes current index ; Second state which does not include current index ; Function to get the largest subset ; Initialize the DP table with - 1 ; Sort the array ; Get the answer ; Driver code ; ( height , width ) pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  10 NEW_LINE int dp [ N ] [ N ] ; int findLongest ( pair < int , int > a [ ] , int n , int present , int previous ) { if ( present == n ) { return 0 ; } else if ( previous != -1 ) { if ( dp [ present ] [ previous ] != -1 ) return dp [ present ] [ previous ] ; } int ans = 0 ; if ( previous == -1 ) { ans = 1 + findLongest ( a , n , present + 1 , present ) ; ans = max ( ans , findLongest ( a , n , present + 1 , previous ) ) ; } else { int h1 = a [ previous ] . first ; int h2 = a [ present ] . first ; int w1 = a [ previous ] . second ; int w2 = a [ present ] . second ; if ( ( h1 <= h2 && w1 <= w2 ) ) { ans = max ( ans , findLongest ( a , n , present + 1 , previous ) ) ; } else { ans = 1 + findLongest ( a , n , present + 1 , present ) ; ans = max ( ans , findLongest ( a , n , present + 1 , previous ) ) ; } } return dp [ present ] [ previous ] = ans ; } int getLongest ( pair < int , int > a [ ] , int n ) { memset ( dp , -1 , sizeof dp ) ; sort ( a , a + n ) ; int ans = findLongest ( a , n , 0 , -1 ) ; return ans ; } int main ( ) { pair < int , int > a [ ] = { { 1 , 5 } , { 2 , 4 } , { 1 , 1 } , { 3 , 3 } } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << getLongest ( a , n ) ; return 0 ; }
Maximum length subsequence such that adjacent elements in the subsequence have a common factor | C ++ implementation of the above approach ; Function to compute least prime divisor of i ; Function that returns the maximum length subsequence such that adjacent elements have a common factor . ; Initialize dp array with 1. ; p has appeared at least once . ; Update latest occurrence of prime p . ; Take maximum value as the answer . ; Driver code
#include <bits/stdc++.h> NEW_LINE #define N  100005 NEW_LINE #define MAX  10000002 NEW_LINE using namespace std ; int lpd [ MAX ] ; void preCompute ( ) { memset ( lpd , 0 , sizeof ( lpd ) ) ; lpd [ 0 ] = lpd [ 1 ] = 1 ; for ( int i = 2 ; i * i < MAX ; i ++ ) { for ( int j = i * 2 ; j < MAX ; j += i ) { if ( lpd [ j ] == 0 ) { lpd [ j ] = i ; } } } for ( int i = 2 ; i < MAX ; i ++ ) { if ( lpd [ i ] == 0 ) { lpd [ i ] = i ; } } } int maxLengthSubsequence ( int arr [ ] , int n ) { int dp [ N ] ; unordered_map < int , int > pos ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { while ( arr [ i ] > 1 ) { int p = lpd [ arr [ i ] ] ; if ( pos [ p ] ) { dp [ i ] = max ( dp [ i ] , 1 + dp [ pos [ p ] ] ) ; } pos [ p ] = i ; while ( arr [ i ] % p == 0 ) arr [ i ] /= p ; } } int ans = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { ans = max ( ans , dp [ i ] ) ; } return ans ; } int main ( ) { int arr [ ] = { 13 , 2 , 8 , 6 , 3 , 1 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; preCompute ( ) ; cout << maxLengthSubsequence ( arr , n ) ; return 0 ; }
Number of ways to partition a string into two balanced subsequences | C ++ implementation of the above approach ; For maximum length of input string ; Declaring the DP table ; Function to calculate the number of valid assignments ; Return 1 if both subsequences are balanced ; Increment the count if it an opening bracket ; Decrement the count if it a closing bracket ; Driver code ; Initializing the DP table ; Initial value for c_x and c_y is zero
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10 ; int F [ MAX ] [ MAX ] [ MAX ] ; int noOfAssignments ( string & S , int & n , int i , int c_x , int c_y ) { if ( F [ i ] [ c_x ] [ c_y ] != -1 ) return F [ i ] [ c_x ] [ c_y ] ; if ( i == n ) { F [ i ] [ c_x ] [ c_y ] = ! c_x && ! c_y ; return F [ i ] [ c_x ] [ c_y ] ; } if ( S [ i ] == ' ( ' ) { F [ i ] [ c_x ] [ c_y ] = noOfAssignments ( S , n , i + 1 , c_x + 1 , c_y ) + noOfAssignments ( S , n , i + 1 , c_x , c_y + 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } F [ i ] [ c_x ] [ c_y ] = 0 ; if ( c_x ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( S , n , i + 1 , c_x - 1 , c_y ) ; if ( c_y ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( S , n , i + 1 , c_x , c_y - 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } int main ( ) { string S = " ( ( ) ) " ; int n = S . length ( ) ; memset ( F , -1 , sizeof ( F ) ) ; cout << noOfAssignments ( S , n , 0 , 0 , 0 ) ; return 0 ; }
Gould 's Sequence | CPP program to generate Gould 's Sequence ; Utility function to count odd numbers in ith row of Pascals 's triangle ; Initialize count as zero ; Return 2 ^ count ; Function to generate gould 's Sequence ; loop to generate gould 's Sequence up to n ; Driver code ; Get n ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOddNumber ( int row_num ) { unsigned int count = 0 ; while ( row_num ) { count += row_num & 1 ; row_num >>= 1 ; } return ( 1 << count ) ; } void gouldSequence ( int n ) { for ( int row_num = 0 ; row_num < n ; row_num ++ ) { cout << countOddNumber ( row_num ) << " ▁ " ; } } int main ( ) { int n = 16 ; gouldSequence ( n ) ; return 0 ; }
Count numbers ( smaller than or equal to N ) with given digit sum |  ; N can be max 10 ^ 18 and hence digitsum will be 162 maximum . ; If sum_so_far equals to given sum then return 1 else 0 ; Our constructed number should not become greater than N . ; If tight is true then it will also be true for ( i + 1 ) digit . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long dp [ 18 ] [ 2 ] [ 162 ] ; long long solve ( int i , bool tight , int sum_so_far , int Sum , string number , int len ) { if ( i == len ) { if ( sum_so_far == Sum ) return 1 ; else return 0 ; } long long & ans = dp [ i ] [ tight ] [ sum_so_far ] ; if ( ans != -1 ) { return ans ; } ans = 0 ; bool ntight ; int nsum_so_far ; for ( char currdigit = '0' ; currdigit <= '9' ; currdigit ++ ) { if ( ! tight && currdigit > number [ i ] ) { break ; } ntight = tight || currdigit < number [ i ] ; nsum_so_far = sum_so_far + ( currdigit - '0' ) ; ans += solve ( i + 1 , ntight , nsum_so_far , Sum , number , len ) ; } return ans ; } int main ( ) { long long count = 0 ; long long sum = 4 ; string number = "100" ; memset ( dp , -1 , sizeof dp ) ; cout << solve ( 0 , 0 , 0 , sum , number , number . size ( ) ) ; return 0 ; }
Longest dividing subsequence | Dynamic Programming C ++ implementation of lds problem ; lds ( ) returns the length of the longest dividing subsequence in arr [ ] of size n ; Compute optimized lds values in bottom up manner ; Return maximum value in lds [ ] ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lds ( int arr [ ] , int n ) { int lds [ n ] ; lds [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { lds [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) if ( lds [ j ] != 0 && arr [ i ] % arr [ j ] == 0 ) lds [ i ] = max ( lds [ i ] , lds [ j ] + 1 ) ; } return * max_element ( lds , lds + n ) ; } int main ( ) { int arr [ ] = { 2 , 11 , 16 , 12 , 36 , 60 , 71 , 17 , 29 , 144 , 288 , 129 , 432 , 993 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Length ▁ of ▁ lds ▁ is ▁ % d STRNEWLINE " , lds ( arr , n ) ) ; return 0 ; }
Memoization ( 1D , 2D and 3D ) | A recursive implementation of LCS problem of three strings ; Utility function to get max of 2 integers ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; base case ; if equal , then check for next combination ; recursive call ; return the maximum of the three other possible states in recursion ; Driver Code
#include <bits/stdc++.h> NEW_LINE int max ( int a , int b ) ; int max ( int a , int b ) { return ( a > b ) ? a : b ; } int lcs ( char * X , char * Y , char * Z , int m , int n , int o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] and Y [ n - 1 ] == Z [ o - 1 ] ) { return 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; } else { return max ( lcs ( X , Y , Z , m , n - 1 , o ) , max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; } } int main ( ) { char X [ ] = " geeks " ; char Y [ ] = " geeksfor " ; char Z [ ] = " geeksforge " ; int m = strlen ( X ) ; int n = strlen ( Y ) ; int o = strlen ( Z ) ; printf ( " Length ▁ of ▁ LCS ▁ is ▁ % d " , lcs ( X , Y , Z , m , n , o ) ) ; return 0 ; }
Memoization ( 1D , 2D and 3D ) | A memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Utility function to get max of 2 integers ; Driver Code
#include <bits/stdc++.h> NEW_LINE int arr [ 100 ] [ 100 ] [ 100 ] ; int max ( int a , int b ) ; int lcs ( char * X , char * Y , char * Z , int m , int n , int o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( arr [ m - 1 ] [ n - 1 ] [ o - 1 ] != -1 ) return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; if ( X [ m - 1 ] == Y [ n - 1 ] and Y [ n - 1 ] == Z [ o - 1 ] ) { arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; } else { arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = max ( lcs ( X , Y , Z , m , n - 1 , o ) , max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; } } int max ( int a , int b ) { return ( a > b ) ? a : b ; } int main ( ) { memset ( arr , -1 , sizeof ( arr ) ) ; char X [ ] = " geeks " ; char Y [ ] = " geeksfor " ; char Z [ ] = " geeksforgeeks " ; int m = strlen ( X ) ; int n = strlen ( Y ) ; int o = strlen ( Z ) ; printf ( " Length ▁ of ▁ LCS ▁ is ▁ % d " , lcs ( X , Y , Z , m , n , o ) ) ; return 0 ; }
Find the probability of reaching all points after N moves from point N | C ++ program to calculate the probability of reaching all points after N moves from point N ; Function to calculate the probabilities ; Array where row represent the pass and the column represents the points on the line ; Initially the person can reach left or right with one move ; Calculate probabilities for N - 1 moves ; when the person moves from ith index in right direction when i moves has been done ; when the person moves from ith index in left direction when i moves has been done ; Print the arr ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printProbabilities ( int n , double left ) { double right = 1 - left ; double arr [ n + 1 ] [ 2 * n + 1 ] = { { 0 } } ; arr [ 1 ] [ n + 1 ] = right ; arr [ 1 ] [ n - 1 ] = left ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= 2 * n ; j ++ ) arr [ i ] [ j ] += ( arr [ i - 1 ] [ j - 1 ] * right ) ; for ( int j = 2 * n - 1 ; j >= 0 ; j -- ) arr [ i ] [ j ] += ( arr [ i - 1 ] [ j + 1 ] * left ) ; } for ( int i = 0 ; i < 2 * n + 1 ; i ++ ) printf ( " % 5.4f ▁ " , arr [ n ] [ i ] ) ; } int main ( ) { int n = 2 ; double left = 0.5 ; printProbabilities ( n , left ) ; return 0 ; }
Check if it is possible to reach a number by making jumps of two given length |  ; Function to perform BFS traversal to find minimum number of step needed to reach x from K ; Calculate GCD of d1 and d2 ; If position is not reachable return - 1 ; Queue for BFS ; Hash Table for marking visited positions ; we need 0 steps to reach K ; Mark starting position as visited ; stp is the number of steps to reach position s ; if position not visited add to queue and mark visited ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minStepsNeeded ( int k , int d1 , int d2 , int x ) { int gcd = __gcd ( d1 , d2 ) ; if ( ( k - x ) % gcd != 0 ) return -1 ; queue < pair < int , int > > q ; unordered_set < int > visited ; q . push ( { k , 0 } ) ; visited . insert ( k ) ; while ( ! q . empty ( ) ) { int s = q . front ( ) . first ; int stp = q . front ( ) . second ; if ( s == x ) return stp ; q . pop ( ) ; if ( visited . find ( s + d1 ) == visited . end ( ) ) { q . push ( { s + d1 , stp + 1 } ) ; visited . insert ( s + d1 ) ; } if ( visited . find ( s + d2 ) == visited . end ( ) ) { q . push ( { s + d2 , stp + 1 } ) ; visited . insert ( s + d2 ) ; } if ( visited . find ( s - d1 ) == visited . end ( ) ) { q . push ( { s - d1 , stp + 1 } ) ; visited . insert ( s - d1 ) ; } if ( visited . find ( s - d2 ) == visited . end ( ) ) { q . push ( { s - d2 , stp + 1 } ) ; visited . insert ( s - d2 ) ; } } } int main ( ) { int k = 10 , d1 = 4 , d2 = 6 , x = 8 ; cout << minStepsNeeded ( k , d1 , d2 , x ) ; return 0 ; }
Tiling with Dominoes | C ++ program to find no . of ways to fill a 3 xn board with 2 x1 dominoes . ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int countWays ( int n ) { int A [ n + 1 ] , B [ n + 1 ] ; A [ 0 ] = 1 , A [ 1 ] = 0 , B [ 0 ] = 0 , B [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] ; B [ i ] = A [ i - 1 ] + B [ i - 2 ] ; } return A [ n ] ; } int main ( ) { int n = 8 ; cout << countWays ( n ) ; return 0 ; }
Newman | C ++ program for n - th element of Newman - Conway Sequence ; Recursive Function to find the n - th element ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sequence ( int n ) { if ( n == 1 n == 2 ) return 1 ; else return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ; } int main ( ) { int n = 10 ; cout << sequence ( n ) ; return 0 ; }
Connect nodes at same level | CPP program to connect nodes at same level using extended pre - order traversal ; Constructor that allocates a new node with the given data and NULL left and right pointers . ; Sets the nextRight of root and calls connectRecur ( ) for other nodes ; Set the nextRight for root ; Set the next right for rest of the nodes ( other than root ) ; Set next right of all descendents of p . Assumption : p is a compete binary tree ; Base case ; Set the nextRight pointer for p 's left child ; Set the nextRight pointer for p 's right child p->nextRight  will be NULL if p is the right  most child at its level ; Set nextRight for other nodes in pre order fashion ; Driver code ; Constructed binary tree is 10 / \ 8 2 / 3 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; node * nextRight ; node ( int data ) { this -> data = data ; this -> left = NULL ; this -> right = NULL ; this -> nextRight = NULL ; } } ; void connectRecur ( node * p ) ; void connect ( node * p ) { p -> nextRight = NULL ; connectRecur ( p ) ; } void connectRecur ( node * p ) { if ( ! p ) return ; if ( p -> left ) p -> left -> nextRight = p -> right ; if ( p -> right ) p -> right -> nextRight = ( p -> nextRight ) ? p -> nextRight -> left : NULL ; connectRecur ( p -> left ) ; connectRecur ( p -> right ) ; } int main ( ) { node * root = new node ( 10 ) ; root -> left = new node ( 8 ) ; root -> right = new node ( 2 ) ; root -> left -> left = new node ( 3 ) ; connect ( root ) ; cout << " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ the ▁ tree " " ▁ ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) STRNEWLINE " ; cout << " nextRight ▁ of ▁ " << root -> data << " ▁ is ▁ " << ( root -> nextRight ? root -> nextRight -> data : -1 ) << endl ; cout << " nextRight ▁ of ▁ " << root -> left -> data << " ▁ is ▁ " << ( root -> left -> nextRight ? root -> left -> nextRight -> data : -1 ) << endl ; cout << " nextRight ▁ of ▁ " << root -> right -> data << " ▁ is ▁ " << ( root -> right -> nextRight ? root -> right -> nextRight -> data : -1 ) << endl ; cout << " nextRight ▁ of ▁ " << root -> left -> left -> data << " ▁ is ▁ " << ( root -> left -> left -> nextRight ? root -> left -> left -> nextRight -> data : -1 ) << endl ; return 0 ; }
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Base condition ; A participant can choose to consider ( 1 ) Remains single . Number of people reduce to ( x - 1 ) ( 2 ) Pairs with one of the ( x - 1 ) others . For every pairing , number of people reduce to ( x - 2 ) . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int numberOfWays ( int x ) { if ( x == 0 x == 1 ) return 1 ; else return numberOfWays ( x - 1 ) + ( x - 1 ) * numberOfWays ( x - 2 ) ; } int main ( ) { int x = 3 ; cout << numberOfWays ( x ) << endl ; return 0 ; }
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Driver code
#include <iostream> NEW_LINE using namespace std ; int numberOfWays ( int x ) { int dp [ x + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= x ; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; return dp [ x ] ; } int main ( ) { int x = 3 ; cout << numberOfWays ( x ) << endl ; return 0 ; }
Longest Repeated Subsequence | Refer https : www . geeksforgeeks . org / longest - repeating - subsequence / for complete code . This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; Create and initialize DP table ; initializing first row and column in dp table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match
int findLongestRepeatingSubSeq ( string str ) { int n = str . length ( ) ; int dp [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { dp [ i ] [ 0 ] = 0 ; dp [ 0 ] [ i ] = 0 ; } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ n ] ; }
Number of ways to arrange N items under given constraints | C ++ program to find number of ways to arrange items under given constraint ; method returns number of ways with which items can be arranged ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; declare dp array to store result up to ith colored item ; variable to keep track of count of items considered till now ; loop over all different colors ; populate next value using current value and stated relation ; return value stored at last index ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int waysToArrange ( int N , int K , int k [ ] ) { int C [ N + 1 ] [ N + 1 ] ; int i , j ; for ( i = 0 ; i <= N ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) ; } } int dp [ K ] ; int count = 0 ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < K ; i ++ ) { dp [ i + 1 ] = ( dp [ i ] * C [ count + k [ i ] - 1 ] [ k [ i ] - 1 ] ) ; count += k [ i ] ; } return dp [ K ] ; } int main ( ) { int N = 4 ; int k [ ] = { 2 , 2 } ; int K = sizeof ( k ) / sizeof ( int ) ; cout << waysToArrange ( N , K , k ) << endl ; return 0 ; }
Minimum cells required to reach destination with jumps equal to cell values | C ++ implementation to count minimum cells required to be covered to reach destination ; function to count minimum cells required to be covered to reach destination ; to store min cells required to be covered to reach a particular cell ; initially no cells can be reached ; base case ; building up the dp [ ] [ ] matrix ; dp [ i ] [ j ] != INT_MAX denotes that cell ( i , j ) can be reached from cell ( 0 , 0 ) and the other half of the condition finds the cell on the right that can be reached from ( i , j ) ; the other half of the condition finds the cell right below that can be reached from ( i , j ) ; it true then cell ( m - 1 , n - 1 ) can be reached from cell ( 0 , 0 ) and returns the minimum number of cells covered ; cell ( m - 1 , n - 1 ) cannot be reached from cell ( 0 , 0 ) ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  100 NEW_LINE int minCells ( int mat [ SIZE ] [ SIZE ] , int m , int n ) { int dp [ m ] [ n ] ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = INT_MAX ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( dp [ i ] [ j ] != INT_MAX && ( j + mat [ i ] [ j ] ) < n && ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) dp [ i ] [ j + mat [ i ] [ j ] ] = dp [ i ] [ j ] + 1 ; if ( dp [ i ] [ j ] != INT_MAX && ( i + mat [ i ] [ j ] ) < m && ( dp [ i ] [ j ] + 1 ) < dp [ i + mat [ i ] [ j ] ] [ j ] ) dp [ i + mat [ i ] [ j ] ] [ j ] = dp [ i ] [ j ] + 1 ; } } if ( dp [ m - 1 ] [ n - 1 ] != INT_MAX ) return dp [ m - 1 ] [ n - 1 ] ; return -1 ; } int main ( ) { int mat [ SIZE ] [ SIZE ] = { { 2 , 3 , 2 , 1 , 4 } , { 3 , 2 , 5 , 8 , 2 } , { 1 , 1 , 2 , 2 , 1 } } ; int m = 3 , n = 5 ; cout << " Minimum ▁ number ▁ of ▁ cells ▁ = ▁ " << minCells ( mat , m , n ) ; return 0 ; }
Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays | CPP to find largest bitonic sequence such that ; utility Binary search ; function to find LIS in reverse form ; int len = 1 ; it will always point to empty location ; new smallest value ; arr [ i ] wants to extend largest subsequence ; arr [ i ] wants to be a potential candidate of future subsequence It will replace ceil value in tailIndices ; put LIS into vector ; function for finding longest bitonic seq ; find LIS of array 1 in reverse form ; reverse res to get LIS of first array ; reverse array2 and find its LIS ; print result ; driver preogram
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > res ; int GetCeilIndex ( int arr [ ] , vector < int > & T , int l , int r , int key ) { while ( r - l > 1 ) { int m = l + ( r - l ) / 2 ; if ( arr [ T [ m ] ] >= key ) r = m ; else l = m ; } return r ; } void LIS ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ tailIndices [ 0 ] ] ) tailIndices [ 0 ] = i ; else if ( arr [ i ] > arr [ tailIndices [ len - 1 ] ] ) { prevIndices [ i ] = tailIndices [ len - 1 ] ; tailIndices [ len ++ ] = i ; } else { int pos = GetCeilIndex ( arr , tailIndices , -1 , len - 1 , arr [ i ] ) ; prevIndices [ i ] = tailIndices [ pos - 1 ] ; tailIndices [ pos ] = i ; } } for ( int i = tailIndices [ len - 1 ] ; i >= 0 ; i = prevIndices [ i ] ) res . push_back ( arr [ i ] ) ; } void longestBitonic ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 ) { LIS ( arr1 , n1 ) ; reverse ( res . begin ( ) , res . end ( ) ) ; reverse ( arr2 , arr2 + n2 ) ; LIS ( arr2 , n2 ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) cout << res [ i ] << " ▁ " ; } int main ( ) { int arr1 [ ] = { 1 , 2 , 4 , 3 , 2 } ; int arr2 [ ] = { 8 , 6 , 4 , 7 , 8 , 9 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; longestBitonic ( arr1 , n1 , arr2 , n2 ) ; return 0 ; }