codeparrot/xlcost-text-to-code huggingface dataset:
Maximum Product Subarray | Added negative product case | C ++ program to find maximum subarray product . ; Function to find maximum subarray product . ; As maximum product can be negative , so initialize ans with minimum integer value . ; Variable to store maximum product until current value . ; Variable to store minimum product until current value . ; Variable used during updation of maximum product and minimum product . ; If current element is positive , update maxval . Update minval if it is negative . ; If current element is zero , maximum product cannot end at current element . Update minval with 1 and maxval with 0. maxval is updated to 0 as in case all other elements are negative , then maxval is 0. ; If current element is negative , then new value of maxval is previous minval * arr [ i ] and new value of minval is previous maxval * arr [ i ] . Before updating maxval , store its previous value in prevMax to be used to update minval . ; Update ans if necessary . ; If maxval is zero , then to calculate product for next iteration , it should be set to 1 as maximum product subarray does not include 0. The minimum possible value to be considered in maximum product subarray is already stored in minval , so when maxval is negative it is set to 1. ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxProduct ( int arr [ ] , int n ) { int i ; int ans = INT_MIN ; int maxval = 1 ; int minval = 1 ; int prevMax ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { maxval = maxval * arr [ i ] ; minval = min ( 1 , minval * arr [ i ] ) ; } else if ( arr [ i ] == 0 ) { minval = 1 ; maxval = 0 ; } else if ( arr [ i ] < 0 ) { prevMax = maxval ; maxval = minval * arr [ i ] ; minval = prevMax * arr [ i ] ; } ans = max ( ans , maxval ) ; if ( maxval <= 0 ) { maxval = 1 ; } } return ans ; } int main ( ) { int arr [ ] = { 0 , -4 , 0 , -2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxProduct ( arr , n ) ; return 0 ; }
Maximum and Minimum Values of an Algebraic Expression | CPP program to find the maximum and minimum values of an Algebraic expression of given form ; Finding sum of array elements ; shifting the integers by 50 so that they become positive ; dp [ i ] [ j ] represents true if sum j can be reachable by choosing i numbers ; if dp [ i ] [ j ] is true , that means it is possible to select i numbers from ( n + m ) numbers to sum upto j ; k can be at max n because the left expression has n numbers ; checking if a particular sum can be reachable by choosing n numbers ; getting the actual sum as we shifted the numbers by / 50 to avoid negative indexing in array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define INF  1e9 NEW_LINE #define MAX  50 NEW_LINE int minMaxValues ( int arr [ ] , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { sum += arr [ i ] ; arr [ i ] += 50 ; } bool dp [ MAX + 1 ] [ MAX * MAX + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { for ( int k = min ( n , i + 1 ) ; k >= 1 ; k -- ) { for ( int j = 0 ; j < MAX * MAX + 1 ; j ++ ) { if ( dp [ k - 1 ] [ j ] ) dp [ k ] [ j + arr [ i ] ] = 1 ; } } } int max_value = - INF , min_value = INF ; for ( int i = 0 ; i < MAX * MAX + 1 ; i ++ ) { if ( dp [ n ] [ i ] ) { int temp = i - 50 * n ; max_value = max ( max_value , temp * ( sum - temp ) ) ; min_value = min ( min_value , temp * ( sum - temp ) ) ; } } cout << " Maximum ▁ Value : ▁ " << max_value << " STRNEWLINE " << " Minimum ▁ Value : ▁ " << min_value << endl ; } int main ( ) { int n = 2 , m = 2 ; int arr [ ] = { 1 , 2 , 3 , 4 } ; minMaxValues ( arr , n , m ) ; return 0 ; }
Check if any valid sequence is divisible by M | C ++ program to check if any valid sequence is divisible by M ; Base case ; check if sum is divisible by M ; check if the current state is already computed ; 1. Try placing ' + ' ; 2. Try placing ' - ' ; calculate value of res for recursive case ; store the value for res for current states and return for parent call ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; bool isPossible ( int n , int index , int sum , int M , int arr [ ] , int dp [ ] [ MAX ] ) { if ( index == n ) { if ( ( sum % M ) == 0 ) return true ; return false ; } if ( dp [ index ] [ sum ] != -1 ) return dp [ index ] [ sum ] ; bool placeAdd = isPossible ( n , index + 1 , sum + arr [ index ] , M , arr , dp ) ; bool placeMinus = isPossible ( n , index + 1 , sum - arr [ index ] , M , arr , dp ) ; bool res = ( placeAdd placeMinus ) ; dp [ index ] [ sum ] = res ; return res ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = 4 ; int dp [ n + 1 ] [ MAX ] ; memset ( dp , -1 , sizeof ( dp ) ) ; bool res ; res = isPossible ( n , 0 , 0 , M , arr , dp ) ; cout << ( res ? " True " : " False " ) << endl ; return 0 ; }
Dynamic Programming on Trees | Set 2 | C ++ code to find the maximum path length considering any node as root ; function to pre - calculate the array in [ ] which stores the maximum height when travelled via branches ; initially every node has 0 height ; traverse in the subtree of u ; if child is same as parent ; dfs called ; recursively calculate the max height ; function to pre - calculate the array ouut [ ] which stores the maximum height when traveled via parent ; stores the longest and second longest branches ; traverse in the subtress of u ; compare and store the longest and second longest ; traverse in the subtree of u ; if longest branch has the node , then consider the second longest branch ; recursively calculate out [ i ] ; dfs function call ; function to print all the maximum heights from every node ; traversal to calculate in [ ] array ; traversal to calculate out [ ] array ; print all maximum heights ; Driver Code ; initialize the tree given in the diagram ; function to print the maximum height from every node
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_NODES = 100 ; int in [ MAX_NODES ] ; int out [ MAX_NODES ] ; void dfs1 ( vector < int > v [ ] , int u , int parent ) { in [ u ] = 0 ; for ( int child : v [ u ] ) { if ( child == parent ) continue ; dfs1 ( v , child , u ) ; in [ u ] = max ( in [ u ] , 1 + in [ child ] ) ; } } void dfs2 ( vector < int > v [ ] , int u , int parent ) { int mx1 = -1 , mx2 = -1 ; for ( int child : v [ u ] ) { if ( child == parent ) continue ; if ( in [ child ] >= mx1 ) { mx2 = mx1 ; mx1 = in [ child ] ; } else if ( in [ child ] > mx2 ) mx2 = in [ child ] ; } for ( int child : v [ u ] ) { if ( child == parent ) continue ; int longest = mx1 ; if ( mx1 == in [ child ] ) longest = mx2 ; out [ child ] = 1 + max ( out [ u ] , 1 + longest ) ; dfs2 ( v , child , u ) ; } } void printHeights ( vector < int > v [ ] , int n ) { dfs1 ( v , 1 , 0 ) ; dfs2 ( v , 1 , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) cout << " The ▁ maximum ▁ height ▁ when ▁ node ▁ " << i << " ▁ is ▁ considered ▁ as ▁ root " << " ▁ is ▁ " << max ( in [ i ] , out [ i ] ) << " STRNEWLINE " ; } int main ( ) { int n = 11 ; vector < int > v [ n + 1 ] ; v [ 1 ] . push_back ( 2 ) , v [ 2 ] . push_back ( 1 ) ; v [ 1 ] . push_back ( 3 ) , v [ 3 ] . push_back ( 1 ) ; v [ 1 ] . push_back ( 4 ) , v [ 4 ] . push_back ( 1 ) ; v [ 2 ] . push_back ( 5 ) , v [ 5 ] . push_back ( 2 ) ; v [ 2 ] . push_back ( 6 ) , v [ 6 ] . push_back ( 2 ) ; v [ 3 ] . push_back ( 7 ) , v [ 7 ] . push_back ( 3 ) ; v [ 7 ] . push_back ( 10 ) , v [ 10 ] . push_back ( 7 ) ; v [ 7 ] . push_back ( 11 ) , v [ 11 ] . push_back ( 7 ) ; v [ 4 ] . push_back ( 8 ) , v [ 8 ] . push_back ( 4 ) ; v [ 4 ] . push_back ( 9 ) , v [ 9 ] . push_back ( 4 ) ; printHeights ( v , n ) ; return 0 ; }
Golomb sequence | C ++ Program to find first n terms of Golomb sequence . ; Return the nth element of Golomb sequence ; base case ; Recursive Step ; Print the first n term of Golomb Sequence ; Finding first n terms of Golomb Sequence . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findGolomb ( int n ) { if ( n == 1 ) return 1 ; return 1 + findGolomb ( n - findGolomb ( findGolomb ( n - 1 ) ) ) ; } void printGolomb ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) cout << findGolomb ( i ) << " ▁ " ; } int main ( ) { int n = 9 ; printGolomb ( n ) ; return 0 ; }
Printing Items in 0 / 1 Knapsack | CPP code for Dynamic Programming based solution for 0 - 1 Knapsack problem ; A utility function that returns maximum of two integers ; Prints the items which are put in a knapsack of capacity W ; Build table K [ ] [ ] in bottom up manner ; stores the result of Knapsack ; either the result comes from the top ( K [ i - 1 ] [ w ] ) or from ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] ) as in Knapsack table . If it comes from the latter one / it means the item is included . ; This item is included . ; Since this weight is included its value is deducted ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int max ( int a , int b ) { return ( a > b ) ? a : b ; } void printknapSack ( int W , int wt [ ] , int val [ ] , int n ) { int i , w ; int K [ n + 1 ] [ W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] , K [ i - 1 ] [ w ] ) ; else K [ i ] [ w ] = K [ i - 1 ] [ w ] ; } } int res = K [ n ] [ W ] ; cout << res << endl ; w = W ; for ( i = n ; i > 0 && res > 0 ; i -- ) { if ( res == K [ i - 1 ] [ w ] ) continue ; else { cout << " ▁ " << wt [ i - 1 ] ; res = res - val [ i - 1 ] ; w = w - wt [ i - 1 ] ; } } } int main ( ) { int val [ ] = { 60 , 100 , 120 } ; int wt [ ] = { 10 , 20 , 30 } ; int W = 50 ; int n = sizeof ( val ) / sizeof ( val [ 0 ] ) ; printknapSack ( W , wt , val , n ) ; return 0 ; }
s | C ++ program for the above approach ; To sort the array and return the answer ; sort the array ; fill all stated with - 1 when only one element ; as dp [ 0 ] = 0 ( base case ) so min no of elements to be removed are n - 1 elements ; Iterate from 1 to n - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int removals ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int dp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = -1 ; int ans = n - 1 ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = i ; int j = dp [ i - 1 ] ; while ( j != i && arr [ i ] - arr [ j ] > k ) { j ++ ; } dp [ i ] = min ( dp [ i ] , j ) ; ans = min ( ans , ( n - ( i - j + 1 ) ) ) ; } return ans ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 4 ; cout << removals ( a , n , k ) ; return 0 ; }
Maximum number of segments of lengths a , b and c | C ++ implementation to divide N into maximum number of segments of length a , b and c ; function to find the maximum number of segments ; stores the maximum number of segments each index can have ; initialize with - 1 ; 0 th index will have 0 segments base case ; traverse for all possible segments till n ; conditions if ( i + a <= n ) avoid buffer overflow ; if ( i + b <= n ) avoid buffer overflow ; if ( i + c <= n ) avoid buffer overflow ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSegments ( int n , int a , int b , int c ) { int dp [ n + 1 ] ; memset ( dp , -1 , sizeof ( dp ) ) ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ i ] != -1 ) { dp [ i + a ] = max ( dp [ i ] + 1 , dp [ i + a ] ) ; dp [ i + b ] = max ( dp [ i ] + 1 , dp [ i + b ] ) ; dp [ i + c ] = max ( dp [ i ] + 1 , dp [ i + c ] ) ; } } return dp [ n ] ; } int main ( ) { int n = 7 , a = 5 , b = 2 , c = 5 ; cout << maximumSegments ( n , a , b , c ) ; return 0 ; }
Maximize the sum of selected numbers from an array to make it empty | CPP program to Maximize the sum of selected numbers by deleting three consecutive numbers . ; function to maximize the sum of selected numbers ; Largest element in the array ; An array to count the occurence of each element ; ans to store the result ; Using the above mentioned approach ; if occurence is greater than 0 ; add it to ans ; decrease i - 1 th element by 1 ; decrease ith element by 1 ; decrease i ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximizeSum ( int arr [ ] , int n ) { int mx = -1 ; for ( int i = 0 ; i < n ; i ++ ) { mx = max ( mx , arr [ i ] ) ; } int freq [ mx + 1 ] ; memset ( freq , 0 , sizeof ( freq ) ) ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } int ans = 0 , i = mx ; while ( i > 0 ) { if ( freq [ i ] > 0 ) { ans += i ; freq [ i - 1 ] -- ; freq [ i ] -- ; } else { i -- ; } } return ans ; } int main ( ) { int a [ ] = { 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maximizeSum ( a , n ) ; return 0 ; }
Print n terms of Newman | C ++ Program to print n terms of Newman - Conway Sequence ; Function to find the n - th element ; Declare array to store sequence ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sequence ( int n ) { int f [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; cout << f [ 1 ] << " ▁ " << f [ 2 ] << " ▁ " ; for ( int i = 3 ; i <= n ; i ++ ) { f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; cout << f [ i ] << " ▁ " ; } } int main ( ) { int n = 13 ; sequence ( n ) ; return 0 ; }
LCS formed by consecutive segments of at least length K | CPP program to find the Length of Longest subsequence formed by consecutive segments of at least length K ; Returns the length of the longest common subsequence with a minimum of length of K consecutive segments ; length of strings ; declare the lcs and cnt array ; iterate from i = 1 to n and j = 1 to j = m ; stores the maximum of lcs [ i - 1 ] [ j ] and lcs [ i ] [ j - 1 ] ; when both the characters are equal of s1 and s2 ; when length of common segment is more than k , then update lcs answer by adding that segment to the answer ; formulate for all length of segments to get the longest subsequence with consecutive Common Segment of length of min k length ; update lcs value by adding segment length ; driver code to check the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubsequenceCommonSegment ( int k , string s1 , string s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; int lcs [ n + 1 ] [ m + 1 ] ; int cnt [ n + 1 ] [ m + 1 ] ; memset ( lcs , 0 , sizeof ( lcs ) ) ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { lcs [ i ] [ j ] = max ( lcs [ i - 1 ] [ j ] , lcs [ i ] [ j - 1 ] ) ; if ( s1 [ i - 1 ] == s2 [ j - 1 ] ) cnt [ i ] [ j ] = cnt [ i - 1 ] [ j - 1 ] + 1 ; if ( cnt [ i ] [ j ] >= k ) { for ( int a = k ; a <= cnt [ i ] [ j ] ; a ++ ) lcs [ i ] [ j ] = max ( lcs [ i ] [ j ] , lcs [ i - a ] [ j - a ] + a ) ; } } } return lcs [ n ] [ m ] ; } int main ( ) { int k = 4 ; string s1 = " aggasdfa " ; string s2 = " aggajasdfa " ; cout << longestSubsequenceCommonSegment ( k , s1 , s2 ) ; return 0 ; }
Check for possible path in 2D matrix | C ++ program to find if there is path from top left to right bottom ; to find the path from top left to bottom right ; directions ; queue ; insert the top right corner . ; until queue is empty ; mark as visited ; destination is reached . ; check all four directions ; using the direction array ; not blocked and valid ; Driver Code ; Given array ; path from arr [ 0 ] [ 0 ] to arr [ row ] [ col ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define row  5 NEW_LINE #define col  5 NEW_LINE bool isPath ( int arr [ row ] [ col ] ) { int dir [ 4 ] [ 2 ] = { { 0 , 1 } , { 0 , -1 } , { 1 , 0 } , { -1 , 0 } } ; queue < pair < int , int > > q ; q . push ( make_pair ( 0 , 0 ) ) ; while ( q . size ( ) > 0 ) { pair < int , int > p = q . front ( ) ; q . pop ( ) ; arr [ p . first ] [ p . second ] = -1 ; if ( p == make_pair ( row - 1 , col - 1 ) ) return true ; for ( int i = 0 ; i < 4 ; i ++ ) { int a = p . first + dir [ i ] [ 0 ] ; int b = p . second + dir [ i ] [ 1 ] ; if ( arr [ a ] [ b ] != -1 && a >= 0 && b >= 0 && a < row && b < col ) { q . push ( make_pair ( a , b ) ) ; } } } return false ; } int main ( ) { int arr [ row ] [ col ] = { { 0 , 0 , 0 , -1 , 0 } , { -1 , 0 , 0 , -1 , -1 } , { 0 , 0 , 0 , -1 , 0 } , { -1 , 0 , -1 , 0 , -1 } , { 0 , 0 , -1 , 0 , 0 } } ; if ( isPath ( arr ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Next Smaller Element | A Stack based C ++ program to find next smaller element for all array elements ; prints NSE for elements of array arr [ ] of size n ; iterate for rest of the elements ; while stack is not empty and the top element is greater than next a ) NSE for top is next , use top 's index to maintain original order b) pop the top element from stack ; push next to stack so that we can find NSE for it ; After iterating over the loop , the remaining elements in stack do not have any NSE , so set - 1 for them ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNSE ( int arr [ ] , int n ) { stack < pair < int , int > > s ; vector < int > ans ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { int next = arr [ i ] ; if ( s . empty ( ) ) { s . push ( { next , i } ) ; continue ; } while ( ! s . empty ( ) && s . top ( ) . first > next ) { ans [ s . top ( ) . second ] = next ; s . pop ( ) ; } s . push ( { next , i } ) ; } while ( ! s . empty ( ) ) { ans [ s . top ( ) . second ] = -1 ; s . pop ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ - - - > ▁ " << ans [ i ] << endl ; } } int main ( ) { int arr [ ] = { 11 , 13 , 21 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printNSE ( arr , n ) ; return 0 ; }
Entringer Number | CPP Program to find Entringer Number E ( n , k ) ; Return Entringer Number E ( n , k ) ; Base cases ; Finding dp [ i ] [ j ] ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int zigzag ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ i - j ] ; return dp [ n ] [ k ] ; } int main ( ) { int n = 4 , k = 3 ; cout << zigzag ( n , k ) << endl ; return 0 ; }
Lobb Number | CPP Program to find Ln , m Lobb Number . ; Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Return the Lm , n Lobb Number . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAXN  109 NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int lobb ( int n , int m ) { return ( ( 2 * m + 1 ) * binomialCoeff ( 2 * n , m + n ) ) / ( m + n + 1 ) ; } int main ( ) { int n = 5 , m = 3 ; cout << lobb ( n , m ) << endl ; return 0 ; }
Count of arrays having consecutive element with different values | CPP Program to find count of arrays . ; Return the number of arrays with given constartints . ; Initialising dp [ 0 ] and dp [ 1 ] . ; Computing f ( i ) for each 2 <= i <= n . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAXN  109 NEW_LINE using namespace std ; int countarray ( int n , int k , int x ) { int dp [ MAXN ] = { 0 } ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ; return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ; } int main ( ) { int n = 4 , k = 3 , x = 2 ; cout << countarray ( n , k , x ) << endl ; return 0 ; }
Number of palindromic subsequences of length k where k <= 3 | CPP program to count number of subsequences of given length . ; Precompute the prefix and suffix array . ; Precompute the prefix 2D array ; Precompute the Suffix 2D array . ; Find the number of palindromic subsequence of length k ; If k is 1. ; If k is 2 ; Adding all the products of prefix array ; For k greater than 2. Adding all the products of value of prefix and suffix array . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  100 NEW_LINE #define MAX_CHAR  26 NEW_LINE using namespace std ; void precompute ( string s , int n , int l [ ] [ MAX ] , int r [ ] [ MAX ] ) { l [ s [ 0 ] - ' a ' ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) l [ j ] [ i ] += l [ j ] [ i - 1 ] ; l [ s [ i ] - ' a ' ] [ i ] ++ ; } r [ s [ n - 1 ] - ' a ' ] [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) r [ j ] [ i ] += r [ j ] [ i + 1 ] ; r [ s [ i ] - ' a ' ] [ i ] ++ ; } } int countPalindromes ( int k , int n , int l [ ] [ MAX ] , int r [ ] [ MAX ] ) { int ans = 0 ; if ( k == 1 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += l [ i ] [ n - 1 ] ; return ans ; } if ( k == 2 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += ( ( l [ i ] [ n - 1 ] * ( l [ i ] [ n - 1 ] - 1 ) ) / 2 ) ; return ans ; } for ( int i = 1 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < MAX_CHAR ; j ++ ) ans += l [ j ] [ i - 1 ] * r [ j ] [ i + 1 ] ; return ans ; } int main ( ) { string s = " aabab " ; int k = 2 ; int n = s . length ( ) ; int l [ MAX_CHAR ] [ MAX ] = { 0 } , r [ MAX_CHAR ] [ MAX ] = { 0 } ; precompute ( s , n , l , r ) ; cout << countPalindromes ( k , n , l , r ) << endl ; return 0 ; }
Minimum cost to make Longest Common Subsequence of length k | C ++ program to find subarray with sum closest to 0 ; Return Minimum cost to make LCS of length k ; If k is 0. ; If length become less than 0 , return big number . ; If state already calculated . ; Finding the cost ; Finding minimum cost and saving the state value ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 30 ; int solve ( char X [ ] , char Y [ ] , int l , int r , int k , int dp [ ] [ N ] [ N ] ) { if ( ! k ) return 0 ; if ( l < 0 r < 0 ) return 1e9 ; if ( dp [ l ] [ r ] [ k ] != -1 ) return dp [ l ] [ r ] [ k ] ; int cost = ( X [ l ] - ' a ' ) ^ ( Y [ r ] - ' a ' ) ; return dp [ l ] [ r ] [ k ] = min ( { cost + solve ( X , Y , l - 1 , r - 1 , k - 1 , dp ) , solve ( X , Y , l - 1 , r , k , dp ) , solve ( X , Y , l , r - 1 , k , dp ) } ) ; } int main ( ) { char X [ ] = " abble " ; char Y [ ] = " pie " ; int n = strlen ( X ) ; int m = strlen ( Y ) ; int k = 2 ; int dp [ N ] [ N ] [ N ] ; memset ( dp , -1 , sizeof dp ) ; int ans = solve ( X , Y , n - 1 , m - 1 , k , dp ) ; cout << ( ans == 1e9 ? -1 : ans ) << endl ; return 0 ; }
Maximum sum path in a matrix from top to bottom | C ++ implementation to find the maximum sum path in a matrix ; function to find the maximum sum path in a matric ; if there is a single element only ; dp [ ] [ ] matrix to store the results of each iteration ; base case , copying elements of last row ; building up the dp [ ] [ ] matrix from bottom to the top row ; finding the maximum diagonal element in the ( i + 1 ) th row if that cell exists ; adding that ' max ' element to the mat [ i ] [ j ] element ; finding the maximum value from the first row of dp [ ] [ ] ; required maximum sum ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  10 NEW_LINE int maxSum ( int mat [ SIZE ] [ SIZE ] , int n ) { if ( n == 1 ) return mat [ 0 ] [ 0 ] ; int dp [ n ] [ n ] ; int maxSum = INT_MIN , max ; for ( int j = 0 ; j < n ; j ++ ) dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { max = INT_MIN ; if ( ( ( j - 1 ) >= 0 ) && ( max < dp [ i + 1 ] [ j - 1 ] ) ) max = dp [ i + 1 ] [ j - 1 ] ; if ( ( ( j + 1 ) < n ) && ( max < dp [ i + 1 ] [ j + 1 ] ) ) max = dp [ i + 1 ] [ j + 1 ] ; dp [ i ] [ j ] = mat [ i ] [ j ] + max ; } } for ( int j = 0 ; j < n ; j ++ ) if ( maxSum < dp [ 0 ] [ j ] ) maxSum = dp [ 0 ] [ j ] ; return maxSum ; } int main ( ) { int mat [ SIZE ] [ SIZE ] = { { 5 , 6 , 1 , 7 } , { -2 , 10 , 8 , -1 } , { 3 , -7 , -9 , 11 } , { 12 , -4 , 2 , 6 } } ; int n = 4 ; cout << " Maximum ▁ Sum ▁ = ▁ " << maxSum ( mat , n ) ; return 0 ; }