codeparrot/xlcost-text-to-code huggingface dataset:
Ways of transforming one string to other by removing 0 or more characters | C ++ program to count the distinct transformation of one string to other . ; If b = " " i . e . , an empty string . There is only one way to transform ( remove all characters ) ; Fil dp [ ] [ ] in bottom up manner Traverse all character of b [ ] ; Traverse all characters of a [ ] for b [ i ] ; Filling the first row of the dp matrix . ; Filling other rows . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTransformation ( string a , string b ) { int n = a . size ( ) , m = b . size ( ) ; if ( m == 0 ) return 1 ; int dp [ m ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( i == 0 ) { if ( j == 0 ) dp [ i ] [ j ] = ( a [ j ] == b [ i ] ) ? 1 : 0 ; else if ( a [ j ] == b [ i ] ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + 1 ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; } else { if ( a [ j ] == b [ i ] ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; } } } return dp [ m - 1 ] [ n - 1 ] ; } int main ( ) { string a = " abcccdf " , b = " abccdf " ; cout << countTransformation ( a , b ) << endl ; return 0 ; }
n | CPP code to find nth number with digits 0 , 1 , 2 , 3 , 4 , 5 ; function to convert num to base 6 ; Driver code ; initialize an array to 0 ; function calling to convert number n to base 6 ; if size is zero then return zero
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define max  100000 NEW_LINE int baseconversion ( int arr [ ] , int num , int base ) { int i = 0 , rem , j ; if ( num == 0 ) { return 0 ; } while ( num > 0 ) { rem = num % base ; arr [ i ++ ] = rem ; num /= base ; } return i ; } int main ( ) { int arr [ max ] = { 0 } ; int n = 10 ; int size = baseconversion ( arr , n - 1 , 6 ) ; if ( size == 0 ) cout << size ; for ( int i = size - 1 ; i >= 0 ; i -- ) { cout << arr [ i ] ; } return 0 ; }
Choose maximum weight with given weight and value ratio | C ++ program to choose item with maximum sum of weight under given constraint ; memoized recursive method to return maximum weight with K as ratio of weight and values ; base cases : if no item is remaining ; first make pair with last chosen item and difference between weight and values ; choose maximum value from following two 1 ) not selecting the current item and calling recursively 2 ) selection current item , including the weight and updating the difference before calling recursively ; method returns maximum sum of weight with K as ration of sum of weight and their values ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxWeightRec ( int wt [ ] , int val [ ] , int K , map < pair < int , int > , int > & mp , int last , int diff ) { if ( last == -1 ) { if ( diff == 0 ) return 0 ; else return INT_MIN ; } pair < int , int > tmp = make_pair ( last , diff ) ; if ( mp . find ( tmp ) != mp . end ( ) ) return mp [ tmp ] ; mp [ tmp ] = max ( maxWeightRec ( wt , val , K , mp , last - 1 , diff ) , wt [ last ] + maxWeightRec ( wt , val , K , mp , last - 1 , diff + wt [ last ] - val [ last ] * K ) ) ; return mp [ tmp ] ; } int maxWeight ( int wt [ ] , int val [ ] , int K , int N ) { map < pair < int , int > , int > mp ; return maxWeightRec ( wt , val , K , mp , N - 1 , 0 ) ; } int main ( ) { int wt [ ] = { 4 , 8 , 9 } ; int val [ ] = { 2 , 4 , 6 } ; int N = sizeof ( wt ) / sizeof ( int ) ; int K = 2 ; cout << maxWeight ( wt , val , K , N ) ; return 0 ; }
Pyramid form ( increasing then decreasing ) consecutive array using reduce operations | Program to find minimum cost for pyramid from given array ; Returns minimum cost to form a pyramid ; Store the maximum possible pyramid height ; Maximum height at start is 1 ; For each position calculate maximum height ; Maximum height at end is 1 ; For each position calculate maximum height ; Find minimum possible among calculated values ; Find maximum height of pyramid ; Calculate cost of this pyramid ; Calculate cost of left half ; Calculate cost of right half ; Driver code
#include <iostream> NEW_LINE using namespace std ; #define ull  unsigned long long NEW_LINE ull minPyramidCost ( ull arr [ ] , ull N ) { ull * left = new ull [ N ] ; ull * right = new ull [ N ] ; left [ 0 ] = min ( arr [ 0 ] , ( ull ) 1 ) ; for ( int i = 1 ; i < N ; ++ i ) left [ i ] = min ( arr [ i ] , min ( left [ i - 1 ] + 1 , ( ull ) i + 1 ) ) ; right [ N - 1 ] = min ( arr [ N - 1 ] , ( ull ) 1 ) ; for ( int i = N - 2 ; i >= 0 ; -- i ) right [ i ] = min ( arr [ i ] , min ( right [ i + 1 ] + 1 , N - i ) ) ; ull tot [ N ] ; for ( int i = 0 ; i < N ; ++ i ) tot [ i ] = min ( right [ i ] , left [ i ] ) ; ull max_ind = 0 ; for ( int i = 0 ; i < N ; ++ i ) if ( tot [ i ] > tot [ max_ind ] ) max_ind = i ; ull cost = 0 ; ull height = tot [ max_ind ] ; for ( int x = max_ind ; x >= 0 ; -- x ) { cost += arr [ x ] - height ; if ( height > 0 ) -- height ; } height = tot [ max_ind ] - 1 ; for ( int x = max_ind + 1 ; x < N ; ++ x ) { cost += arr [ x ] - height ; if ( height > 0 ) -- height ; } return cost ; } int main ( ) { ull arr [ ] = { 1 , 2 , 3 , 4 , 2 , 1 } ; ull N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minPyramidCost ( arr , N ) ; return 0 ; }
Maximum sum in a 2 x n grid such that no two elements are adjacent | C ++ program to find maximum sum in a grid such that no two elements are adjacent . ; Function to find max sum without adjacent ; Sum including maximum element of first column ; Not including first column 's element ; Traverse for further elements ; Update max_sum on including or excluding of previous column ; Include current column . Add maximum element from both row of current column ; If current column doesn 't to be included ; Return maximum of excl and incl As that will be the maximum sum ; Driver code
#include <bits/stdc++.h> NEW_LINE #define MAX  1000 NEW_LINE using namespace std ; int maxSum ( int grid [ 2 ] [ MAX ] , int n ) { int incl = max ( grid [ 0 ] [ 0 ] , grid [ 1 ] [ 0 ] ) ; int excl = 0 , excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = max ( excl , incl ) ; incl = excl + max ( grid [ 0 ] [ i ] , grid [ 1 ] [ i ] ) ; excl = excl_new ; } return max ( excl , incl ) ; } int main ( ) { int grid [ 2 ] [ MAX ] = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 10 } } ; int n = 5 ; cout << maxSum ( grid , n ) ; return 0 ; }
Minimum insertions to sort an array | C ++ program to get minimum number of insertion steps to sort an array ; method returns min steps of insertion we need to perform to sort array ' arr ' ; lis [ i ] is going to store length of lis that ends with i . ; Initialize lis values for all indexes ; Compute optimized lis values in bottom up manner ; The overall LIS must end with of the array elements . Pick maximum of all lis values ; return size of array minus length of LIS as final result ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minInsertionStepToSortArray ( int arr [ ] , int N ) { int lis [ N ] ; 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 ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int max = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( max < lis [ i ] ) max = lis [ i ] ; return ( N - max ) ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 1 , 4 , 7 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minInsertionStepToSortArray ( arr , N ) ; return 0 ; }
Print all distinct characters of a string in order ( 3 Methods ) |  ; checking if two charactors are equal
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { string str = " GeeksforGeeks " ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { int flag = 0 ; for ( int j = 0 ; j < str . size ( ) ; j ++ ) { if ( str [ i ] == str [ j ] and i != j ) { flag = 1 ; break ; } } if ( flag == 0 ) cout << str [ i ] ; } return 0 ; }
Print all distinct characters of a string in order ( 3 Methods ) | C ++ program to print distinct characters of a string . ; Print duplicates present in the passed string ; Create an array of size 256 and count of every character in it ; Count array with frequency of characters ; Print characters having count more than 0 ; Driver program
# include <iostream> NEW_LINE using namespace std ; # define NO_OF_CHARS  256 NEW_LINE void printDistinct ( char * str ) { int count [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; * ( str + i ) ; i ++ ) if ( * ( str + i ) != ' ▁ ' ) count [ * ( str + i ) ] ++ ; int n = i ; for ( i = 0 ; i < n ; i ++ ) if ( count [ * ( str + i ) ] == 1 ) cout << str [ i ] ; } int main ( ) { char str [ ] = " GeeksforGeeks " ; printDistinct ( str ) ; return 0 ; }
Shortest Uncommon Subsequence | A simple recursive C ++ program to find shortest uncommon subsequence . ; A recursive function to find the length of shortest uncommon subsequence ; S string is empty ; T string is empty ; Loop to search for current character ; char not found in T ; Return minimum of following two Not including current char in answer Including current char ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1005 NEW_LINE int shortestSeq ( char * S , char * T , int m , int n ) { if ( m == 0 ) return MAX ; if ( n <= 0 ) return 1 ; int k ; for ( k = 0 ; k < n ; k ++ ) if ( T [ k ] == S [ 0 ] ) break ; if ( k == n ) return 1 ; return min ( shortestSeq ( S + 1 , T , m - 1 , n ) , 1 + shortestSeq ( S + 1 , T + k + 1 , m - 1 , n - k - 1 ) ) ; } int main ( ) { char S [ ] = " babab " ; char T [ ] = " babba " ; int m = strlen ( S ) , n = strlen ( T ) ; int ans = shortestSeq ( S , T , m , n ) ; if ( ans >= MAX ) ans = -1 ; cout << " Length ▁ of ▁ shortest ▁ subsequence ▁ is : ▁ " << ans << endl ; return 0 ; }
Shortest Uncommon Subsequence | A dynamic programming based C ++ program to find shortest uncommon subsequence . ; Returns length of shortest common subsequence ; declaring 2D array of m + 1 rows and n + 1 columns dynamically ; T string is empty ; S string is empty ; char not present in T ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1005 NEW_LINE int shortestSeq ( char * S , char * T ) { int m = strlen ( S ) , n = strlen ( T ) ; int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i <= n ; i ++ ) dp [ 0 ] [ i ] = MAX ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { char ch = S [ i - 1 ] ; int k ; for ( k = j - 1 ; k >= 0 ; k -- ) if ( T [ k ] == ch ) break ; if ( k == -1 ) dp [ i ] [ j ] = 1 ; else dp [ i ] [ j ] = min ( dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ k ] + 1 ) ; } } int ans = dp [ m ] [ n ] ; if ( ans >= MAX ) ans = -1 ; return ans ; } int main ( ) { char S [ ] = " babab " ; char T [ ] = " babba " ; int m = strlen ( S ) , n = strlen ( T ) ; cout << " Length ▁ of ▁ shortest ▁ subsequence ▁ is ▁ : ▁ " << shortestSeq ( S , T ) << endl ; }
Count number of ways to jump to reach end | C ++ implementation to count number of ways to jump to reach end ; function to count ways to jump to reach end for each array element ; count_jump [ i ] store number of ways arr [ i ] can reach to the end ; Last element does not require to jump . Count ways to jump for remaining elements ; if the element can directly jump to the end ; add the count of all the elements that can reach to end and arr [ i ] can reach to them ; if element can reach to end then add its count to count_jump [ i ] ; if arr [ i ] cannot reach to the end ; print count_jump for each array element ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countWaysToJump ( int arr [ ] , int n ) { int count_jump [ n ] ; memset ( count_jump , 0 , sizeof ( count_jump ) ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= n - i - 1 ) count_jump [ i ] ++ ; for ( int j = i + 1 ; j < n - 1 && j <= arr [ i ] + i ; j ++ ) if ( count_jump [ j ] != -1 ) count_jump [ i ] += count_jump [ j ] ; if ( count_jump [ i ] == 0 ) count_jump [ i ] = -1 ; } for ( int i = 0 ; i < n ; i ++ ) cout << count_jump [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 8 , 9 , 1 , 0 , 7 , 6 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countWaysToJump ( arr , n ) ; return 0 ; }
Minimum steps to delete a string after repeated deletion of palindrome substrings | C ++ program to find minimum step to delete a string ; method returns minimum step for deleting the string , where in one step a palindrome is removed ; declare dp array and initialize it with 0 s ; loop for substring length we are considering ; loop with two variables i and j , denoting starting and ending of substrings ; If substring length is 1 , then 1 step will be needed ; delete the ith char individually and assign result for subproblem ( i + 1 , j ) ; if current and next char are same , choose min from current and subproblem ( i + 2 , j ) ; loop over all right characters and suppose Kth char is same as ith character then choose minimum from current and two substring after ignoring ith and Kth char ; Uncomment below snippet to print actual dp tablex for ( int i = 0 ; i < N ; i ++ , cout << endl ) for ( int j = 0 ; j < N ; j ++ ) cout << dp [ i ] [ j ] << " ▁ " ; ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minStepToDeleteString ( string str ) { int N = str . length ( ) ; int dp [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) for ( int j = 0 ; j <= N ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int len = 1 ; len <= N ; len ++ ) { for ( int i = 0 , j = len - 1 ; j < N ; i ++ , j ++ ) { if ( len == 1 ) dp [ i ] [ j ] = 1 ; else { dp [ i ] [ j ] = 1 + dp [ i + 1 ] [ j ] ; if ( str [ i ] == str [ i + 1 ] ) dp [ i ] [ j ] = min ( 1 + dp [ i + 2 ] [ j ] , dp [ i ] [ j ] ) ; for ( int K = i + 2 ; K <= j ; K ++ ) if ( str [ i ] == str [ K ] ) dp [ i ] [ j ] = min ( dp [ i + 1 ] [ K - 1 ] + dp [ K + 1 ] [ j ] , dp [ i ] [ j ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } int main ( ) { string str = "2553432" ; cout << minStepToDeleteString ( str ) << endl ; return 0 ; }
Clustering / Partitioning an array such that sum of square differences is minimum | C ++ program to find minimum cost k partitions of array . ; Initialize answer as infinite . ; function to generate all possible answers . and compute minimum of all costs . i -- > is index of previous partition par -- > is current number of partitions a [ ] and n -- > Input array and its size current_ans -- > Cost of partitions made so far . ; If number of partitions is more than k ; If we have mad k partitions and have reached last element ; 1 ) Partition array at different points 2 ) For every point , increase count of partitions , " par " by 1. 3 ) Before recursive call , add cost of the partition to current_ans ; Driver code
#include <iostream> NEW_LINE using namespace std ; const int inf = 1000000000 ; int ans = inf ; void solve ( int i , int par , int a [ ] , int n , int k , int current_ans ) { if ( par > k ) return ; if ( par == k && i == n - 1 ) { ans = min ( ans , current_ans ) ; return ; } for ( int j = i + 1 ; j < n ; j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a [ j ] - a [ i + 1 ] ) ) ; } int main ( ) { int k = 2 ; int a [ ] = { 1 , 5 , 8 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; solve ( -1 , 0 , a , n , k , 0 ) ; cout << ans << endl ; return 0 ; }
Minimum steps to minimize n as per given condition | CPP program to minimize n to 1 by given rule in minimum steps ; function to calculate min steps ; base case ; store temp value for n as min ( f ( n - 1 ) , f ( n / 2 ) , f ( n / 3 ) ) + 1 ; store memo [ n ] and return ; This function mainly initializes memo [ ] and calls getMinSteps ( n , memo ) ; initialize memoized array ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinSteps ( int n , int * memo ) { if ( n == 1 ) return 0 ; if ( memo [ n ] != -1 ) return memo [ n ] ; int res = getMinSteps ( n - 1 , memo ) ; if ( n % 2 == 0 ) res = min ( res , getMinSteps ( n / 2 , memo ) ) ; if ( n % 3 == 0 ) res = min ( res , getMinSteps ( n / 3 , memo ) ) ; memo [ n ] = 1 + res ; return memo [ n ] ; } int getMinSteps ( int n ) { int memo [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) memo [ i ] = -1 ; return getMinSteps ( n , memo ) ; } int main ( ) { int n = 10 ; cout << getMinSteps ( n ) ; return 0 ; }
Count of arrays in which all adjacent elements are such that one of them divide the another | C ++ program to count number of arrays of size n such that every element is in range [ 1 , m ] and adjacen are divisible ; For storing factors . ; For storing multiples . ; calculating the factors and multiples of elements [ 1. . . m ] . ; Initialising for size 1 array for each i <= m . ; Calculating the number of array possible of size i and starting with j . ; For all previous possible values . Adding number of factors . ; Adding number of multiple . ; Calculating the total count of array which start from [ 1. . . m ] . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  1000 NEW_LINE using namespace std ; int numofArray ( int n , int m ) { int dp [ MAX ] [ MAX ] ; vector < int > di [ MAX ] ; vector < int > mu [ MAX ] ; memset ( dp , 0 , sizeof dp ) ; memset ( di , 0 , sizeof di ) ; memset ( mu , 0 , sizeof mu ) ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 2 * i ; j <= m ; j += i ) { di [ j ] . push_back ( i ) ; mu [ i ] . push_back ( j ) ; } di [ i ] . push_back ( i ) ; } for ( int i = 1 ; i <= m ; i ++ ) dp [ 1 ] [ i ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ i ] [ j ] = 0 ; for ( auto x : di [ j ] ) dp [ i ] [ j ] += dp [ i - 1 ] [ x ] ; for ( auto x : mu [ j ] ) dp [ i ] [ j ] += dp [ i - 1 ] [ x ] ; } } int ans = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { ans += dp [ n ] [ i ] ; di [ i ] . clear ( ) ; mu [ i ] . clear ( ) ; } return ans ; } int main ( ) { int n = 3 , m = 3 ; cout << numofArray ( n , m ) << " STRNEWLINE " ; return 0 ; }
Temple Offerings | C ++ Program to find total offerings required ; To store count of increasing order temples on left and right ( including current temple ) ; Returns count of minimum offerings for n temples of given heights . ; Initialize counts for all temples ; Values corner temples ; Filling left and right values using same values of previous ( or next ) ; Computing max of left and right for all temples and returning sum . ; Driver function
#include <iostream> NEW_LINE using namespace std ; struct Temple { int L ; int R ; } ; int offeringNumber ( int n , int templeHeight [ ] ) { Temple chainSize [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { chainSize [ i ] . L = -1 ; chainSize [ i ] . R = -1 ; } chainSize [ 0 ] . L = 1 ; chainSize [ n - 1 ] . R = 1 ; for ( int i = 1 ; i < n ; ++ i ) { if ( templeHeight [ i - 1 ] < templeHeight [ i ] ) chainSize [ i ] . L = chainSize [ i - 1 ] . L + 1 ; else chainSize [ i ] . L = 1 ; } for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( templeHeight [ i + 1 ] < templeHeight [ i ] ) chainSize [ i ] . R = chainSize [ i + 1 ] . R + 1 ; else chainSize [ i ] . R = 1 ; } int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) sum += max ( chainSize [ i ] . L , chainSize [ i ] . R ) ; return sum ; } int main ( ) { int arr1 [ 3 ] = { 1 , 2 , 2 } ; cout << offeringNumber ( 3 , arr1 ) << " STRNEWLINE " ; int arr2 [ 6 ] = { 1 , 4 , 3 , 6 , 2 , 1 } ; cout << offeringNumber ( 6 , arr2 ) << " STRNEWLINE " ; return 0 ; }
Smallest length string with repeated replacement of two distinct adjacent | C ++ program to find smallest possible length of a string of only three characters ; Returns smallest possible length with given operation allowed . ; Counint occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; If all characters are same . ; If all characters are present even number of times or all are present odd number of times . ; Answer is 1 for all other cases . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int stringReduction ( string str ) { int n = str . length ( ) ; int count [ 3 ] = { 0 } ; for ( int i = 0 ; i < n ; ++ i ) count [ str [ i ] - ' a ' ] ++ ; if ( count [ 0 ] == n count [ 1 ] == n count [ 2 ] == n ) return n ; if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) && ( count [ 1 ] % 2 ) == ( count [ 2 ] % 2 ) ) return 2 ; return 1 ; } int main ( ) { string str = " abcbbaacb " ; cout << stringReduction ( str ) ; return 0 ; }
Largest sum Zigzag sequence in a matrix | Memoization based C ++ program to find the largest sum zigzag sequence ; Returns largest sum of a Zigzag sequence starting from ( i , j ) and ending at a bottom cell . ; If we have reached bottom ; Find the largest sum by considering all possible next elements in sequence . ; Returns largest possible sum of a Zizag sequence starting from top and ending at bottom . ; Consider all cells of top row as starting point ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; int dp [ MAX ] [ MAX ] ; int largestZigZagSumRec ( int mat [ ] [ MAX ] , int i , int j , int n ) { if ( dp [ i ] [ j ] != -1 ) return dp [ i ] [ j ] ; if ( i == n - 1 ) return ( dp [ i ] [ j ] = mat [ i ] [ j ] ) ; int zzs = 0 ; for ( int k = 0 ; k < n ; k ++ ) if ( k != j ) zzs = max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ; return ( dp [ i ] [ j ] = ( zzs + mat [ i ] [ j ] ) ) ; } int largestZigZag ( int mat [ ] [ MAX ] , int n ) { memset ( dp , -1 , sizeof ( dp ) ) ; int res = 0 ; for ( int j = 0 ; j < n ; j ++ ) res = max ( res , largestZigZagSumRec ( mat , 0 , j , n ) ) ; return res ; } int main ( ) { int n = 3 ; int mat [ ] [ MAX ] = { { 4 , 2 , 1 } , { 3 , 9 , 6 } , { 11 , 3 , 15 } } ; cout << " Largest ▁ zigzag ▁ sum : ▁ " << largestZigZag ( mat , n ) ; return 0 ; }
Number of subsequences of the form a ^ i b ^ j c ^ k | C ++ program to count subsequences of the form a ^ i b ^ j c ^ k ; Returns count of subsequences of the form a ^ i b ^ j c ^ k ; Initialize counts of different subsequences caused by different combination of ' a ' ; Initialize counts of different subsequences caused by different combination of ' a ' and different combination of ' b ' ; Initialize counts of different subsequences caused by different combination of ' a ' , ' b ' and ' c ' . ; Traverse all characters of given string ; If current character is ' a ' , then there are the following possibilities : a ) Current character begins a new subsequence . b ) Current character is part of aCount subsequences . c ) Current character is not part of aCount subsequences . ; If current character is ' b ' , then there are following possibilities : a ) Current character begins a new subsequence of b 's with aCount subsequences. b) Current character is part of bCount subsequences. c) Current character is not part of  bCount subsequences.  ; If current character is ' c ' , then there are following possibilities : a ) Current character begins a new subsequence of c 's with bCount subsequences. b) Current character is part of cCount subsequences. c) Current character is not part of  cCount subsequences.  ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubsequences ( string s ) { int aCount = 0 ; int bCount = 0 ; int cCount = 0 ; for ( unsigned int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == ' a ' ) aCount = ( 1 + 2 * aCount ) ; else if ( s [ i ] == ' b ' ) bCount = ( aCount + 2 * bCount ) ; else if ( s [ i ] == ' c ' ) cCount = ( bCount + 2 * cCount ) ; } return cCount ; } int main ( ) { string s = " abbc " ; cout << countSubsequences ( s ) << endl ; return 0 ; }