codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to divide an array into K equal sum sub | C ++ implementation of the approach ; Array to store the states of DP ; Array to check if a state has been solved before ; To store the sum of the array elements ; Function to find the sum of all the array elements ; Function to return the number of ways ; If sum is not divisible by k answer will be zero ; Base case ; To check if a state has been solved before ; Sum of all the numbers from the beginning of the array ; Setting the current state as solved ; Recurrence relation ; Returning solved state ; Driver code ; Function call to find the sum of the array elements ; Print the number of ways
#include <bits/stdc++.h> NEW_LINE #define max_size  20 NEW_LINE #define max_k  20 NEW_LINE using namespace std ; int dp [ max_size ] [ max_k ] ; bool v [ max_size ] [ max_k ] ; int sum = 0 ; void findSum ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; } int cntWays ( int arr [ ] , int i , int ck , int k , int n , int curr_sum ) { if ( sum % k != 0 ) return 0 ; if ( i != n and ck == k + 1 ) return 0 ; if ( i == n ) { if ( ck == k + 1 ) return 1 ; else return 0 ; } if ( v [ i ] [ ck ] ) return dp [ i ] [ ck ] ; curr_sum += arr [ i ] ; v [ i ] [ ck ] = 1 ; dp [ i ] [ ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ; if ( curr_sum == ( sum / k ) * ck ) dp [ i ] [ ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ; return dp [ i ] [ ck ] ; } int main ( ) { int arr [ ] = { 1 , -1 , 1 , -1 , 1 , -1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 2 ; findSum ( arr , n ) ; cout << cntWays ( arr , 0 , 1 , k , n , 0 ) ; }
Maximum sum in an array such that every element has exactly one adjacent element to it | C ++ implementation of the approach ; To store the states of dp ; Function to return the maximized sum ; Base case ; Checks if a state is already solved ; Recurrence relation ; Return the result ; Driver code
#include <bits/stdc++.h> NEW_LINE #define arrSize  51 NEW_LINE using namespace std ; int dp [ arrSize ] ; bool v [ arrSize ] ; int sumMax ( int i , int arr [ ] , int n ) { if ( i >= n - 1 ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) ) ; return dp [ i ] ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << sumMax ( 0 , arr , n ) ; return 0 ; }
Maximum Sum Subsequence of length k | C ++ program to calculate the maximum sum of increasing subsequence of length k ; In the implementation dp [ n ] [ k ] represents maximum sum subsequence of length k and the subsequence is ending at index n . ; Initializing whole multidimensional dp array with value - 1 ; For each ith position increasing subsequence of length 1 is equal to that array ith value so initializing dp [ i ] [ 1 ] with that array value ; Starting from 1 st index as we have calculated for 0 th index . Computing optimized dp values in bottom - up manner ; check for increasing subsequence ; Proceed if value is pre calculated ; Check for all the subsequences ending at any j < i and try including element at index i in them for some length l . Update the maximum value for every length . ; The final result would be the maximum value of dp [ i ] [ k ] for all different i . ; When no subsequence of length k is possible sum would be considered zero ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxIncreasingSub ( int arr [ ] , int n , int k ) { int dp [ n ] [ k + 1 ] , ans = -1 ; memset ( dp , -1 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] [ 1 ] = arr [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { for ( int l = 1 ; l <= k - 1 ; l ++ ) { if ( dp [ j ] [ l ] != -1 ) { dp [ i ] [ l + 1 ] = max ( dp [ i ] [ l + 1 ] , dp [ j ] [ l ] + arr [ i ] ) ; } } } } } for ( int i = 0 ; i < n ; i ++ ) { if ( ans < dp [ i ] [ k ] ) ans = dp [ i ] [ k ] ; } return ( ans == -1 ) ? 0 : ans ; } int main ( ) { int n = 8 , k = 3 ; int arr [ n ] = { 8 , 5 , 9 , 10 , 5 , 6 , 21 , 8 } ; int ans = MaxIncreasingSub ( arr , n , k ) ; cout << ans << " STRNEWLINE " ; return 0 ; }
Generate all unique partitions of an integer | Set 2 | C ++ implementation of the approach ; Array to store the numbers used to form the required sum ; Function to print the array which contains the unique partitions which are used to form the required sum ; Function to find all the unique partitions remSum = remaining sum to form maxVal is the maximum number that can be used to make the partition ; If remSum == 0 that means the sum is achieved so print the array ; i will begin from maxVal which is the maximum value which can be used to form the sum ; Store the number used in forming sum gradually in the array ; Since i used the rest of partition cant have any number greater than i hence second parameter is i ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 200 ] ; int count = 0 ; void print ( int idx ) { for ( int i = 1 ; i < idx ; i ++ ) { cout << dp [ i ] << " ▁ " ; } cout << endl ; } void solve ( int remSum , int maxVal , int idx , int & count ) { if ( remSum == 0 ) { print ( idx ) ; count ++ ; return ; } for ( int i = maxVal ; i >= 1 ; i -- ) { if ( i > remSum ) { continue ; } else if ( i <= remSum ) { dp [ idx ] = i ; solve ( remSum - i , i , idx + 1 , count ) ; } } } int main ( ) { int n = 4 , count = 0 ; solve ( n , n , 1 , count ) ; return 0 ; }
Queries for bitwise OR in the given matrix | C ++ implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix - count for each row ; Finding column - wise prefix count ; Function to return the result for a query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; If count of variables with ith bit set is greater than 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE #define bitscount  32 NEW_LINE #define n  3 NEW_LINE using namespace std ; int prefix_count [ bitscount ] [ n ] [ n ] ; void findPrefixCount ( int arr [ ] [ n ] ) { for ( int i = 0 ; i < bitscount ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 ) ; for ( int k = 1 ; k < n ; k ++ ) { prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ] ; } } } for ( int i = 0 ; i < bitscount ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ] ; } int rangeOr ( int x1 , int y1 , int x2 , int y2 ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int p ; if ( x1 == 0 and y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] ; else if ( x1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] ; else if ( y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] ; else p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ; if ( p != 0 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } int main ( ) { int arr [ ] [ n ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; findPrefixCount ( arr ) ; int queries [ ] [ 4 ] = { { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 2 } } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) << endl ; return 0 ; }
Queries for bitwise AND in the given matrix | C ++ implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix - count for each row ; Finding column - wise prefix count ; Function to return the result for a query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; If count of variables whose ith bit is set equals to the total elements in the sub - matrix ; Driver code
#include <bits/stdc++.h> NEW_LINE #define bitscount  32 NEW_LINE #define n  3 NEW_LINE using namespace std ; int prefix_count [ bitscount ] [ n ] [ n ] ; void findPrefixCount ( int arr [ ] [ n ] ) { for ( int i = 0 ; i < bitscount ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 ) ; for ( int k = 1 ; k < n ; k ++ ) { prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ] ; } } } for ( int i = 0 ; i < bitscount ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ] ; } int rangeAnd ( int x1 , int y1 , int x2 , int y2 ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int p ; if ( x1 == 0 and y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] ; else if ( x1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] ; else if ( y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] ; else p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ; if ( p == ( x2 - x1 + 1 ) * ( y2 - y1 + 1 ) ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } int main ( ) { int arr [ ] [ n ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; findPrefixCount ( arr ) ; int queries [ ] [ 4 ] = { { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 2 } } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << rangeAnd ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) << endl ; return 0 ; }
Maximum sum such that no two elements are adjacent | Set 2 | C ++ program to implement above approach ; variable to store states of dp ; variable to check if a given state has been solved ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code
#include <bits/stdc++.h> NEW_LINE #define maxLen  10 NEW_LINE using namespace std ; int dp [ maxLen ] ; bool v [ maxLen ] ; int maxSum ( int arr [ ] , int i , int n ) { if ( i >= n ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = 1 ; dp [ i ] = max ( maxSum ( arr , i + 1 , n ) , arr [ i ] + maxSum ( arr , i + 2 , n ) ) ; return dp [ i ] ; } int main ( ) { int arr [ ] = { 12 , 9 , 7 , 33 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << maxSum ( arr , 0 , n ) ; return 0 ; }
Optimal Strategy for a Game | Set 2 | C ++ program to find out maximum value from a given sequence of coins ; For both of your choices , the opponent gives you total sum minus maximum of his value ; Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Compute sum of elements ; Initialize memoization table ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; int memo [ MAX ] [ MAX ] ; int oSRec ( int arr [ ] , int i , int j , int sum ) { if ( j == i + 1 ) return max ( arr [ i ] , arr [ j ] ) ; if ( memo [ i ] [ j ] != -1 ) return memo [ i ] [ j ] ; memo [ i ] [ j ] = max ( ( sum - oSRec ( arr , i + 1 , j , sum - arr [ i ] ) ) , ( sum - oSRec ( arr , i , j - 1 , sum - arr [ j ] ) ) ) ; return memo [ i ] [ j ] ; } int optimalStrategyOfGame ( int * arr , int n ) { int sum = 0 ; sum = accumulate ( arr , arr + n , sum ) ; memset ( memo , -1 , sizeof ( memo ) ) ; return oSRec ( arr , 0 , n - 1 , sum ) ; } int main ( ) { int arr1 [ ] = { 8 , 15 , 3 , 7 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; printf ( " % d STRNEWLINE " , optimalStrategyOfGame ( arr1 , n ) ) ; int arr2 [ ] = { 2 , 2 , 2 , 2 } ; n = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; printf ( " % d STRNEWLINE " , optimalStrategyOfGame ( arr2 , n ) ) ; int arr3 [ ] = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = sizeof ( arr3 ) / sizeof ( arr3 [ 0 ] ) ; printf ( " % d STRNEWLINE " , optimalStrategyOfGame ( arr3 , n ) ) ; return 0 ; }
Minimum cost to select K strictly increasing elements | C ++ program for the above approach ; Function to calculate min cost to choose k increasing elements ; If k elements are counted return 0 ; If all elements of array has been traversed then return MAX_VALUE ; To check if this is already calculated ; When i 'th elements  is not considered ; When the ith element is greater than previous element check if adding its cost makes total cost minimum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1005 ; const int K = 20 ; int n , k ; int dp [ N + 1 ] [ N + 1 ] [ K + 1 ] ; int minCost ( int i , int prev , int cnt , int ele [ ] , int cost [ ] ) { if ( cnt == k + 1 ) { return 0 ; } if ( i == n + 1 ) { return 1e5 ; } int & ans = dp [ i ] [ prev ] [ cnt ] ; if ( ans != -1 ) { return ans ; } ans = minCost ( i + 1 , prev , cnt , ele , cost ) ; if ( ele [ i ] > ele [ prev ] ) { ans = min ( ans , cost [ i ] + minCost ( i + 1 , i , cnt + 1 , ele , cost ) ) ; } return ans ; } int main ( ) { memset ( dp , -1 , sizeof ( dp ) ) ; n = 4 ; k = 2 ; int ele [ n + 1 ] = { 0 , 2 , 6 , 4 , 8 } ; int cost [ n + 1 ] = { 0 , 40 , 20 , 30 , 10 } ; int ans = minCost ( 1 , 0 , 1 , ele , cost ) ; if ( ans == 1e5 ) { ans = -1 ; } cout << ans << endl ; return 0 ; }
Maximize the subarray sum after multiplying all elements of any subarray with X | C ++ implementation of the approach ; Function to return the maximum sum ; Base case ; If already calculated ; If no elements have been chosen ; Do not choose any element and use Kadane 's algorithm by taking max ; Choose the sub - array and multiply x ; Choose the sub - array and multiply x ; End the sub - array multiplication ; No more multiplication ; Memoize and return the answer ; Function to get the maximum sum ; Initialize dp with - 1 ; Iterate from every position and find the maximum sum which is possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  5 NEW_LINE int func ( int idx , int cur , int a [ ] , int dp [ N ] [ 3 ] , int n , int x ) { if ( idx == n ) return 0 ; if ( dp [ idx ] [ cur ] != -1 ) return dp [ idx ] [ cur ] ; int ans = 0 ; if ( cur == 0 ) { ans = max ( ans , a [ idx ] + func ( idx + 1 , 0 , a , dp , n , x ) ) ; ans = max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; } else if ( cur == 1 ) { ans = max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; ans = max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; } else ans = max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; return dp [ idx ] [ cur ] = ans ; } int getMaximumSum ( int a [ ] , int n , int x ) { int dp [ n ] [ 3 ] ; memset ( dp , -1 , sizeof dp ) ; int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) maxi = max ( maxi , func ( i , 0 , a , dp , n , x ) ) ; return maxi ; } int main ( ) { int a [ ] = { -3 , 8 , -2 , 1 , -6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int x = -1 ; cout << getMaximumSum ( a , n , x ) ; return 0 ; }
Count pairs of non | C ++ implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to return the number of pairs ; Create the dp table initially ; Declare the left array ; Declare the right array ; Initially left [ 0 ] is 1 ; Count the number of palindrome pairs to the left ; Initially right most as 1 ; Count the number of palindrome pairs to the right ; Count the number of pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100 NEW_LINE void pre_process ( bool dp [ N ] [ N ] , string s ) { int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = false ; } for ( int j = 1 ; j <= n ; j ++ ) { for ( int i = 0 ; i <= n - j ; i ++ ) { if ( j <= 2 ) { if ( s [ i ] == s [ i + j - 1 ] ) dp [ i ] [ i + j - 1 ] = true ; } else if ( s [ i ] == s [ i + j - 1 ] ) dp [ i ] [ i + j - 1 ] = dp [ i + 1 ] [ i + j - 2 ] ; } } } int countPairs ( string s ) { bool dp [ N ] [ N ] ; pre_process ( dp , s ) ; int n = s . length ( ) ; int left [ n ] ; memset ( left , 0 , sizeof left ) ; int right [ n ] ; memset ( right , 0 , sizeof right ) ; left [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( dp [ j ] [ i ] == 1 ) left [ i ] ++ ; } } right [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { right [ i ] = right [ i + 1 ] ; for ( int j = n - 1 ; j >= i ; j -- ) { if ( dp [ i ] [ j ] == 1 ) right [ i ] ++ ; } } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) ans += left [ i ] * right [ i + 1 ] ; return ans ; } int main ( ) { string s = " abacaba " ; cout << countPairs ( s ) ; return 0 ; }
Queries to check if substring [ L ... R ] is palindrome or not | C ++ implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to answer every query in O ( 1 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100 NEW_LINE void pre_process ( bool dp [ N ] [ N ] , string s ) { int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = false ; } for ( int j = 1 ; j <= n ; j ++ ) { for ( int i = 0 ; i <= n - j ; i ++ ) { if ( j <= 2 ) { if ( s [ i ] == s [ i + j - 1 ] ) dp [ i ] [ i + j - 1 ] = true ; } else if ( s [ i ] == s [ i + j - 1 ] ) dp [ i ] [ i + j - 1 ] = dp [ i + 1 ] [ i + j - 2 ] ; } } } void answerQuery ( int l , int r , bool dp [ N ] [ N ] ) { if ( dp [ l ] [ r ] ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; } int main ( ) { string s = " abaaab " ; bool dp [ N ] [ N ] ; pre_process ( dp , s ) ; int queries [ ] [ 2 ] = { { 0 , 1 } , { 1 , 5 } } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) answerQuery ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , dp ) ; return 0 ; }
Length of the longest increasing subsequence such that no two adjacent elements are coprime | CPP program to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; Function to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; To store dp and d value ; To store required answer ; For all elements in the array ; Initially answer is one ; For all it 's divisors ; Update the dp value ; Update the divisor value ; Check for required answer ; Update divisor of a [ i ] ; Return required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE int LIS ( int a [ ] , int n ) { int dp [ N ] , d [ N ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { dp [ a [ i ] ] = 1 ; for ( int j = 2 ; j * j <= a [ i ] ; j ++ ) { if ( a [ i ] % j == 0 ) { dp [ a [ i ] ] = max ( dp [ a [ i ] ] , dp [ d [ j ] ] + 1 ) ; dp [ a [ i ] ] = max ( dp [ a [ i ] ] , dp [ d [ a [ i ] / j ] ] + 1 ) ; d [ j ] = a [ i ] ; d [ a [ i ] / j ] = a [ i ] ; } } ans = max ( ans , dp [ a [ i ] ] ) ; d [ a [ i ] ] = a [ i ] ; } return ans ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << LIS ( a , n ) ; return 0 ; }
Find the number of binary strings of length N with at least 3 consecutive 1 s | C ++ implementation of the approach ; Function that returns true if s contains three consecutive 1 's ; Function to return the count of required strings ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string & s ) { int n = s . length ( ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( s [ i ] == '1' && s [ i - 1 ] == '1' && s [ i - 2 ] == '1' ) return 1 ; } return 0 ; } int countStr ( int i , string & s ) { if ( i < 0 ) { if ( check ( s ) ) return 1 ; return 0 ; } s [ i ] = '0' ; int ans = countStr ( i - 1 , s ) ; s [ i ] = '1' ; ans += countStr ( i - 1 , s ) ; s [ i ] = '0' ; return ans ; } int main ( ) { int N = 4 ; string s ( N , '0' ) ; cout << countStr ( N - 1 , s ) ; return 0 ; }
Find the number of binary strings of length N with at least 3 consecutive 1 s | C ++ implementation of the approach ; Function to return the count of required strings ; '0' at ith position ; '1' at ith position ; Driver code ; Base condition : 2 ^ ( i + 1 ) because of 0 indexing
#include <bits/stdc++.h> NEW_LINE using namespace std ; int n ; int solve ( int i , int x , int dp [ ] [ 4 ] ) { if ( i < 0 ) return x == 3 ; if ( dp [ i ] [ x ] != -1 ) return dp [ i ] [ x ] ; dp [ i ] [ x ] = solve ( i - 1 , 0 , dp ) ; dp [ i ] [ x ] += solve ( i - 1 , x + 1 , dp ) ; return dp [ i ] [ x ] ; } int main ( ) { n = 4 ; int dp [ n ] [ 4 ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < 4 ; j ++ ) dp [ i ] [ j ] = -1 ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] [ 3 ] = ( 1 << ( i + 1 ) ) ; } cout << solve ( n - 1 , 0 , dp ) ; return 0 ; }
Find the sum of the diagonal elements of the given N X N spiral matrix | C ++ implementation of the approach ; Function to return the sum of both the diagonal elements of the required matrix ; Array to store sum of diagonal elements ; Base cases ; Computing the value of dp ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n ) { int dp [ n + 1 ] ; dp [ 1 ] = 1 ; dp [ 0 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( 4 * ( i * i ) ) - 6 * ( i - 1 ) + dp [ i - 2 ] ; } return dp [ n ] ; } int main ( ) { int n = 4 ; cout << findSum ( n ) ; return 0 ; }
Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming | C ++ program to find maximum sum of a subset of nodes that are not adjacent ; Function to find the diameter of the tree using Dynamic Programming ; Traverse for all children of node ; Call DFS function again ; Include the current node then donot include the children ; Donot include current node , then include children or not include them ; Recurrence value ; Driver program to test above functions ; Constructed tree is 1 / \ 2 3 / \ 4 5 ; create undirected edges ; Numbers to node ; Find maximum sum by calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dfs ( int node , int parent , int dp1 [ ] , int dp2 [ ] , list < int > * adj , int tree [ ] ) { int sum1 = 0 , sum2 = 0 ; for ( auto i = adj [ node ] . begin ( ) ; i != adj [ node ] . end ( ) ; ++ i ) { if ( * i == parent ) continue ; dfs ( * i , node , dp1 , dp2 , adj , tree ) ; sum1 += dp2 [ * i ] ; sum2 += max ( dp1 [ * i ] , dp2 [ * i ] ) ; } dp1 [ node ] = tree [ node ] + sum1 ; dp2 [ node ] = sum2 ; } int main ( ) { int n = 5 ; list < int > * adj = new list < int > [ n + 1 ] ; adj [ 1 ] . push_back ( 2 ) ; adj [ 2 ] . push_back ( 1 ) ; adj [ 1 ] . push_back ( 3 ) ; adj [ 3 ] . push_back ( 1 ) ; adj [ 2 ] . push_back ( 4 ) ; adj [ 4 ] . push_back ( 2 ) ; adj [ 2 ] . push_back ( 5 ) ; adj [ 5 ] . push_back ( 2 ) ; int tree [ n + 1 ] ; tree [ 1 ] = 10 ; tree [ 2 ] = 5 ; tree [ 3 ] = 11 ; tree [ 4 ] = 6 ; tree [ 5 ] = 8 ; int dp1 [ n + 1 ] , dp2 [ n + 1 ] ; memset ( dp1 , 0 , sizeof dp1 ) ; memset ( dp2 , 0 , sizeof dp2 ) ; dfs ( 1 , 1 , dp1 , dp2 , adj , tree ) ; cout << " Maximum ▁ sum : ▁ " << max ( dp1 [ 1 ] , dp2 [ 1 ] ) << endl ; return 0 ; }