codeparrot/xlcost-text-to-code huggingface dataset:
Geek | Function to calculate the N - th Geek - onacci Number ; Stores the geekonacci series ; Store the first three terms of the series ; Iterate over the range [ 3 , N ] ; Update the value of arr [ i ] as the sum of previous 3 terms in the series ; Return the last element of arr [ ] as the N - th term ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( int A , int B , int C , int N ) { int arr [ N ] ; arr [ 0 ] = A ; arr [ 1 ] = B ; arr [ 2 ] = C ; for ( int i = 3 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] + arr [ i - 3 ] ; } return arr [ N - 1 ] ; } int main ( ) { int A = 1 , B = 3 , C = 2 , N = 4 ; cout << ( find ( A , B , C , N ) ) ; return 0 ; }
Maximum sum subsequence made up of at most K distant elements including the first and last array elements | CPP program for the above approach ; Function to find maximum sum of a subsequence satisfying the given conditions ; Stores the maximum sum ; Starting index of the subsequence ; Stores the pair of maximum value and the index of that value ; Traverse the array ; Increment the first value of deque by arr [ i ] and store it in dp [ i ] ; Delete all the values which are less than dp [ i ] in deque ; Append the current pair of value and index in deque ; If first value of the queue is at a distance > K ; Return the value at the last index ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxResult ( int arr [ ] , int k , int n ) { int dp [ n ] = { 0 } ; dp [ 0 ] = arr [ 0 ] ; deque < pair < int , int > > q ; q . push_back ( { arr [ 0 ] , 0 } ) ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] + q . front ( ) . first ; while ( q . size ( ) > 0 and q . back ( ) . first < dp [ i ] ) q . pop_back ( ) ; q . push_back ( { dp [ i ] , i } ) ; if ( i - k == q . front ( ) . second ) q . pop_front ( ) ; } return dp [ n - 1 ] ; } int main ( ) { int arr [ ] = { 10 , -5 , -2 , 4 , 0 , 3 } ; int K = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxResult ( arr , K , n ) ; }
Minimum days required to cure N persons | C ++ Program for the above approach ; Function to find minimum count of days required to give a cure such that the high risk person and risk person does not get a dose on same day . ; Stores count of persons whose age is less than or equal to 10 and greater than or equal to 60. ; Stores the count of persons whose age is in the range [ 11 , 59 ] ; Traverse the array arr [ ] ; If age less than or equal to 10 or greater than or equal to 60 ; Update risk ; Update normal_risk ; Calculate days to cure risk and normal_risk persons ; Print the days ; Driver Code ; Given array ; Size of the array ; Given P
#include <bits/stdc++.h> NEW_LINE using namespace std ; void daysToCure ( int arr [ ] , int N , int P ) { int risk = 0 ; int normal_risk = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 60 arr [ i ] <= 10 ) { risk ++ ; } else { normal_risk ++ ; } } int days = ( risk / P ) + ( risk % P > 0 ) + ( normal_risk / P ) + ( normal_risk % P > 0 ) ; cout << days ; } int main ( ) { int arr [ ] = { 9 , 80 , 27 , 72 , 79 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int P = 2 ; daysToCure ( arr , N , P ) ; return 0 ; }
Count numbers from a given range whose product of digits is K | C ++ program to implement the above approach ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; If count of digits in a number greater than count of digits in X ; If product of digits of a number equal to K ; If overlapping subproblems already occurred ; Stores count of numbers whose product of digits is K ; Check if the numbers exceeds K or not ; Iterate over all possible value of i - th digits ; if number contains leading 0 ; Update res ; Update res ; Return res ; Utility function to count the numbers in the range [ L , R ] whose prod of digits is K ; Stores numbers in the form of string ; Stores overlapping subproblems ; Initialize dp [ ] [ ] [ ] to - 1 ; Stores count of numbers in the range [ 0 , R ] whose product of digits is k ; Update str ; Initialize dp [ ] [ ] [ ] to - 1 ; Stores count of numbers in the range [ 0 , L - 1 ] whose product of digits is k ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  100 NEW_LINE int cntNum ( string X , int i , int prod , int K , int st , int tight , int dp [ M ] [ M ] [ 2 ] [ 2 ] ) { if ( i >= X . length ( ) prod > K ) { return prod == K ; } if ( dp [ prod ] [ i ] [ tight ] [ st ] != -1 ) { return dp [ prod ] [ i ] [ tight ] [ st ] ; } int res = 0 ; int end = tight ? X [ i ] - '0' : 9 ; for ( int j = 0 ; j <= end ; j ++ ) { if ( j == 0 && ! st ) { res += cntNum ( X , i + 1 , prod , K , false , ( tight & ( j == end ) ) , dp ) ; } else { res += cntNum ( X , i + 1 , prod * j , K , true , ( tight & ( j == end ) ) , dp ) ; } return dp [ prod ] [ i ] [ tight ] [ st ] = res ; } int UtilCntNumRange ( int L , int R , int K ) { string str = to_string ( R ) ; int dp [ M ] [ M ] [ 2 ] [ 2 ] ; memset ( dp , -1 , sizeof ( dp ) ) ; int cntR = cntNum ( str , 0 , 1 , K , false , true , dp ) ; str = to_string ( L - 1 ) ; memset ( dp , -1 , sizeof ( dp ) ) ; int cntL = cntNum ( str , 0 , 1 , K , false , true , dp ) ; return ( cntR - cntL ) ; } int main ( ) { int L = 20 , R = 10000 , K = 14 ; cout << UtilCntNumRange ( L , R , K ) ; }
Count all possible N | C ++ program for the above approach ; Function to find the number of vowel permutations possible ; To avoid the large output value ; Initialize 2D dp array ; Initialize dp [ 1 ] [ i ] as 1 since string of length 1 will consist of only one vowel in the string ; Directed graph using the adjacency matrix ; Iterate over the range [ 1 , N ] ; Traverse the directed graph ; Traversing the list ; Update dp [ i + 1 ] [ u ] ; Stores total count of permutations ; Return count of permutations ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countVowelPermutation ( int n ) { int MOD = ( int ) ( 1e9 + 7 ) ; long dp [ n + 1 ] [ 5 ] ; for ( int i = 0 ; i < 5 ; i ++ ) { dp [ 1 ] [ i ] = 1 ; } vector < vector < int > > relation = { { 1 } , { 0 , 2 } , { 0 , 1 , 3 , 4 } , { 2 , 4 } , { 0 } } ; for ( int i = 1 ; i < n ; i ++ ) { for ( int u = 0 ; u < 5 ; u ++ ) { dp [ i + 1 ] [ u ] = 0 ; for ( int v : relation [ u ] ) { dp [ i + 1 ] [ u ] += dp [ i ] [ v ] % MOD ; } } } long ans = 0 ; for ( int i = 0 ; i < 5 ; i ++ ) { ans = ( ans + dp [ n ] [ i ] ) % MOD ; } return ( int ) ans ; } int main ( ) { int N = 2 ; cout << countVowelPermutation ( N ) ; }
Queries to calculate Bitwise OR of each subtree of a given node in an N | C ++ program for the above approach ; Maximum Number of nodes ; Adjacency list ; Stores Bitwise OR of each node ; Function to add edges to the Tree ; Traverse the edges ; Add edges ; Function to perform DFS Traversal on the given tree ; Initialize answer with bitwise OR of current node ; Iterate over each child of the current node ; Skip parent node ; Call DFS for each child ; Taking bitwise OR of the answer of the child to find node 's OR value ; Function to call DFS from the '= root for precomputing answers ; Function to calculate and print the Bitwise OR for Q queries ; Perform preprocessing ; Iterate over each given query ; Utility function to find and print bitwise OR for Q queries ; Function to add edges to graph ; Function call ; Driver Code ; Number of nodes ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1e5 + 5 ; vector < int > adj [ N ] ; vector < int > answer ( N ) ; void addEdgesToGraph ( int Edges [ ] [ 2 ] , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } } void DFS ( int node , int parent , int Val [ ] ) { answer [ node ] = Val [ node ] ; for ( int child : adj [ node ] ) { if ( child == parent ) continue ; DFS ( child , node , Val ) ; answer [ node ] = ( answer [ node ] answer [ child ] ) ; } } void preprocess ( int Val [ ] ) { DFS ( 1 , -1 , Val ) ; } void findSubtreeOR ( int Queries [ ] , int Q , int Val [ ] ) { preprocess ( Val ) ; for ( int i = 0 ; i < Q ; i ++ ) { cout << answer [ Queries [ i ] ] << ' ▁ ' ; } } void findSubtreeORUtil ( int N , int Edges [ ] [ 2 ] , int Val [ ] , int Queries [ ] , int Q ) { addEdgesToGraph ( Edges , N ) ; findSubtreeOR ( Queries , Q , Val ) ; } int main ( ) { int N = 5 ; int Edges [ ] [ 2 ] = { { 1 , 2 } , { 1 , 3 } , { 3 , 4 } , { 3 , 5 } } ; int Val [ ] = { 0 , 2 , 3 , 4 , 8 , 16 } ; int Queries [ ] = { 2 , 3 , 1 } ; int Q = sizeof ( Queries ) / sizeof ( Queries [ 0 ] ) ; findSubtreeORUtil ( N , Edges , Val , Queries , Q ) ; return 0 ; }
Maximize sum of K elements selected from a Matrix such that each selected element must be preceded by selected row elements | C ++ program for the above approach ; Function to return the maximum of two elements ; Function to find the maximum sum of selecting K elements from the given 2D array arr [ ] [ ] ; dp table of size ( K + 1 ) * ( N + 1 ) ; Initialize dp [ 0 ] [ i ] = 0 ; Initialize dp [ i ] [ 0 ] = 0 ; Selecting i elements ; Select i elements till jth row ; sum = 0 , to keep track of cummulative elements sum ; Traverse arr [ j ] [ k ] until number of elements until k > i ; Select arr [ j ] [ k - 1 ] th item ; Store the maxSum in dp [ i ] [ j + 1 ] ; Return the maximum sum ; Driver Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; int max ( int a , int b ) { return a > b ? a : b ; } int maximumsum ( int arr [ ] [ 4 ] , int K , int N , int M ) { int sum = 0 , maxSum ; int i , j , k ; int dp [ K + 1 ] [ N + 1 ] ; for ( i = 0 ; i <= N ; i ++ ) dp [ 0 ] [ i ] = 0 ; for ( i = 0 ; i <= K ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( i = 1 ; i <= K ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { sum = 0 ; maxSum = dp [ i ] [ j ] ; for ( k = 1 ; k <= M && k <= i ; k ++ ) { sum += arr [ j ] [ k - 1 ] ; maxSum = max ( maxSum , sum + dp [ i - k ] [ j ] ) ; } dp [ i ] [ j + 1 ] = maxSum ; } } return dp [ K ] [ N ] ; } int main ( ) { int arr [ ] [ 4 ] = { { 10 , 10 , 100 , 30 } , { 80 , 50 , 10 , 50 } } ; int N = 2 , M = 4 ; int K = 5 ; cout << maximumsum ( arr , K , N , M ) ; return 0 ; }
Subsequences of given string consisting of non | C ++ program to implement the above approach ; Function to find all the subsequences of the string with non - repeating characters ; Base case ; Insert current subsequence ; If str [ i ] is not present in the current subsequence ; Insert str [ i ] into the set ; Insert str [ i ] into the current subsequence ; Remove str [ i ] from current subsequence ; Remove str [ i ] from the set ; Not including str [ i ] from the current subsequence ; Utility function to print all subsequences of string with non - repeating characters ; Stores all possible subsequences with non - repeating characters ; Stores subsequence with non - repeating characters ; Traverse all possible subsequences containing non - repeating characters ; Print subsequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FindSub ( set < string > & sub , set < char > & ch , string str , string res , int i ) { if ( i == str . length ( ) ) { sub . insert ( res ) ; return ; } if ( ! ch . count ( str [ i ] ) ) { ch . insert ( str [ i ] ) ; res . push_back ( str [ i ] ) ; FindSub ( sub , ch , str , res , i + 1 ) ; res . pop_back ( ) ; ch . erase ( str [ i ] ) ; } FindSub ( sub , ch , str , res , i + 1 ) ; } void printSubwithUniqueChar ( string str , int N ) { set < string > sub ; set < char > ch ; FindSub ( sub , ch , str , " " , 0 ) ; for ( auto subString : sub ) { cout << subString << " ▁ " ; } } int main ( ) { string str = " abac " ; int N = str . length ( ) ; printSubwithUniqueChar ( str , N ) ; return 0 ; }
Count lexicographically increasing K | C ++ program for the above approach ; Function to count K - length strings from first N alphabets ; To keep track of column sum in dp ; Auxiliary 2d dp array ; Initialize dp [ 0 ] [ i ] = 1 and update the column_sum ; Iterate for K times ; Iterate for N times ; dp [ i ] [ j ] : Stores the number of ways to form i - length strings consisting of j letters ; Update the column_sum ; Print number of ways to arrange K - length strings with N alphabets ; Driver Code ; Given N and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void waysToArrangeKLengthStrings ( int N , int K ) { int column_sum [ N + 1 ] = { 0 } , i , j ; int dp [ K + 1 ] [ N + 1 ] = { 0 } ; for ( i = 0 ; i <= N ; i ++ ) { dp [ 0 ] [ i ] = 1 ; column_sum [ i ] = 1 ; } for ( i = 1 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { dp [ i ] [ j ] += column_sum [ j - 1 ] ; column_sum [ j ] += dp [ i ] [ j ] ; } } cout << dp [ K ] [ N ] ; } int main ( ) { int N = 5 , K = 2 ; waysToArrangeKLengthStrings ( N , K ) ; return 0 ; }
Count N | C ++ program for the above approach ; Function to count N - length strings consisting of vowels only sorted lexicographically ; Stores count of strings consisting of vowels sorted lexicographically of all possible lengths ; Initialize DP [ 1 ] [ 1 ] ; Traverse the matrix row - wise ; Base Case ; Return the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumberOfStrings ( int n ) { vector < vector < int > > DP ( n + 1 , vector < int > ( 6 ) ) ; DP [ 1 ] [ 1 ] = 1 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < 6 ; j ++ ) { if ( i == 1 ) { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + 1 ; } else { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] + DP [ i - 1 ] [ j ] ; } } } return DP [ n ] [ 5 ] ; } int main ( ) { int N = 2 ; cout << findNumberOfStrings ( N ) ; return 0 ; }
Largest subtree sum for each vertex of given N | C ++ program for the above approach ; Function to perform the DFS Traversal on the given Tree ; To check if v is leaf vertex ; Initialize answer for vertex v ; Traverse adjacency list of v ; Update maximum subtree sum ; If v is leaf ; Function to calculate maximum subtree sum for each vertex ; Stores the adjacency list ; Add Edegs to the list ; Stores largest subtree sum for each vertex ; Calculate answer ; Print the result ; Driver Code ; Given nodes ; Give N edges ; Given values ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define V  3 NEW_LINE #define M  2 NEW_LINE void dfs ( int v , int p , vector < int > adj [ ] , int ans [ ] , int vals [ ] ) { bool isLeaf = 1 ; ans [ v ] = INT_MIN ; for ( int u : adj [ v ] ) { if ( u == p ) continue ; isLeaf = 0 ; dfs ( u , v , adj , ans , vals ) ; ans [ v ] = max ( ans [ u ] + vals [ v ] , max ( ans [ u ] , vals [ u ] ) ) ; } if ( isLeaf ) { ans [ v ] = vals [ v ] ; } } void printAnswer ( int n , int edges [ V ] [ M ] , int values [ ] ) { vector < int > adj [ n ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = edges [ i ] [ 0 ] - 1 ; int v = edges [ i ] [ 1 ] - 1 ; adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } int ans [ n ] ; dfs ( 0 , -1 , adj , ans , values ) ; for ( auto x : ans ) { cout << x << " ▁ " ; } } int main ( ) { int N = 4 ; int edges [ V ] [ M ] = { { 1 , 2 } , { 1 , 3 } , { 3 , 4 } } ; int values [ ] = { 1 , -1 , 0 , 1 } ; printAnswer ( N , edges , values ) ; }
Queries to count frequencies of a given character in a given range of indices | C ++ program for the above approach ; Function to print count of char y present in the range [ l , r ] ; Length of the string ; Stores the precomputed results ; Iterate the given string ; Increment dp [ i ] [ y - ' a ' ] by 1 ; Pre - compute ; Traverse each query ; Print the result for each query ; Driver Code ; Given string ; Given Queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void noOfChars ( string s , char queries [ ] [ 3 ] , int q ) { int n = s . length ( ) ; int dp [ n + 1 ] [ 26 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i + 1 ] [ s [ i ] - ' a ' ] ++ ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ i + 1 ] [ j ] += dp [ i ] [ j ] ; } } for ( int i = 0 ; i < q ; i ++ ) { int l = ( int ) queries [ i ] [ 0 ] ; int r = ( int ) queries [ i ] [ 1 ] ; int c = queries [ i ] [ 2 ] - ' a ' ; cout << dp [ r ] - dp [ l - 1 ] << " ▁ " ; } } int main ( ) { string S = " aabv " ; char queries [ 2 ] [ 3 ] = { { 1 , 2 , ' a ' } , { 2 , 3 , ' b ' } } ; noOfChars ( S , queries , 2 ) ; return 0 ; }
Minimum cost required to rearrange a given array to make it equal to another given array | C ++ program for the above approach ; Function to find length of the longest common subsequence ; Find position where element is to be inserted ; Return the length of LCS ; Function to find the minimum cost required to convert the sequence A exactly same as B ; Auxiliary array ; Stores positions of elements of A [ ] ; Initialize index array with - 1 ; Update the index array with index of corresponding elements of B ; Place only A 's array values  with its mapped values  into nums array ; Find LCS ; No of elements to be added in array A [ ] ; Stores minimum cost ; Print the minimum cost ; Driver Code ; Given array A [ ] ; Given C ; Size of arr A ; Size of arr B ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLCS ( int * nums , int N ) { int k = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int pos = lower_bound ( nums , nums + k , nums [ i ] ) - nums ; nums [ pos ] = nums [ i ] ; if ( k == pos ) { k = pos + 1 ; } } return k ; } int minimumCost ( int * A , int * B , int M , int N , int C ) { int nums [ 1000000 ] ; int index [ 1000000 ] ; memset ( index , -1 , sizeof ( index ) ) ; for ( int i = 0 ; i < N ; i ++ ) { index [ B [ i ] ] = i ; } int k = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( index [ A [ i ] ] != -1 ) { nums [ k ++ ] = index [ A [ i ] ] ; } } int lcs_length = findLCS ( nums , k ) ; int elements_to_be_added = N - lcs_length ; int min_cost = elements_to_be_added * C ; cout << min_cost ; } int main ( ) { int A [ ] = { 1 , 6 , 3 , 5 , 10 } ; int B [ ] = { 3 , 1 , 5 } ; int C = 2 ; int M = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int N = sizeof ( B ) / sizeof ( B [ 0 ] ) ; minimumCost ( A , B , M , N , C ) ; return 0 ; }
Maximize count of array elements required to obtain given sum | C ++ 14 program for the above approach ; Function that count the maximum number of elements to obtain sum V ; Stores the maximum number of elements required to obtain V ; Base Case ; Initialize all table values as Infinite ; Find the max arr required for all values from 1 to V ; Go through all arr smaller than i ; If current coin value is less than i ; Update table [ i ] ; Return the final count ; Driver Code ; Given array ; Given sum V ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxCount ( vector < int > arr , int m , int V ) { vector < int > table ( V + 1 ) ; table [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) table [ i ] = -1 ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr [ j ] <= i ) { int sub_res = table [ i - arr [ j ] ] ; if ( sub_res != -1 && sub_res + 1 > table [ i ] ) table [ i ] = sub_res + 1 ; } } } return table [ V ] ; } int main ( ) { vector < int > arr = { 25 , 10 , 5 } ; int m = arr . size ( ) ; int V = 30 ; cout << ( maxCount ( arr , m , V ) ) ; return 0 ; }
Maximize path sum from top | C ++ program to implement the above approach ; Function to get the maximum path sum from top - left cell to all other cells of the given matrix ; Store the maximum path sum ; Initialize the value of dp [ i ] [ j ] to 0. ; Base case ; Compute the value of dp [ i ] [ j ] using the recurrence relation ; Print maximum path sum from the top - left cell ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SZ  100 NEW_LINE void pathSum ( const int mat [ SZ ] [ SZ ] , int N , int M ) { int dp [ N ] [ M ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; } for ( int j = 1 ; j < M ; j ++ ) { dp [ 0 ] [ j ] = mat [ 0 ] [ j ] + dp [ 0 ] [ j - 1 ] ; } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { dp [ i ] [ j ] = mat [ i ] [ j ] + max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { cout << dp [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { int mat [ SZ ] [ SZ ] = { { 3 , 2 , 1 } , { 6 , 5 , 4 } , { 7 , 8 , 9 } } ; int N = 3 ; int M = 3 ; pathSum ( mat , N , M ) ; }
Length of Longest Palindrome Substring | C ++ program for the above approach ; Function to find the length of the longest palindromic substring ; Length of string str ; Stores the dp states ; Initialise table [ ] [ ] as false ; All substrings of length 1 are palindromes ; Check for sub - string of length 2 ; If adjacent character are same ; Update table [ i ] [ i + 1 ] ; Check for lengths greater than 2 k is length of substring ; Fix the starting index ; Ending index of substring of length k ; Check for palindromic substring str [ i , j ] ; Mark true ; Update the maximum length ; Return length of LPS ; Driver Code ; Given string str ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestPalSubstr ( string str ) { int n = str . size ( ) ; bool table [ n ] [ n ] ; memset ( table , 0 , sizeof ( table ) ) ; int maxLength = 1 ; for ( int i = 0 ; i < n ; ++ i ) table [ i ] [ i ] = true ; int start = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( str [ i ] == str [ i + 1 ] ) { table [ i ] [ i + 1 ] = true ; start = i ; maxLength = 2 ; } } for ( int k = 3 ; k <= n ; ++ k ) { for ( int i = 0 ; i < n - k + 1 ; ++ i ) { int j = i + k - 1 ; if ( table [ i + 1 ] [ j - 1 ] && str [ i ] == str [ j ] ) { table [ i ] [ j ] = true ; if ( k > maxLength ) { start = i ; maxLength = k ; } } } } return maxLength ; } int main ( ) { string str = " forgeeksskeegfor " ; cout << longestPalSubstr ( str ) ; return 0 ; }
Maximize sum of an Array by flipping sign of all elements of a single subarray | C ++ program for the above approach ; Function to find the maximum sum after flipping a subarray ; Stores the total sum of array ; Initialize the maximum sum ; Iterate over all possible subarrays ; Initialize sum of the subarray before flipping sign ; Initialize sum of subarray after flipping sign ; Calculate the sum of original subarray ; Subtract the original subarray sum and add the flipped subarray sum to the total sum ; Return the max_sum ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSumFlip ( int a [ ] , int n ) { int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) total_sum += a [ i ] ; int max_sum = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; int flip_sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += a [ j ] ; max_sum = max ( max_sum , total_sum - 2 * sum ) ; } } return max ( max_sum , total_sum ) ; } int main ( ) { int arr [ ] = { -2 , 3 , -1 , -4 , -2 } ; int N = sizeof ( arr ) / sizeof ( int ) ; cout << maxSumFlip ( arr , N ) ; }