codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the binary matrix by filpping submatrix once | C ++ program to find maximum number of ones after one flipping in Binary Matrix ; Return number of ones in square submatrix of size k x k starting from ( x , y ) ; Return maximum number of 1 s after flipping a submatrix ; Precomputing the number of 1 s ; Finding the maximum number of 1 s after flipping ; Driver code
#include <bits/stdc++.h> NEW_LINE #define R  3 NEW_LINE #define C  3 NEW_LINE using namespace std ; int cal ( int ones [ R + 1 ] [ C + 1 ] , int x , int y , int k ) { return ones [ x + k - 1 ] [ y + k - 1 ] - ones [ x - 1 ] [ y + k - 1 ] - ones [ x + k - 1 ] [ y - 1 ] + ones [ x - 1 ] [ y - 1 ] ; } int sol ( int mat [ R ] [ C ] ) { int ans = 0 ; int ones [ R + 1 ] [ C + 1 ] = { 0 } ; for ( int i = 1 ; i <= R ; i ++ ) for ( int j = 1 ; j <= C ; j ++ ) ones [ i ] [ j ] = ones [ i - 1 ] [ j ] + ones [ i ] [ j - 1 ] - ones [ i - 1 ] [ j - 1 ] + ( mat [ i - 1 ] [ j - 1 ] == 1 ) ; for ( int k = 1 ; k <= min ( R , C ) ; k ++ ) for ( int i = 1 ; i + k - 1 <= R ; i ++ ) for ( int j = 1 ; j + k - 1 <= C ; j ++ ) ans = max ( ans , ( ones [ R ] [ C ] + k * k - 2 * cal ( ones , i , j , k ) ) ) ; return ans ; } int main ( ) { int mat [ R ] [ C ] = { { 0 , 0 , 1 } , { 0 , 0 , 1 } , { 1 , 0 , 1 } } ; cout << sol ( mat ) << endl ; return 0 ; }
Level with maximum number of nodes | C ++ implementation to find the level having maximum number of Nodes ; 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 . ; function to find the level having maximum number of Nodes ; Current level ; Maximum Nodes at same level ; Level having maximum Nodes ; Count Nodes in a level ; If it is maximum till now Update level_no to current level ; Pop complete current level ; Increment for next level ; Driver program to test above ; binary tree formation ; 2 ; / \ ; 1 3 ; / \ \ ; 4 6 8 ; / ; 5
#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 = NULL ; node -> right = NULL ; return ( node ) ; } int maxNodeLevel ( Node * root ) { if ( root == NULL ) return -1 ; queue < Node * > q ; q . push ( root ) ; int level = 0 ; int max = INT_MIN ; int level_no = 0 ; while ( 1 ) { int NodeCount = q . size ( ) ; if ( NodeCount == 0 ) break ; if ( NodeCount > max ) { max = NodeCount ; level_no = level ; } while ( NodeCount > 0 ) { Node * Node = q . front ( ) ; q . pop ( ) ; if ( Node -> left != NULL ) q . push ( Node -> left ) ; if ( Node -> right != NULL ) q . push ( Node -> right ) ; NodeCount -- ; } level ++ ; } return level_no ; } int main ( ) { struct Node * root = newNode ( 2 ) ; root -> left = newNode ( 1 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 6 ) ; root -> right -> right = newNode ( 8 ) ; root -> left -> right -> left = newNode ( 5 ) ; printf ( " Level ▁ having ▁ maximum ▁ number ▁ of ▁ Nodes ▁ : ▁ % d " , maxNodeLevel ( root ) ) ; return 0 ; }
Minimum steps to minimize n as per given condition |  ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinSteps ( int n ) { int table [ n + 1 ] ; table [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( ! ( i % 2 ) && ( i % 3 ) ) table [ i ] = 1 + min ( table [ i - 1 ] , table [ i / 2 ] ) ; else if ( ! ( i % 3 ) && ( i % 2 ) ) table [ i ] = 1 + min ( table [ i - 1 ] , table [ i / 3 ] ) ; else if ( ! ( i % 2 ) && ! ( i % 3 ) ) table [ i ] = 1 + min ( table [ i - 1 ] , min ( table [ i / 2 ] , table [ i / 3 ] ) ) ; else table [ i ] = 1 + table [ i - 1 ] ; } return table [ n ] ; } int main ( ) { int n = 14 ; cout << getMinSteps ( n ) ; return 0 ; }
How to solve a Dynamic Programming Problem ? | Returns the number of arrangements to form ' n ' ; base case
int solve ( int n ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; return solve ( n - 1 ) + solve ( n - 3 ) + solve ( n - 5 ) ; }
Maximum points collected by two persons allowed to meet once | C ++ program to find maximum points that can be collected by two persons in a matrix . ; To store points collected by Person P1 when he / she begins journy from start and from end . ; To store points collected by Person P2 when he / she begins journey from start and from end . ; Table for P1 's journey from  start to meeting cell ; Table for P1 's journey from  end to meet cell ; Table for P2 's journey from start to meeting cell ; Table for P2 's journey from end to meeting cell ; Now iterate over all meeting positions ( i , j ) ; Driver code ; input the calories burnt matrix
#include <bits/stdc++.h> NEW_LINE #define M  3 NEW_LINE #define N  3 NEW_LINE using namespace std ; int findMaxPoints ( int A [ ] [ M ] ) { int P1S [ M + 1 ] [ N + 1 ] , P1E [ M + 1 ] [ N + 1 ] ; memset ( P1S , 0 , sizeof ( P1S ) ) ; memset ( P1E , 0 , sizeof ( P1E ) ) ; int P2S [ M + 1 ] [ N + 1 ] , P2E [ M + 1 ] [ N + 1 ] ; memset ( P2S , 0 , sizeof ( P2S ) ) ; memset ( P2E , 0 , sizeof ( P2E ) ) ; for ( int i = 1 ; i <= N ; i ++ ) for ( int j = 1 ; j <= M ; j ++ ) P1S [ i ] [ j ] = max ( P1S [ i - 1 ] [ j ] , P1S [ i ] [ j - 1 ] ) + A [ i - 1 ] [ j - 1 ] ; for ( int i = N ; i >= 1 ; i -- ) for ( int j = M ; j >= 1 ; j -- ) P1E [ i ] [ j ] = max ( P1E [ i + 1 ] [ j ] , P1E [ i ] [ j + 1 ] ) + A [ i - 1 ] [ j - 1 ] ; for ( int i = N ; i >= 1 ; i -- ) for ( int j = 1 ; j <= M ; j ++ ) P2S [ i ] [ j ] = max ( P2S [ i + 1 ] [ j ] , P2S [ i ] [ j - 1 ] ) + A [ i - 1 ] [ j - 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) for ( int j = M ; j >= 1 ; j -- ) P2E [ i ] [ j ] = max ( P2E [ i - 1 ] [ j ] , P2E [ i ] [ j + 1 ] ) + A [ i - 1 ] [ j - 1 ] ; int ans = 0 ; for ( int i = 2 ; i < N ; i ++ ) { for ( int j = 2 ; j < M ; j ++ ) { int op1 = P1S [ i ] [ j - 1 ] + P1E [ i ] [ j + 1 ] + P2S [ i + 1 ] [ j ] + P2E [ i - 1 ] [ j ] ; int op2 = P1S [ i - 1 ] [ j ] + P1E [ i + 1 ] [ j ] + P2S [ i ] [ j - 1 ] + P2E [ i ] [ j + 1 ] ; ans = max ( ans , max ( op1 , op2 ) ) ; } } return ans ; } int main ( ) { int A [ ] [ M ] = { { 100 , 100 , 100 } , { 100 , 1 , 100 } , { 100 , 100 , 100 } } ; cout << " Max ▁ Points ▁ : ▁ " << findMaxPoints ( A ) ; return 0 ; }
Longest subsequence such that difference between adjacents is one | C ++ program to find the longest subsequence such the difference between adjacent elements of the subsequence is one . ; Function to find the length of longest subsequence ; Initialize the dp [ ] array with 1 as a single element will be of 1 length ; Start traversing the given array ; Compare with all the previous elements ; If the element is consecutive then consider this subsequence and update dp [ i ] if required . ; Longest length will be the maximum value of dp array . ; Driver code ; Longest subsequence with one difference is { 1 , 2 , 3 , 4 , 3 , 2 }
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubseqWithDiffOne ( int arr [ ] , int n ) { int dp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ] = max ( dp [ i ] , dp [ j ] + 1 ) ; } } int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( result < dp [ i ] ) result = dp [ i ] ; return result ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestSubseqWithDiffOne ( arr , n ) ; return 0 ; }
Non | C ++ program to count number of ways to connect n ( where nis even ) points on a circle such that no two connecting lines cross each other and every point is connected with one other point . ; A dynamic programming based function to find nth Catalan number ; Table to store results of subproblems ; Initialize first two values in table ; Fill entries in catalan [ ] using recursive formula ; Return last entry ; Returns count of ways to connect n points on a circle such that no two connecting lines cross each other and every point is connected with one other point . ; Throw error if n is odd ; Else return n / 2 'th Catalan number ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; unsigned long int catalanDP ( unsigned int n ) { unsigned long int catalan [ n + 1 ] ; catalan [ 0 ] = catalan [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { catalan [ i ] = 0 ; for ( int j = 0 ; j < i ; j ++ ) catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ] ; } return catalan [ n ] ; } unsigned long int countWays ( unsigned long int n ) { if ( n & 1 ) { cout << " Invalid " ; return 0 ; } return catalanDP ( n / 2 ) ; } int main ( ) { cout << countWays ( 6 ) << " ▁ " ; return 0 ; }
Ways to arrange Balls such that adjacent balls are of different types | C ++ program to count number of ways to arrange three types of balls such that no two balls of same color are adjacent to each other ; table to store to store results of subproblems ; Returns count of arrangements where last placed ball is ' last ' . ' last ' is 0 for ' p ' , 1 for ' q ' and 2 for ' r ' ; if number of balls of any color becomes less than 0 the number of ways arrangements is 0. ; If last ball required is of type P and the number of balls of P type is 1 while number of balls of other color is 0 the number of ways is 1. ; Same case as above for ' q ' and ' r ' ; If this subproblem is already evaluated ; if last ball required is P and the number of ways is the sum of number of ways to form sequence with ' p - 1' P balls , q Q Balls and r R balls ending with Q and R . ; Same as above case for ' q ' and ' r ' ; else ( last == 2 ) ; Returns count of required arrangements ; Initialize ' dp ' array ; Three cases arise : return countWays ( p , q , r , 0 ) + Last required balls is type P countWays ( p , q , r , 1 ) + Last required balls is type Q countWays ( p , q , r , 2 ) ; Last required balls is type R ; Driver code to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE int dp [ MAX ] [ MAX ] [ MAX ] [ 3 ] ; int countWays ( int p , int q , int r , int last ) { if ( p < 0 q < 0 r < 0 ) return 0 ; if ( p == 1 && q == 0 && r == 0 && last == 0 ) return 1 ; if ( p == 0 && q == 1 && r == 0 && last == 1 ) return 1 ; if ( p == 0 && q == 0 && r == 1 && last == 2 ) return 1 ; if ( dp [ p ] [ q ] [ r ] [ last ] != -1 ) return dp [ p ] [ q ] [ r ] [ last ] ; if ( last == 0 ) dp [ p ] [ q ] [ r ] [ last ] = countWays ( p - 1 , q , r , 1 ) + countWays ( p - 1 , q , r , 2 ) ; else if ( last == 1 ) dp [ p ] [ q ] [ r ] [ last ] = countWays ( p , q - 1 , r , 0 ) + countWays ( p , q - 1 , r , 2 ) ; dp [ p ] [ q ] [ r ] [ last ] = countWays ( p , q , r - 1 , 0 ) + countWays ( p , q , r - 1 , 1 ) ; return dp [ p ] [ q ] [ r ] [ last ] ; } int countUtil ( int p , int q , int r ) { memset ( dp , -1 , sizeof ( dp ) ) ; } int main ( ) { int p = 1 , q = 1 , r = 1 ; printf ( " % d " , countUtil ( p , q , r ) ) ; return 0 ; }
Count Derangements ( Permutation such that no element appears in its original position ) | A Naive Recursive C ++ program to count derangements ; Function to count derangements ; Base cases ; countDer ( n ) = ( n - 1 ) [ countDer ( n - 1 ) + der ( n - 2 ) ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDer ( int n ) { if ( n == 1 ) return 0 ; if ( n == 2 ) return 1 ; return ( n - 1 ) * ( countDer ( n - 1 ) + countDer ( n - 2 ) ) ; } int main ( ) { int n = 4 ; cout << " Count ▁ of ▁ Derangements ▁ is ▁ " << countDer ( n ) ; return 0 ; }
Count Derangements ( Permutation such that no element appears in its original position ) | A Dynamic programming based C ++ program to count derangements ; Function to count derangements ; Create an array to store counts for subproblems ; Base cases ; Fill der [ 0. . n ] in bottom up manner using above recursive formula ; Return result for n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDer ( int n ) { int der [ n + 1 ] = { 0 } ; der [ 1 ] = 0 ; der [ 2 ] = 1 ; for ( int i = 3 ; i <= n ; ++ i ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) ; return der [ n ] ; } int main ( ) { int n = 4 ; cout << " Count ▁ of ▁ Derangements ▁ is ▁ " << countDer ( n ) ; return 0 ; }
Find number of solutions of a linear equation of n variables | A Dynamic programming based C ++ program to find number of non - negative solutions for a given linear equation ; Returns count of solutions for given rhs and coefficients coeff [ 0. . n - 1 ] ; Create and initialize a table to store results of subproblems ; Fill table in bottom up manner ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSol ( int coeff [ ] , int n , int rhs ) { int dp [ rhs + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = coeff [ i ] ; j <= rhs ; j ++ ) dp [ j ] += dp [ j - coeff [ i ] ] ; return dp [ rhs ] ; } int main ( ) { int coeff [ ] = { 2 , 2 , 5 } ; int rhs = 4 ; int n = sizeof ( coeff ) / sizeof ( coeff [ 0 ] ) ; cout << countSol ( coeff , n , rhs ) ; return 0 ; }
Find all distinct palindromic sub | C ++ program to find all distinct palindrome sub - strings of a given string ; Function to print all distinct palindrome sub - strings of s ; table for storing results ( 2 rows for odd - and even - length palindromes ; Find all sub - string palindromes from the given input string insert ' guards ' to iterate easily over s ; int rp = 0 ; length of ' palindrome ▁ radius ' ; Attempt to expand palindrome centered at i ; Assigning the found palindromic length to odd / even length array ; remove ' guards ' ; Put all obtained palindromes in a hash map to find only distinct palindromess ; printing all distinct palindromes from hash map ; Driver program
#include <iostream> NEW_LINE #include <map> NEW_LINE using namespace std ; void palindromeSubStrs ( string s ) { map < string , int > m ; int n = s . size ( ) ; int R [ 2 ] [ n + 1 ] ; s = " @ " + s + " # " ; for ( int j = 0 ; j <= 1 ; j ++ ) { R [ j ] [ 0 ] = 0 ; int i = 1 ; while ( i <= n ) { while ( s [ i - rp - 1 ] == s [ i + j + rp ] ) R [ j ] [ i ] = rp ; int k = 1 ; while ( ( R [ j ] [ i - k ] != rp - k ) && ( k < rp ) ) { R [ j ] [ i + k ] = min ( R [ j ] [ i - k ] , rp - k ) ; k ++ ; } rp = max ( rp - k , 0 ) ; i += k ; } } s = s . substr ( 1 , n ) ; m [ string ( 1 , s [ 0 ] ) ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 1 ; j ++ ) for ( int rp = R [ j ] [ i ] ; rp > 0 ; rp -- ) m [ s . substr ( i - rp - 1 , 2 * rp + j ) ] = 1 ; m [ string ( 1 , s [ i ] ) ] = 1 ; } cout << " Below ▁ are ▁ " << m . size ( ) - 1 << " ▁ palindrome ▁ sub - strings " ; map < string , int > :: iterator ii ; for ( ii = m . begin ( ) ; ii != m . end ( ) ; ++ ii ) cout << ( * ii ) . first << endl ; } int main ( ) { palindromeSubStrs ( " abaaa " ) ; return 0 ; }
Largest value in each level of Binary Tree | Set | C ++ implementation to print largest value in each level of Binary Tree ; structure of a node of binary tree ; function to get a new node ; allocate space ; put in the data ; function to print largest value in each level of Binary Tree ; if tree is empty ; push root to the queue ' q ' ; node count for the current level ; if true then all the nodes of the tree have been traversed ; maximum element for the current level ; get the front element from ' q ' ; remove front element from ' q ' ; if true , then update ' max ' ; if left child exists ; if right child exists ; print maximum element of current level ; Driver code ; Construct a Binary Tree 4 / \ 9 2 / \ \ 3 5 7 ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void largestValueInEachLevel ( Node * root ) { if ( ! root ) return ; queue < Node * > q ; int nc , max ; q . push ( root ) ; while ( 1 ) { nc = q . size ( ) ; if ( nc == 0 ) break ; max = INT_MIN ; while ( nc -- ) { Node * front = q . front ( ) ; q . pop ( ) ; if ( max < front -> data ) max = front -> data ; if ( front -> left ) q . push ( front -> left ) ; if ( front -> right ) q . push ( front -> right ) ; } cout << max << " ▁ " ; } } int main ( ) { Node * root = NULL ; root = newNode ( 4 ) ; root -> left = newNode ( 9 ) ; root -> right = newNode ( 2 ) ; root -> left -> left = newNode ( 3 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> right = newNode ( 7 ) ; largestValueInEachLevel ( root ) ; return 0 ; }
Populate Inorder Successor for all nodes | C ++ program to populate inorder traversal of all nodes ; A binary tree node ; Set next of p and all descendants of p by traversing them in reverse Inorder ; The first visited node will be the rightmost node next of the rightmost node will be NULL ; First set the next pointer in right subtree ; Set the next as previously visited node in reverse Inorder ; Change the prev for subsequent node ; Finally , set the next pointer in left subtree ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Constructed binary tree is 10 / \ 8 12 / 3 ; Populates nextRight pointer in all nodes ; Let us see the populated values ; - 1 is printed if there is no successor
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; node * next ; } ; void populateNext ( node * p ) { static node * next = NULL ; if ( p ) { populateNext ( p -> right ) ; p -> next = next ; next = p ; populateNext ( p -> left ) ; } } node * newnode ( int data ) { node * Node = new node ( ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; Node -> next = NULL ; return ( Node ) ; } int main ( ) { node * root = newnode ( 10 ) ; root -> left = newnode ( 8 ) ; root -> right = newnode ( 12 ) ; root -> left -> left = newnode ( 3 ) ; populateNext ( root ) ; node * ptr = root -> left -> left ; while ( ptr ) { cout << " Next ▁ of ▁ " << ptr -> data << " ▁ is ▁ " << ( ptr -> next ? ptr -> next -> data : -1 ) << endl ; ptr = ptr -> next ; } return 0 ; }
Maximum Product Cutting | DP |  ; The main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; Keep removing parts of size 3 while n is greater than 4 ; Keep multiplying 3 to res ; The last part multiplied by previous parts ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; int maxProd ( int n ) { if ( n == 2 n == 3 ) return ( n - 1 ) ; int res = 1 ; while ( n > 4 ) { n -= 3 ; res *= 3 ; } return ( n * res ) ; } int main ( ) { cout << " Maximum ▁ Product ▁ is ▁ " << maxProd ( 10 ) ; return 0 ; }
Dice Throw | DP | C ++ program to find number of ways to get sum ' x ' with ' n ' dice where every dice has ' m ' faces ; The main function that returns number of ways to get sum ' x ' with ' n ' dice and ' m ' with m faces . ; Create a table to store results of subproblems . One extra row and column are used for simpilicity ( Number of dice is directly used as row index and sum is directly used as column index ) . The entries in 0 th row and 0 th column are never used . ; Table entries for only one dice ; Fill rest of the entries in table using recursive relation i : number of dice , j : sum ; Return value ; Driver program to test above functions
#include <iostream> NEW_LINE #include <string.h> NEW_LINE using namespace std ; int findWays ( int m , int n , int x ) { int table [ n + 1 ] [ x + 1 ] ; for ( int j = 1 ; j <= m && j <= x ; j ++ ) table [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) for ( int j = 1 ; j <= x ; j ++ ) for ( int k = 1 ; k <= m && k < j ; k ++ ) table [ i ] [ j ] += table [ i - 1 ] [ j - k ] ; return table [ n ] [ x ] ; } int main ( ) { cout << findWays ( 4 , 2 , 1 ) << endl ; cout << findWays ( 2 , 2 , 3 ) << endl ; cout << findWays ( 6 , 3 , 8 ) << endl ; cout << findWays ( 4 , 2 , 5 ) << endl ; cout << findWays ( 4 , 3 , 5 ) << endl ; return 0 ; }
Dice Throw | DP | C ++ program The main function that returns number of ways to get sum ' x ' with ' n ' dice and ' m ' with m faces . ; * * Count ways * * @ param f * @ param d * @ param s * @ return ; Create a table to store results of subproblems . One extra row and column are used for simpilicity ( Number of dice is directly used as row index and sum is directly used as column index ) . The entries in 0 th row and 0 th column are never used . ; Table entries for no dices If you do not have any data , then the value must be 0 , so the result is 1 ; Iterate over dices ; Iterate over sum ; The result is obtained in two ways , pin the current dice and spending 1 of the value , so we have mem [ i - 1 ] [ j - 1 ] remaining combinations , to find the remaining combinations we would have to pin the values ? ? above 1 then we use mem [ i ] [ j - 1 ] to sum all combinations that pin the remaining j - 1 's. But there is a way, when "j-f-1> = 0" we would be adding  extra combinations, so we remove the combinations that only pin the extrapolated dice face and  subtract the extrapolated combinations. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long findWays ( int f , int d , int s ) { long mem [ d + 1 ] [ s + 1 ] ; memset ( mem , 0 , sizeof mem ) ; mem [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= d ; i ++ ) { for ( int j = i ; j <= s ; j ++ ) { mem [ i ] [ j ] = mem [ i ] [ j - 1 ] + mem [ i - 1 ] [ j - 1 ] ; if ( j - f - 1 >= 0 ) mem [ i ] [ j ] -= mem [ i - 1 ] [ j - f - 1 ] ; } } return mem [ d ] [ s ] ; } int main ( void ) { cout << findWays ( 4 , 2 , 1 ) << endl ; cout << findWays ( 2 , 2 , 3 ) << endl ; cout << findWays ( 6 , 3 , 8 ) << endl ; cout << findWays ( 4 , 2 , 5 ) << endl ; cout << findWays ( 4 , 3 , 5 ) << endl ; return 0 ; }
Minimum insertions to form a palindrome | DP | A Naive recursive program to find minimum number insertions needed to make a string palindrome ; Recursive function to find minimum number of insertions ; Base Cases ; Check if the first and last characters are same . On the basis of the comparison result , decide which subrpoblem ( s ) to call ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinInsertions ( char str [ ] , int l , int h ) { if ( l > h ) return INT_MAX ; if ( l == h ) return 0 ; if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ; return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( min ( findMinInsertions ( str , l , h - 1 ) , findMinInsertions ( str , l + 1 , h ) ) + 1 ) ; } int main ( ) { char str [ ] = " geeks " ; cout << findMinInsertions ( str , 0 , strlen ( str ) - 1 ) ; return 0 ; }
Longest Palindromic Subsequence | DP | C ++ program of above approach ; A utility function to get max of two integers ; Returns the length of the longest palindromic subsequence in seq ; Base Case 1 : If there is only 1 character ; Base Case 2 : If there are only 2 characters and both are same ; If the first and last characters match ; If the first and last characters do not match ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max ( int x , int y ) { return ( x > y ) ? x : y ; } int lps ( char * seq , int i , int j ) { if ( i == j ) return 1 ; if ( seq [ i ] == seq [ j ] && i + 1 == j ) return 2 ; if ( seq [ i ] == seq [ j ] ) return lps ( seq , i + 1 , j - 1 ) + 2 ; return max ( lps ( seq , i , j - 1 ) , lps ( seq , i + 1 , j ) ) ; } int main ( ) { char seq [ ] = " GEEKSFORGEEKS " ; int n = strlen ( seq ) ; cout << " The ▁ length ▁ of ▁ the ▁ LPS ▁ is ▁ " << lps ( seq , 0 , n - 1 ) ; return 0 ; }