codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find minimum absolute difference between adjacent array elements in given ranges | C ++ program for the above approach ; Structure for query range ; Function to find the minimum difference between adjacent array element over the given range [ L , R ] for Q Queries ; Find the sum of all queries ; Left and right boundaries of current range ; Print the sum of the current query range ; Function to find the minimum absolute difference of adjacent array elements for the given range ; Stores the absolute difference of adjacent elements ; Find the minimum difference of adjacent elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Query { int L , R ; } ; int MAX = 5000 ; void minDifference ( int arr [ ] , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; int ans = MAX ; for ( int i = L ; i < R ; i ++ ) { ans = min ( ans , arr [ i ] ) ; } cout << ans << ' ' ; } } void minimumDifference ( int arr [ ] , Query q [ ] , int N , int m ) { int diff [ N ] ; for ( int i = 0 ; i < N - 1 ; i ++ ) diff [ i ] = abs ( arr [ i ] - arr [ i + 1 ] ) ; minDifference ( diff , N - 1 , q , m ) ; } int main ( ) { int arr [ ] = { 2 , 6 , 1 , 8 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Query Q [ ] = { { 0 , 3 } , { 1 , 5 } , { 4 , 5 } } ; int M = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ; minimumDifference ( arr , Q , N , M ) ; return 0 ; }
Count of N digit numbers which contains all single digit primes | C ++ program for the above approach ; Function ; If index = N + 1 ; Find count of distinct prime numbers by counting number of set bits . ; If count of distinct prime numbers is equal to 4 return 1. ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If N = 1 , 0 can be also placed . ; If the digit is a prime number , set the index of the digit to 1 in the bitmask . ; For remaining positions , any digit from [ 0 - 9 ] can be placed ; If the digit is a prime number , set the index of the digit to 1 in the bitmask . ; Return the answer . ; Driver Code ; Initializing dp array with - 1. ; Indexing prime numbers in ascending order ; Given Input ; Function call .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 1 << 4 ] ; map < int , int > primeIndex ; int countOfNumbers ( int index , int mask , int N ) { if ( index == N + 1 ) { int countOfPrimes = __builtin_popcount ( mask ) ; if ( countOfPrimes == 4 ) { return 1 ; } return 0 ; } int & val = dp [ index ] [ mask ] ; if ( val != -1 ) { return val ; } val = 0 ; if ( index == 1 ) { for ( int digit = ( N == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { if ( primeIndex . find ( digit ) != primeIndex . end ( ) ) { val += countOfNumbers ( index + 1 , mask | ( 1 << primeIndex [ digit ] ) , N ) ; } else { val += countOfNumbers ( index + 1 , mask , N ) ; } } } else { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { if ( primeIndex . find ( digit ) != primeIndex . end ( ) ) { val += countOfNumbers ( index + 1 , mask | ( 1 << primeIndex [ digit ] ) , N ) ; } else { val += countOfNumbers ( index + 1 , mask , N ) ; } } } return val ; } int main ( ) { memset ( dp , -1 , sizeof dp ) ; primeIndex [ 2 ] = 0 ; primeIndex [ 3 ] = 1 ; primeIndex [ 5 ] = 2 ; primeIndex [ 7 ] = 3 ; int N = 4 ; cout << countOfNumbers ( 1 , 0 , N ) ; return 0 ; }
Count of N | C ++ program for the above approach ; Stores the dp - states ; Recursive Function to find number of N - digit numbers which has equal count of distinct odd & even digits ; If index is N + 1 ; Find the count of set bits in the evenMask ; Find the count of set bits in the oddMask ; If the count of set bits in both masks are equal then return 1 as they have equal number of distinct odd and even digits ; If the state has already been computed ; If N = 1 , 0 can be also placed ; If digit is odd ; Set the ( digit / 2 ) th bit of the oddMask ; Set the ( digit / 2 ) th bit of the number evenMask ; For remaining positions , any digit from [ 0 - 9 ] can be placed ; If digit is odd ; Set the ( digit / 2 ) th bit of oddMask ; Set the ( digit / 2 ) th bit of evenMask ; Return the answer ; Function to find number of N - digit numbers which has equal count of distinct odd and even digits ; Initialize dp array with - 1 ; Function Call ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 1 << 5 ] [ 1 << 5 ] ; int countOfNumbers ( int index , int evenMask , int oddMask , int N ) { if ( index == N + 1 ) { int countOfEvenDigits = __builtin_popcount ( evenMask ) ; int countOfOddDigits = __builtin_popcount ( oddMask ) ; if ( countOfOddDigits == countOfEvenDigits ) { return 1 ; } return 0 ; } int & val = dp [ index ] [ evenMask ] [ oddMask ] ; if ( val != -1 ) return val ; val = 0 ; if ( index == 1 ) { for ( int digit = ( N == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { if ( digit & 1 ) { val += countOfNumbers ( index + 1 , evenMask , oddMask | ( 1 << ( digit / 2 ) ) , N ) ; } else { val += countOfNumbers ( index + 1 , evenMask | ( 1 << ( digit / 2 ) ) , oddMask , N ) ; } } } else { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { if ( digit & 1 ) { val += countOfNumbers ( index + 1 , evenMask , oddMask | ( 1 << ( digit / 2 ) ) , N ) ; } else { val += countOfNumbers ( index + 1 , evenMask | ( 1 << ( digit / 2 ) ) , oddMask , N ) ; } } } return val ; } void countNDigitNumber ( int N ) { memset ( dp , -1 , sizeof dp ) ; cout << countOfNumbers ( 1 , 0 , 0 , N ) ; } int main ( ) { int N = 3 ; countNDigitNumber ( N ) ; return 0 ; }
Minimum number of flips or swaps of adjacent characters required to make two strings equal | C ++ program for the above approach ; Function to count the minimum number of operations required to make strings A and B equal ; Stores all dp - states ; Iterate over the range [ 1 , N ] ; If A [ i - 1 ] equals to B [ i - 1 ] ; Assign Dp [ i - 1 ] to Dp [ i ] ; Otherwise ; Update dp [ i ] ; If swapping is possible ; Update dp [ i ] ; Return the minimum number of steps required ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMinSteps ( string A , string B , int N ) { vector < int > dp ( N + 1 , 0 ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( A [ i - 1 ] == B [ i - 1 ] ) { dp [ i ] = dp [ i - 1 ] ; } else { dp [ i ] = dp [ i - 1 ] + 1 ; } if ( i >= 2 && A [ i - 2 ] == B [ i - 1 ] && A [ i - 1 ] == B [ i - 2 ] ) { dp [ i ] = min ( dp [ i ] , dp [ i - 2 ] + 1 ) ; } } return dp [ N ] ; } int main ( ) { string A = "0101" ; string B = "0011" ; int N = A . length ( ) ; cout << countMinSteps ( A , B , N ) ; return 0 ; }
Count of N | C ++ program for the above approach ; Function to find the number of N digit numbers such that at least one digit occurs more than once ; Base Case ; If repeated is true , then for remaining positions any digit can be placed ; If the current state has already been computed , then return it ; Stores the count of number for the current recursive calls ; If n = 1 , 0 can be also placed ; If a digit has occurred for the second time , then set repeated to 1 ; Otherwise ; For remaining positions any digit can be placed ; If a digit has occurred for the second time , then set repeated to 1 ; Return the resultant count for the current recursive call ; Function to count all the N - digit numbers having at least one digit 's occurrence more than once ; Initialize dp array with - 1 ; Function to count all possible number satisfying the given criteria ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 50 ] [ 1 << 10 ] [ 2 ] ; int countOfNumbers ( int digit , int mask , bool repeated , int n ) { if ( digit == n + 1 ) { if ( repeated == true ) { return 1 ; } return 0 ; } if ( repeated == true ) { return pow ( 10 , n - digit + 1 ) ; } int & val = dp [ digit ] [ mask ] [ repeated ] ; if ( val != -1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( int i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { if ( mask & ( 1 << i ) ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) ; } else { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) ; } } } else { for ( int i = 0 ; i <= 9 ; ++ i ) { if ( mask & ( 1 << i ) ) { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 1 , n ) ; } else { val += countOfNumbers ( digit + 1 , mask | ( 1 << i ) , 0 , n ) ; } } } return val ; } void countNDigitNumber ( int N ) { memset ( dp , -1 , sizeof dp ) ; cout << countOfNumbers ( 1 , 0 , 0 , N ) ; } int main ( ) { int N = 2 ; countNDigitNumber ( N ) ; return 0 ; }
Construct the largest number whose sum of cost of digits is K | C ++ program for the above approach ; Function to find the maximum number among the two numbers S and T ; If "0" exists in the string S ; If "0" exists in the string T ; Else return the maximum number formed ; Recursive function to find maximum number formed such that the sum of cost of digits of formed number is K ; Base Case ; Return the stored state ; Including the digit ( idx + 1 ) ; Excluding the digit ( idx + 1 ) ; Store the result and return ; Function to find the maximum number formed such that the sum of the cost digits in the formed number is K ; Stores all Dp - states ; Recursive Call ; Return the result ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; string getMaximum ( string & S , string & T ) { if ( S . find ( "0" ) != string :: npos ) return T ; if ( T . find ( "0" ) != string :: npos ) return S ; return ( S . length ( ) > T . length ( ) ? S : T ) ; } string recursion ( int arr [ ] , int idx , int N , int K , vector < vector < string > > & dp ) { if ( K == 0 ) { return " " ; } if ( K < 0 or idx == N ) { return "0" ; } if ( dp [ idx ] [ K ] != " - 1" ) return dp [ idx ] [ K ] ; string include = to_string ( idx + 1 ) + recursion ( arr , 0 , N , K - arr [ idx ] , dp ) ; string exclude = recursion ( arr , idx + 1 , N , K , dp ) ; return dp [ idx ] [ K ] = getMaximum ( include , exclude ) ; } string largestNumber ( int arr [ ] , int N , int K ) { vector < vector < string > > dp ( N + 1 , vector < string > ( K + 1 , " - 1" ) ) ; string ans = recursion ( arr , 0 , N , K , dp ) ; return ( ans == " " ? "0" : ans ) ; } int main ( ) { int arr [ ] = { 3 , 12 , 9 , 5 , 3 , 4 , 6 , 5 , 10 } ; int K = 14 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << largestNumber ( arr , N , K ) ; return 0 ; }
Count of N | C ++ program for the above approach ; Function to calculate count of ' N ' digit numbers such that bitwise AND of adjacent digits is 0. ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If n = 1 , 0 can be also placed . ; For remaining positions , any digit from [ 0 - 9 ] can be placed after checking the conditions . ; Check if bitwise AND of current digit and previous digit is 0. ; Return answer ; Driver code ; Initialize dp array with - 1. ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 10 ] ; int countOfNumbers ( int digit , int prev , int n ) { if ( digit == n + 1 ) { return 1 ; } int & val = dp [ digit ] [ prev ] ; if ( val != -1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( int i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , n ) ; } } else { for ( int i = 0 ; i <= 9 ; ++ i ) { if ( ( i & prev ) == 0 ) { val += countOfNumbers ( digit + 1 , i , n ) ; } } } return val ; } int main ( ) { memset ( dp , -1 , sizeof dp ) ; int N = 3 ; cout << countOfNumbers ( 1 , 0 , N ) << endl ; }
Maximum sum among all ( a x b ) submatrices for given Q queries | C ++ program for the above approach ; Function to preprcess input mat [ N ] [ M ] . This function mainly fills dp [ N ] [ M ] such that dp [ i ] [ j ] stores sum of elements from ( 0 , 0 ) to ( i , j ) ; Copy first row of mat [ ] [ ] to dp [ ] [ ] ; Do column wise sum ; Do row wise sum ; A O ( 1 ) time function to compute sum of submatrix between ( tli , tlj ) and ( rbi , rbj ) using dp [ ] [ ] which is built by the preprocess function ; Result is now sum of elements between ( 0 , 0 ) and ( rbi , rbj ) ; Remove elements between ( 0 , 0 ) and ( tli - 1 , rbj ) ; Remove elements between ( 0 , 0 ) and ( rbi , tlj - 1 ) ; Add dp [ tli - 1 ] [ tlj - 1 ] as elements between ( 0 , 0 ) and ( tli - 1 , tlj - 1 ) are subtracted twice ; Function to find the maximum sum among all ( a x b ) sub - matrices of the matrix ; Function call ; Run a loop for finding answer for all queries ; Driver Code ; Given input ; Function call ; Print answer for all queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void preProcess ( vector < vector < int > > & mat , vector < vector < int > > & dp , int n , int m ) { for ( int i = 0 ; i < m ; i ++ ) { dp [ 0 ] [ i ] = mat [ 0 ] [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + mat [ i ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j < m ; j ++ ) { dp [ i ] [ j ] += dp [ i ] [ j - 1 ] ; } } } int sumQuery ( vector < vector < int > > dp , int tli , int tlj , int rbi , int rbj ) { int res = dp [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - dp [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - dp [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + dp [ tli - 1 ] [ tlj - 1 ] ; return res ; } vector < int > maxSubMatrixSumQueries ( vector < vector < int > > mat , int n , int m , vector < vector < int > > queries , int q ) { vector < vector < int > > dp ( n , vector < int > ( m ) ) ; preProcess ( mat , dp , n , m ) ; vector < int > maxSum ( ( int ) queries . size ( ) ) ; for ( int qi = 0 ; qi < q ; qi ++ ) { for ( int i = 0 ; i < n - queries [ qi ] [ 0 ] + 1 ; i ++ ) { for ( int j = 0 ; j < m - queries [ qi ] [ 1 ] + 1 ; j ++ ) { maxSum [ qi ] = max ( maxSum [ qi ] , sumQuery ( dp , i , j , i + queries [ qi ] [ 0 ] - 1 , j + queries [ qi ] [ 1 ] - 1 ) ) ; } } } return maxSum ; } int main ( ) { int n = 3 , m = 4 ; vector < vector < int > > mat = { { 1 , 2 , 3 , 9 } , { 4 , 5 , 6 , 2 } , { 8 , 3 , 2 , 6 } } ; int Q = 3 ; vector < vector < int > > Queries = { { 1 , 1 } , { 2 , 2 } , { 3 , 3 } } ; vector < int > maxSum = maxSubMatrixSumQueries ( mat , n , m , Queries , Q ) ; for ( int i = 0 ; i < Q ; i ++ ) { cout << maxSum [ i ] << " ▁ " ; } }
Number of ways such that only K bars are visible from the left | C ++ implementation for the above approach ; dp array ; Function to calculate the number of permutations of N , where only K bars are visible from the left . ; If subproblem has already been calculated , return ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Driver code ; Input ; Initialize dp array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 1005 ] [ 1005 ] ; int KvisibleFromLeft ( int N , int K ) { if ( dp [ N ] [ K ] != -1 ) return dp [ N ] [ K ] ; if ( N == K ) return dp [ N ] [ K ] = 1 ; if ( K == 1 ) { int ans = 1 ; for ( int i = 1 ; i < N ; i ++ ) ans *= i ; return dp [ N ] [ K ] = ans ; } return dp [ N ] [ K ] = KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) ; } int main ( ) { int N = 5 , K = 2 ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << KvisibleFromLeft ( N , K ) << endl ; return 0 ; }
Number of distinct words of size N with at most K contiguous vowels | C ++ program for the above approach ; Power function to calculate long powers with mod ; Function for finding number of ways to create string with length N and atmost K contiguous vowels ; Array dp to store number of ways ; dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] . . dp [ i - 1 ] [ k ] ) * 21 ; Now setting sum to be dp [ i ] [ 0 ] ; If j > i , no ways are possible to create a string with length i and vowel j ; If j = i all the character should be vowel ; dp [ i ] [ j ] relation with dp [ i - 1 ] [ j - 1 ] ; Adding dp [ i ] [ j ] in the sum ; Driver Program ; Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int power ( long long int x , long long int y , long long int p ) { long long int res = 1ll ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } int kvowelwords ( int N , int K ) { long long int i , j ; long long int MOD = 1000000007 ; long long int dp [ N + 1 ] [ K + 1 ] = { 0 } ; long long int sum = 1 ; for ( i = 1 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = sum * 21 ; dp [ i ] [ 0 ] %= MOD ; sum = dp [ i ] [ 0 ] ; for ( j = 1 ; j <= K ; j ++ ) { if ( j > i ) dp [ i ] [ j ] = 0 ; else if ( j == i ) { dp [ i ] [ j ] = power ( 5ll , i , MOD ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] * 5 ; } dp [ i ] [ j ] %= MOD ; sum += dp [ i ] [ j ] ; sum %= MOD ; } } return sum ; } int main ( ) { int N = 3 ; int K = 3 ; cout << kvowelwords ( N , K ) << endl ; return 0 ; }
Maximize the length of upper boundary formed by placing given N rectangles horizontally or vertically | C ++ program for the above approach ; Function to find maximum length of the upper boundary formed by placing each of the rectangles either horizontally or vertically ; Stores the intermediate transition states ; Place the first rectangle horizontally ; Place the first rectangle vertically ; Place horizontally ; Stores the difference in height of current and previous rectangle ; Take maximum out of two options ; Place Vertically ; Stores the difference in height of current and previous rectangle ; Take maximum out two options ; Print maximum of horizontal or vertical alignment of the last rectangle ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxBoundary ( int N , vector < pair < int , int > > V ) { int dp [ N ] [ 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = V [ 0 ] . first ; dp [ 0 ] [ 1 ] = V [ 0 ] . second ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = V [ i ] . first ; int height1 = abs ( V [ i - 1 ] . second - V [ i ] . second ) ; int height2 = abs ( V [ i - 1 ] . first - V [ i ] . second ) ; dp [ i ] [ 0 ] += max ( height1 + dp [ i - 1 ] [ 0 ] , height2 + dp [ i - 1 ] [ 1 ] ) ; dp [ i ] [ 1 ] = V [ i ] . second ; int vertical1 = abs ( V [ i ] . first - V [ i - 1 ] . second ) ; int vertical2 = abs ( V [ i ] . first - V [ i - 1 ] . first ) ; dp [ i ] [ 1 ] += max ( vertical1 + dp [ i - 1 ] [ 0 ] , vertical2 + dp [ i - 1 ] [ 1 ] ) ; } cout << max ( dp [ N - 1 ] [ 0 ] , dp [ N - 1 ] [ 1 ] ) ; } int main ( ) { int N = 5 ; vector < pair < int , int > > V = { { 2 , 5 } , { 3 , 8 } , { 1 , 10 } , { 7 , 14 } , { 2 , 5 } } ; maxBoundary ( N , V ) ; return 0 ; }
Find maximum subset | Dp vector ; current element ; current element modulo D ; copy previous state ; Transitions ; return answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSum ( vector < int > A , int N , int K , int D ) { vector < vector < vector < int > > > dp ( N + 1 , vector < vector < int > > ( K + 1 , vector < int > ( D + 1 , -1 ) ) ) ; for ( int i = 1 ; i <= N ; i ++ ) { int element = A [ i - 1 ] ; int mod = A [ i - 1 ] % D ; dp [ i ] = dp [ i - 1 ] ; for ( int j = 1 ; j <= K ; j ++ ) { dp [ i ] [ j ] [ mod ] = max ( dp [ i ] [ j ] [ mod ] , element ) ; for ( int p = 0 ; p < D ; p ++ ) { if ( dp [ i - 1 ] [ j - 1 ] [ p ] != -1 ) { dp [ i ] [ j ] [ ( p + mod ) % D ] = max ( dp [ i ] [ j ] [ ( p + mod ) % D ] , dp [ i - 1 ] [ j - 1 ] [ p ] + element ) ; } } } } if ( dp [ N ] [ K ] [ 0 ] == -1 ) return 0 ; return dp [ N ] [ K ] [ 0 ] ; } int main ( ) { int N = 5 , K = 3 , D = 7 ; vector < int > A = { 1 , 11 , 5 , 5 , 18 } ; cout << maximumSum ( A , N , K , D ) << endl ; return 0 ; }
Ways to sum to N using Natural Numbers up to K with repetitions allowed | C ++ program for the above approach ; Function to find the total number of ways to represent N as the sum of integers over the range [ 1 , K ] ; Initialize a list ; Update dp [ 0 ] to 1 ; Iterate over the range [ 1 , K + 1 ] ; Iterate over the range [ 1 , N + 1 ] ; If col is greater than or equal to row ; Update current dp [ col ] state ; Return the total number of ways ; Driver Code ; Given inputs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int NumberOfways ( int N , int K ) { vector < int > dp ( N + 1 , 0 ) ; dp [ 0 ] = 1 ; for ( int row = 1 ; row < K + 1 ; row ++ ) { for ( int col = 1 ; col < N + 1 ; col ++ ) { if ( col >= row ) dp [ col ] = dp [ col ] + dp [ col - row ] ; } } return ( dp [ N ] ) ; } int main ( ) { int N = 8 ; int K = 2 ; cout << ( NumberOfways ( N , K ) ) ; }
Queries to check if array elements from indices [ L , R ] forms an Arithmetic Progression or not | C ++ program for the above approach ; Function to check if the given range of queries form an AP or not in the given array arr [ ] ; Stores length of the longest subarray forming AP for every array element ; Iterate over the range [ 0 , N ] ; Stores the index of the last element of forming AP ; Iterate until the element at index ( j , j + 1 ) forms AP ; Increment j by 1 ; Traverse the current subarray over the range [ i , j - 1 ] ; Update the length of the longest subarray at index k ; Update the value of i ; Traverse the given queries ; Print the result ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findAPSequence ( int arr [ ] , int N , int Q [ ] [ 2 ] , int M ) { int dp [ N + 5 ] = { 0 } ; for ( int i = 0 ; i + 1 < N ; ) { int j = i + 1 ; while ( j + 1 < N && arr [ j + 1 ] - arr [ j ] == arr [ i + 1 ] - arr [ i ] ) j ++ ; for ( int k = i ; k < j ; k ++ ) { dp [ k ] = j - k ; } i = j ; } for ( int i = 0 ; i < M ; i ++ ) { if ( dp [ Q [ i ] [ 0 ] ] >= Q [ i ] [ 1 ] - Q [ i ] [ 0 ] ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } } } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 7 , 6 , 5 , 4 , 1 } ; int Q [ ] [ 2 ] = { { 0 , 3 } , { 3 , 4 } , { 2 , 4 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ; findAPSequence ( arr , N , Q , M ) ; return 0 ; }
Minimize difference between sum of two K | C ++ program for the above approach ; Stores the values at recursive states ; Function to find the minimum difference between sum of two K - length subsets ; Base Case ; If k1 and k2 are 0 , then return the absolute difference between sum1 and sum2 ; Otherwise , return INT_MAX ; If the result is already computed , return the result ; Store the 3 options ; Including the element in first subset ; Including the element in second subset ; Not including the current element in both the subsets ; Store minimum of 3 values obtained ; Return the value for the current states ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 100 ] [ 100 ] ; int minSumDifference ( int * arr , int n , int k1 , int k2 , int sum1 , int sum2 ) { if ( n < 0 ) { if ( k1 == 0 && k2 == 0 ) { return abs ( sum1 - sum2 ) ; } else { return INT_MAX ; } } if ( dp [ n ] [ sum1 ] [ sum2 ] != -1 ) { return dp [ n ] [ sum1 ] [ sum2 ] ; } int op1 = INT_MAX ; int op2 = INT_MAX ; int op3 = INT_MAX ; if ( k1 > 0 ) { op1 = minSumDifference ( arr , n - 1 , k1 - 1 , k2 , sum1 + arr [ n ] , sum2 ) ; } if ( k2 > 0 ) { op2 = minSumDifference ( arr , n - 1 , k1 , k2 - 1 , sum1 , sum2 + arr [ n ] ) ; } op3 = minSumDifference ( arr , n - 1 , k1 , k2 , sum1 , sum2 ) ; dp [ n ] [ sum1 ] [ sum2 ] = min ( op1 , min ( op2 , op3 ) ) ; return dp [ n ] [ sum1 ] [ sum2 ] ; } int main ( ) { int arr [ ] = { 12 , 3 , 5 , 6 , 7 , 17 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << minSumDifference ( arr , N - 1 , K , K , 0 , 0 ) ; return 0 ; }