codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of deletions to make a string palindrome | Set 2 | CPP program to find minimum deletions to make palindrome . ; Find reverse of input string ; Create a DP table for storing edit distance of string and reverse . ; Find edit distance between input and revInput considering only delete operation . ; Go from bottom left to top right and find the minimum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getLevenstein ( string const & input ) { string revInput ( input . rbegin ( ) , input . rend ( ) ) ; int n = input . size ( ) ; vector < vector < int > > dp ( n + 1 , vector < int > ( n + 1 , -1 ) ) ; for ( int i = 0 ; i <= n ; ++ i ) { dp [ 0 ] [ i ] = i ; dp [ i ] [ 0 ] = i ; } for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j <= n ; ++ j ) { if ( input [ i - 1 ] == revInput [ j - 1 ] ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + min ( { dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] } ) ; } } int res = numeric_limits < int > :: max ( ) ; for ( int i = n , j = 0 ; i >= 0 ; -- i , ++ j ) { res = min ( res , dp [ i ] [ j ] ) ; if ( i < n ) res = min ( res , dp [ i + 1 ] [ j ] ) ; if ( i > 0 ) res = min ( res , dp [ i - 1 ] [ j ] ) ; } return res ; } int main ( ) { string input ( " myfirstgeekarticle " ) ; cout << getLevenstein ( input ) ; return 0 ; }
Number of ways to form a heap with n distinct integers | CPP program to count max heaps with n distinct keys ; dp [ i ] = number of max heaps for i distinct integers ; nck [ i ] [ j ] = number of ways to choose j elements form i elements , no order ; log2 [ i ] = floor of logarithm of base 2 of i ; to calculate nCk ; calculate l for give value of n ; number of elements that are actually present in last level ( hth level ) ( 2 ^ h - 1 ) ; if more than half - filled ; return ( 1 << h ) - 1 ; ( 2 ^ h ) - 1 ; find maximum number of heaps for n ; function to initialize arrays ; for each power of two find logarithm ; driver function
#include <iostream> NEW_LINE using namespace std ; int dp [ MAXN ] ; int nck [ MAXN ] [ MAXN ] ; int log2 [ MAXN ] ; int choose ( int n , int k ) { if ( k > n ) return 0 ; if ( n <= 1 ) return 1 ; if ( k == 0 ) return 1 ; if ( nck [ n ] [ k ] != -1 ) return nck [ n ] [ k ] ; int answer = choose ( n - 1 , k - 1 ) + choose ( n - 1 , k ) ; nck [ n ] [ k ] = answer ; return answer ; } int getLeft ( int n ) { if ( n == 1 ) return 0 ; int h = log2 [ n ] ; int last = n - ( ( 1 << h ) - 1 ) ; if ( last >= ( numh / 2 ) ) else return ( 1 << h ) - 1 - ( ( numh / 2 ) - last ) ; } int numberOfHeaps ( int n ) { if ( n <= 1 ) return 1 ; if ( dp [ n ] != -1 ) return dp [ n ] ; int left = getLeft ( n ) ; int ans = ( choose ( n - 1 , left ) * numberOfHeaps ( left ) ) * ( numberOfHeaps ( n - 1 - left ) ) ; dp [ n ] = ans ; return ans ; } int solve ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = -1 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) nck [ i ] [ j ] = -1 ; int currLog2 = -1 ; int currPower2 = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( currPower2 == i ) { currLog2 ++ ; currPower2 *= 2 ; } log2 [ i ] = currLog2 ; } return numberOfHeaps ( n ) ; } int main ( ) { int n = 10 ; cout << solve ( n ) << endl ; return 0 ; }
Hosoya 's Triangle | CPP Program to print Hosoya 's triangle of height n. ; Print the Hosoya triangle of height n . ; base case . ; For each row . ; for each column ; ; recursive steps . ; printing the solution ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define N  5 NEW_LINE using namespace std ; void printHosoya ( int n ) { int dp [ N ] [ N ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = dp [ 1 ] [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + dp [ i - 2 ] [ j - 2 ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) cout << dp [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int n = 5 ; printHosoya ( n ) ; return 0 ; }
Different ways to sum n using numbers greater than or equal to m | CPP Program to find number of ways to which numbers that are greater than given number can be added to get sum . ; Return number of ways to which numbers that are greater than given number can be added to get sum . ; Filling the table . k is for numbers greater than or equal that are allowed . ; i is for sum ; initializing dp [ i ] [ k ] to number ways to get sum using numbers greater than or equal k + 1 ; if i > k ; Driver Program
#include <bits/stdc++.h> NEW_LINE #define MAX  100 NEW_LINE using namespace std ; int numberofways ( int n , int m ) { int dp [ n + 2 ] [ n + 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ n + 1 ] = 1 ; for ( int k = n ; k >= m ; k -- ) { for ( int i = 0 ; i <= n ; i ++ ) { dp [ i ] [ k ] = dp [ i ] [ k + 1 ] ; if ( i - k >= 0 ) dp [ i ] [ k ] = ( dp [ i ] [ k ] + dp [ i - k ] [ k ] ) ; } } return dp [ n ] [ m ] ; } int main ( ) { int n = 3 , m = 1 ; cout << numberofways ( n , m ) << endl ; return 0 ; }
Entringer Number | CPP Program to find Entringer Number E ( n , k ) ; Return Entringer Number E ( n , k ) ; Base Case ; Base Case ; Recursive step ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int zigzag ( int n , int k ) { if ( n == 0 && k == 0 ) return 1 ; if ( k == 0 ) return 0 ; return zigzag ( n , k - 1 ) + zigzag ( n - 1 , n - k ) ; } int main ( ) { int n = 4 , k = 3 ; cout << zigzag ( n , k ) << endl ; return 0 ; }
Eulerian Number | CPP Program to find Eulerian number A ( n , m ) ; Return euleriannumber A ( n , m ) ; For each row from 1 to n ; For each column from 0 to m ; If i is greater than j ; If j is 0 , then make that state as 1. ; basic recurrence relation . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int eulerian ( int n , int m ) { int dp [ n + 1 ] [ m + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i > j ) { if ( j == 0 ) dp [ i ] [ j ] = 1 ; else dp [ i ] [ j ] = ( ( i - j ) * dp [ i - 1 ] [ j - 1 ] ) + ( ( j + 1 ) * dp [ i - 1 ] [ j ] ) ; } } } return dp [ n ] [ m ] ; } int main ( ) { int n = 3 , m = 1 ; cout << eulerian ( n , m ) << endl ; return 0 ; }
Delannoy Number | CPP Program of finding nth Delannoy Number . ; Return the nth Delannoy Number . ; Base case ; Recursive step . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dealnnoy ( int n , int m ) { if ( m == 0 n == 0 ) return 1 ; return dealnnoy ( m - 1 , n ) + dealnnoy ( m - 1 , n - 1 ) + dealnnoy ( m , n - 1 ) ; } int main ( ) { int n = 3 , m = 4 ; cout << dealnnoy ( n , m ) << endl ; return 0 ; }
Delannoy Number | CPP Program of finding nth Delannoy Number . ; Return the nth Delannoy Number . ; Base cases ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dealnnoy ( int n , int m ) { int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - 1 ] + dp [ i ] [ j - 1 ] ; return dp [ m ] [ n ] ; } int main ( ) { int n = 3 , m = 4 ; cout << dealnnoy ( n , m ) << endl ; return 0 ; }
Longest alternating ( positive and negative ) subarray starting at every index | CPP program to find longest alternating subarray starting from every index . ; Fill count [ ] from end . ; Print result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void longestAlternating ( int arr [ ] , int n ) { int count [ n ] ; count [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ; else count [ i ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) cout << count [ i ] << " ▁ " ; } int main ( ) { int a [ ] = { -5 , -1 , -1 , 2 , -2 , -3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; longestAlternating ( a , n ) ; return 0 ; }
Maximum value with the choice of either dividing or considering as it is | CPP program for maximize result when we have choice to divide or consider as it is . ; function for calculating max possible result ; Compute remaining values in bottom up manner . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDP ( int n ) { int res [ n + 1 ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res [ i ] = max ( i , ( res [ i / 2 ] + res [ i / 3 ] + res [ i / 4 ] + res [ i / 5 ] ) ) ; return res [ n ] ; } int main ( ) { int n = 60 ; cout << " MaxSum ▁ = " << maxDP ( n ) ; return 0 ; }
Maximum difference of zeros and ones in binary string | CPP Program to find the length of substring with maximum difference of zeroes and ones in binary string . ; Return true if there all 1 s ; Checking each index is 0 or not . ; Find the length of substring with maximum difference of zeroes and ones in binary string ; If string is over . ; If the state is already calculated . ; Returns length of substring which is having maximum difference of number of 0 s and number of 1 s ; If all 1 s return - 1. ; Else find the length . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  100 NEW_LINE using namespace std ; bool allones ( string s , int n ) { int co = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) co += ( s [ i ] == '1' ) ; return ( co == n ) ; } int findlength ( int arr [ ] , string s , int n , int ind , int st , int dp [ ] [ 3 ] ) { if ( ind >= n ) return 0 ; if ( dp [ ind ] [ st ] != -1 ) return dp [ ind ] [ st ] ; if ( st == 0 ) return dp [ ind ] [ st ] = max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , findlength ( arr , s , n , ind + 1 , 0 , dp ) ) ; else return dp [ ind ] [ st ] = max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , 0 ) ; } int maxLen ( string s , int n ) { if ( allones ( s , n ) ) return -1 ; int arr [ MAX ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = ( s [ i ] == '0' ? 1 : -1 ) ; int dp [ MAX ] [ 3 ] ; memset ( dp , -1 , sizeof dp ) ; return findlength ( arr , s , n , 0 , 0 , dp ) ; } int main ( ) { string s = "11000010001" ; int n = 11 ; cout << maxLen ( s , n ) << endl ; return 0 ; }
Minimum jumps to reach last building in a matrix | Recursive CPP program to find minimum jumps to reach last building from first . ; Returns minimum jump path from ( 0 , 0 ) to ( m , n ) in height [ R ] [ C ] ; base case ; Find minimum jumps if we go through diagonal ; Find minimum jumps if we go through down ; Find minimum jumps if we go through right ; return minimum jumps ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; # define R  4 NEW_LINE # define C  3 NEW_LINE bool isSafe ( int x , int y ) { return ( x < R && y < C ) ; } int minJump ( int height [ R ] [ C ] , int x , int y ) { if ( x == R - 1 && y == C - 1 ) return 0 ; int diag = INT_MAX ; if ( isSafe ( x + 1 , y + 1 ) ) diag = minJump ( height , x + 1 , y + 1 ) + abs ( height [ x ] [ y ] - height [ x + 1 ] [ y + 1 ] ) ; int down = INT_MAX ; if ( isSafe ( x + 1 , y ) ) down = minJump ( height , x + 1 , y ) + abs ( height [ x ] [ y ] - height [ x + 1 ] [ y ] ) ; int right = INT_MAX ; if ( isSafe ( x , y + 1 ) ) right = minJump ( height , x , y + 1 ) + abs ( height [ x ] [ y ] - height [ x ] [ y + 1 ] ) ; return min ( { down , right , diag } ) ; } int main ( ) { int height [ ] [ C ] = { { 5 , 4 , 2 } , { 9 , 2 , 1 } , { 2 , 5 , 9 } , { 1 , 3 , 11 } } ; cout << minJump ( height , 0 , 0 ) ; return 0 ; }
Maximum sum subsequence with at | CPP program to find maximum sum subsequence such that elements are at least k distance away . ; MS [ i ] is going to store maximum sum subsequence in subarray from arr [ i ] to arr [ n - 1 ] ; We fill MS from right to left . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int arr [ ] , int N , int k ) { int MS [ N ] ; MS [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( i + k + 1 >= N ) MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] ) ; else MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ; } return MS [ 0 ] ; } int main ( ) { int N = 10 , k = 2 ; int arr [ ] = { 50 , 70 , 40 , 50 , 90 , 70 , 60 , 40 , 70 , 50 } ; cout << maxSum ( arr , N , k ) ; return 0 ; }
Length of Longest Balanced Subsequence | C ++ program to find length of the longest balanced subsequence ; Variable to count all the open brace that does not have the corresponding closing brace . ; To count all the close brace that does not have the corresponding open brace . ; Iterating over the String ; Number of open braces that hasn 't been closed yet. ; Number of close braces that cannot be mapped to any open brace . ; Mapping the ith close brace to one of the open brace . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( char s [ ] , int n ) { int invalidOpenBraces = 0 ; int invalidCloseBraces = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ( ' ) { invalidOpenBraces ++ ; } else { if ( invalidOpenBraces == 0 ) { invalidCloseBraces ++ ; } else { invalidOpenBraces -- ; } } } return ( n - ( invalidOpenBraces + invalidCloseBraces ) ) ; } int main ( ) { char s [ ] = " ( ) ( ( ( ( ( ( ) " ; int n = strlen ( s ) ; cout << maxLength ( s , n ) << endl ; return 0 ; }
Longest alternating sub | C ++ program to calculate longest alternating sub - array for each index elements ; Function to calculate alternating sub - array for each index of array elements ; Initialize the base state of len [ ] ; Calculating value for each element ; If both elements are different then add 1 to next len [ i + 1 ] ; else initialize to 1 ; Print lengths of binary subarrays . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void alternateSubarray ( bool arr [ ] , int n ) { int len [ n ] ; len [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( arr [ i ] ^ arr [ i + 1 ] == 1 ) len [ i ] = len [ i + 1 ] + 1 ; else len [ i ] = 1 ; } for ( int i = 0 ; i < n ; ++ i ) cout << len [ i ] << " ▁ " ; } int main ( ) { bool arr [ ] = { 1 , 0 , 1 , 0 , 0 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; alternateSubarray ( arr , n ) ; return 0 ; }
Longest alternating sub | C ++ program to calculate longest alternating sub - array for each index elements ; Function to calculate alternating sub - array for each index of array elements ; Initialize count variable for storing length of sub - array ; Initialize ' prev ' variable which indicates the previous element while traversing for index ' i ' ; If both elements are same print elements because alternate element is not found for current index ; print count and decrement it . ; Increment count for next element ; Re - initialize previous variable ; If elements are still available after traversing whole array , print it ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void alternateSubarray ( bool arr [ ] , int n ) { int count = 1 ; int prev = arr [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) { if ( ( arr [ i ] ^ prev ) == 0 ) { while ( count ) cout << count -- << " ▁ " ; } ++ count ; prev = arr [ i ] ; } while ( count ) cout << count -- << " ▁ " ; } int main ( ) { bool arr [ ] = { 1 , 0 , 1 , 0 , 0 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; alternateSubarray ( arr , n ) ; return 0 ; }
Longest Common Subsequence with at most k changes allowed | CPP program to find LCS of two arrays with k changes allowed in first array . ; Return LCS with at most k changes allowed . ; If at most changes is less than 0. ; If any of two array is over . ; Making a reference variable to dp [ n ] [ m ] [ k ] ; If value is already calculated , return that value . ; calculating LCS with no changes made . ; calculating LCS when array element are same . ; calculating LCS with changes made . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10 NEW_LINE int lcs ( int dp [ MAX ] [ MAX ] [ MAX ] , int arr1 [ ] , int n , int arr2 [ ] , int m , int k ) { if ( k < 0 ) return -1e7 ; if ( n < 0 m < 0 ) return 0 ; int & ans = dp [ n ] [ m ] [ k ] ; if ( ans != -1 ) return ans ; ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , m - 1 , k ) ) ; if ( arr1 [ n - 1 ] == arr2 [ m - 1 ] ) ans = max ( ans , 1 + lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k ) ) ; ans = max ( ans , 1 + lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k - 1 ) ) ; return ans ; } int main ( ) { int k = 1 ; int arr1 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int arr2 [ ] = { 5 , 3 , 1 , 4 , 2 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int m = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; int dp [ MAX ] [ MAX ] [ MAX ] ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << lcs ( dp , arr1 , n , arr2 , m , k ) << endl ; return 0 ; }
Count ways to build street under given constraints | C ++ program to count ways to build street under given constraints ; function to count ways of building a street of n rows ; base case ; ways of building houses in both the spots of ith row ; ways of building an office in one of the two spots of ith row ; total ways for n rows ; driver program for checking above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; long countWays ( int n ) { long dp [ 2 ] [ n + 1 ] ; dp [ 0 ] [ 1 ] = 1 ; dp [ 1 ] [ 1 ] = 2 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] ; dp [ 1 ] [ i ] = dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ; } return dp [ 0 ] [ n ] + dp [ 1 ] [ n ] ; } int main ( ) { int n = 5 ; cout << " Total ▁ no ▁ of ▁ ways ▁ with ▁ n ▁ = ▁ " << n << " ▁ are : ▁ " << countWays ( n ) << endl ; }
Count ways to build street under given constraints | C ++ program of above approach ; Program to count ways ; Iterate from 2 to n ; Driver code ; Count Ways
#include <iostream> NEW_LINE using namespace std ; int countways ( long long n ) { long long A [ n + 1 ] ; A [ 0 ] = 1 ; A [ 1 ] = 3 ; A [ 2 ] = 7 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = 2 * A [ i - 1 ] + A [ i - 2 ] ; } return A [ n ] ; } int main ( ) { int n = 5 ; cout << countways ( 5 ) << endl ; return 0 ; }
Maximum length subsequence with difference between adjacent elements as either 0 or 1 | C ++ implementation to find maximum length subsequence with difference between adjacent elements as either 0 or 1 ; function to find maximum length subsequence with difference between adjacent elements as either 0 or 1 ; Initialize mls [ ] values for all indexes ; Compute optimized maximum length subsequence values in bottom up manner ; Store maximum of all ' mls ' values in ' max ' ; required maximum length subsequence ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLenSub ( int arr [ ] , int n ) { int mls [ n ] , max = 0 ; for ( int i = 0 ; i < n ; i ++ ) mls [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ] = mls [ j ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( max < mls [ i ] ) max = mls [ i ] ; return max ; } int main ( ) { int arr [ ] = { 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Maximum ▁ length ▁ subsequence ▁ = ▁ " << maxLenSub ( arr , n ) ; return 0 ; }
Unique paths in a Grid with Obstacles | CPP program for the above approach ; If obstacle is at starting position ; Initializing starting position ; first row all are '1' until obstacle ; No ways to reach at this index ; first column all are '1' until obstacle ; No ways to reach at this index ; If current cell has no obstacle ; No ways to reach at this index ; returning the bottom right corner of Grid ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int uniquePathsWithObstacles ( vector < vector < int > > & A ) { int r = A . size ( ) ; int c = A [ 0 ] . size ( ) ; if ( A [ 0 ] [ 0 ] ) return 0 ; A [ 0 ] [ 0 ] = 1 ; for ( int j = 1 ; j < c ; j ++ ) { if ( A [ 0 ] [ j ] == 0 ) { A [ 0 ] [ j ] = A [ 0 ] [ j - 1 ] ; } else { A [ 0 ] [ j ] = 0 ; } } for ( int i = 1 ; i < r ; i ++ ) { if ( A [ i ] [ 0 ] == 0 ) { A [ i ] [ 0 ] = A [ i - 1 ] [ 0 ] ; } else { A [ i ] [ 0 ] = 0 ; } } for ( int i = 1 ; i < r ; i ++ ) { for ( int j = 1 ; j < c ; j ++ ) { if ( A [ i ] [ j ] == 0 ) { A [ i ] [ j ] = A [ i - 1 ] [ j ] + A [ i ] [ j - 1 ] ; } else { A [ i ] [ j ] = 0 ; } } } return A [ r - 1 ] ; } int main ( ) { vector < vector < int > > A = { { 0 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 0 } } ; cout << uniquePathsWithObstacles ( A ) << " STRNEWLINE " ; return 0 ; }
Coin game winner where every player has three choices | C ++ program to find winner of game if player can pick 1 , x , y coins ; To find winner of game ; To store results ; Initial values ; Computing other values . ; If A losses any of i - 1 or i - x or i - y game then he will definitely win game i ; Else A loses game . ; If dp [ n ] is true then A will game otherwise he losses ; Driver program to test findWinner ( ) ;
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findWinner ( int x , int y , int n ) { int dp [ n + 1 ] ; dp [ 0 ] = false ; dp [ 1 ] = true ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i - 1 >= 0 and ! dp [ i - 1 ] ) dp [ i ] = true ; else if ( i - x >= 0 and ! dp [ i - x ] ) dp [ i ] = true ; else if ( i - y >= 0 and ! dp [ i - y ] ) dp [ i ] = true ; else dp [ i ] = false ; } return dp [ n ] ; } int main ( ) { int x = 3 , y = 4 , n = 5 ; if ( findWinner ( x , y , n ) ) cout << ' A ' ; else cout << ' B ' ; return 0 ; }
Maximum games played by winner | C / C ++ program to find maximum number of games played by winner ; method returns maximum games a winner needs to play in N - player tournament ; for 0 games , 1 player is needed for 1 game , 2 players are required ; loop until i - th Fibonacci number is less than or equal to N ; result is ( i - 2 ) because i will be incremented one extra in while loop and we want the last value which is smaller than N , so one more decrement ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxGameByWinner ( int N ) { int dp [ N ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 2 ; int i = 2 ; do { dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ; } while ( dp [ i ++ ] <= N ) ; return ( i - 2 ) ; } int main ( ) { int N = 10 ; cout << maxGameByWinner ( N ) << endl ; return 0 ; }
Convert to Strictly increasing integer array with minimum changes | CPP program to find min elements to change so array is strictly increasing ; To find min elements to remove from array to make it strictly increasing ; Mark all elements of LIS as 1 ; Find LIS of array ; Return min changes for array to strictly increasing ; Driver program to test minRemove ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minRemove ( int arr [ ] , int n ) { int LIS [ n ] , len = 0 ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) { LIS [ i ] = max ( LIS [ i ] , LIS [ j ] + 1 ) ; } } len = max ( len , LIS [ i ] ) ; } return n - len ; } int main ( ) { int arr [ ] = { 1 , 2 , 6 , 5 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minRemove ( arr , n ) ; return 0 ; }