codeparrot/xlcost-text-to-code huggingface dataset:
Find the length of the Largest subset such that all elements are Pairwise Coprime | C ++ implementation to Find the length of the Largest subset such that all elements are Pairwise Coprime ; Dynamic programming table ; Function to obtain the mask for any integer ; List of prime numbers till 50 ; Iterate through all prime numbers to obtain the mask ; Set this prime 's bit ON in the mask ; Return the mask value ; Function to count the number of ways ; Check if subproblem has been solved ; Excluding current element in the subset ; Check if there are no common prime factors then only this element can be included ; Calculate the new mask if this element is included ; Store and return the answer ; Function to find the count of subarray with all digits unique ; Initializing dp ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 5000 ] [ ( 1 << 10 ) + 5 ] ; int getmask ( int val ) { int mask = 0 ; int prime [ 15 ] = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 } ; for ( int i = 0 ; i < 15 ; i ++ ) { if ( val % prime [ i ] == 0 ) { mask = mask | ( 1 << i ) ; } } return mask ; } int calculate ( int pos , int mask , int a [ ] , int n ) { if ( pos == n || mask == ( 1 << n - 1 ) ) return 0 ; if ( dp [ pos ] [ mask ] != -1 ) return dp [ pos ] [ mask ] ; int size = 0 ; size = max ( size , calculate ( pos + 1 , mask , a , n ) ) ; if ( ( getmask ( a [ pos ] ) & mask ) == 0 ) { int new_mask = ( mask | ( getmask ( a [ pos ] ) ) ) ; size = max ( size , 1 + calculate ( pos + 1 , new_mask , a , n ) ) ; } return dp [ pos ] [ mask ] = size ; } int largestSubset ( int a [ ] , int n ) { memset ( dp , -1 , sizeof ( dp ) ) ; return calculate ( 0 , 0 , a , n ) ; } int main ( ) { int A [ ] = { 2 , 3 , 13 , 5 , 14 , 6 , 7 , 11 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << largestSubset ( A , N ) ; return 0 ; }
Maximum sum such that exactly half of the elements are selected and no two adjacent | C ++ program to find maximum sum possible such that exactly floor ( N / 2 ) elements are selected and no two selected elements are adjacent to each other ; Function return the maximum sum possible under given condition ; Base case ; When i is odd ; When i is even ; Maximum of if we pick last element or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaximumSum ( int a [ ] , int n ) { int dp [ n + 1 ] [ 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 2 ] [ 1 ] = a [ 1 ] ; dp [ 2 ] [ 0 ] = a [ 0 ] ; for ( int i = 3 ; i < n + 1 ; i ++ ) { if ( i & 1 ) { int temp = max ( { dp [ i - 3 ] [ 1 ] , dp [ i - 3 ] [ 0 ] , dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] } ) ; dp [ i ] [ 1 ] = a [ i - 1 ] + temp ; dp [ i ] [ 0 ] = max ( { a [ i - 2 ] + dp [ i - 2 ] [ 0 ] , a [ i - 2 ] + dp [ i - 3 ] [ 1 ] , a [ i - 2 ] + dp [ i - 3 ] [ 0 ] , a [ i - 3 ] + dp [ i - 3 ] [ 0 ] } ) ; } else { dp [ i ] [ 1 ] = a [ i - 1 ] + max ( { dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] , dp [ i - 1 ] [ 0 ] } ) ; dp [ i ] [ 0 ] = a [ i - 2 ] + dp [ i - 2 ] [ 0 ] ; } } return max ( dp [ n ] [ 1 ] , dp [ n ] [ 0 ] ) ; } int main ( ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << MaximumSum ( A , N ) ; return 0 ; }
Optimal Strategy for the Divisor game using Dynamic Programming | C ++ program for implementation of Optimal Strategy for the Divisor Game using Dynamic Programming ; Recursive function to find the winner ; check if N = 1 or N = 3 then player B wins ; check if N = 2 then player A wins ; check if current state already visited then return the previously obtained ans ; check if currently it is player A 's turn  then initialise the ans to 0 ; Traversing across all the divisors of N which are less than N ; check if current value of i is a divisor of N ; check if it is player A 's turn  then we need at least one true ; Else if it is player B 's turn  then we need at least one false ; Return the current ans ; Driver code ; initialise N
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool divisorGame ( int N , bool A , int dp [ ] [ 2 ] ) { if ( N == 1 or N == 3 ) return false ; if ( N == 2 ) return true ; if ( dp [ N ] [ A ] != -1 ) return dp [ N ] [ A ] ; int ans = ( A == 1 ) ? 0 : 1 ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { if ( A ) ans |= divisorGame ( N - i , 0 , dp ) ; else ans &= divisorGame ( N - i , 1 , dp ) ; } } return dp [ N ] [ A ] = ans ; } int main ( ) { int N = 3 ; int dp [ N + 1 ] [ 2 ] ; memset ( dp , -1 , sizeof ( dp ) ) ; if ( divisorGame ( N , 1 , dp ) == true ) cout << " Player ▁ A ▁ wins " ; else cout << " Player ▁ B ▁ wins " ; return 0 ; }
Find the count of mountains in a given Matrix | C ++ program find the count of mountains in a given Matrix ; Function to count number of mountains in a given matrix of size n ; form another matrix with one extra layer of border elements . Border elements will contain INT_MIN value . ; For border elements , set value as INT_MIN ; For rest elements , just copy it into new matrix ; Check for mountains in the modified matrix ; check for all directions ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; int countMountains ( int a [ ] [ MAX ] , int n ) { int A [ n + 2 ] [ n + 2 ] ; int count = 0 ; for ( int i = 0 ; i < n + 2 ; i ++ ) { for ( int j = 0 ; j < n + 2 ; j ++ ) { if ( ( i == 0 ) || ( j == 0 ) || ( i == n + 1 ) || ( j == n + 1 ) ) { A [ i ] [ j ] = INT_MIN ; } else { A [ i ] [ j ] = a [ i - 1 ] [ j - 1 ] ; } } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( ( A [ i ] [ j ] > A [ i - 1 ] [ j ] ) && ( A [ i ] [ j ] > A [ i + 1 ] [ j ] ) && ( A [ i ] [ j ] > A [ i ] [ j - 1 ] ) && ( A [ i ] [ j ] > A [ i ] [ j + 1 ] ) && ( A [ i ] [ j ] > A [ i - 1 ] [ j - 1 ] ) && ( A [ i ] [ j ] > A [ i + 1 ] [ j + 1 ] ) && ( A [ i ] [ j ] > A [ i - 1 ] [ j + 1 ] ) && ( A [ i ] [ j ] > A [ i + 1 ] [ j - 1 ] ) ) { count ++ ; } } } return count ; } int main ( ) { int a [ ] [ MAX ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int n = 3 ; cout << countMountains ( a , n ) ; return 0 ; }
Minimum length of the reduced Array formed using given operations | C ++ implementation to find the minimum length of the array ; Function to find the length of minimized array ; Creating the required dp tables ; Initialising the dp table by - 1 ; base case ; Check if the two subarray can be combined ; Initialising dp1 table with max value ; Check if the subarray can be reduced to a single element ; Minimal partition of [ 1 : j - 1 ] + 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimalLength ( int a [ ] , int n ) { int dp [ n + 1 ] [ n + 1 ] , dp1 [ n ] ; int i , j , k ; memset ( dp , -1 , sizeof ( dp ) ) ; for ( int size = 1 ; size <= n ; size ++ ) { for ( i = 0 ; i < n - size + 1 ; i ++ ) { j = i + size - 1 ; if ( i == j ) dp [ i ] [ j ] = a [ i ] ; else { for ( k = i ; k < j ; k ++ ) { if ( dp [ i ] [ k ] != -1 && dp [ i ] [ k ] == dp [ k + 1 ] [ j ] ) dp [ i ] [ j ] = dp [ i ] [ k ] + 1 ; } } } } for ( i = 0 ; i < n ; i ++ ) dp1 [ i ] = 1e7 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( dp [ j ] [ i ] != -1 ) { if ( j == 0 ) dp1 [ i ] = 1 ; else dp1 [ i ] = min ( dp1 [ i ] , dp1 [ j - 1 ] + 1 ) ; } } } return dp1 [ n - 1 ] ; } int main ( ) { int n = 7 ; int a [ n ] = { 3 , 3 , 4 , 4 , 4 , 3 , 3 } ; cout << minimalLength ( a , n ) ; return 0 ; }
Maximum score possible after performing given operations on an Array | C ++ program to find the maximum Score after given operations ; Memoizing by the use of a table ; Function to calculate maximum score ; Bse case ; If the same state has already been computed ; Sum of array in range ( l , r ) ; If the operation is even - numbered the score is decremented ; Exploring all paths , and storing maximum value in DP table to avoid further repetitive recursive calls ; Function to find the max score ; Prefix sum array ; Calculating prefix_sum ; Initialising the DP table , - 1 represents the subproblem hasn 't been solved yet ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 100 ] [ 100 ] ; int MaximumScoreDP ( int l , int r , int prefix_sum [ ] , int num ) { if ( l > r ) return 0 ; if ( dp [ l ] [ r ] [ num ] != -1 ) return dp [ l ] [ r ] [ num ] ; int current_sum = prefix_sum [ r ] - ( l - 1 >= 0 ? prefix_sum [ l - 1 ] : 0 ) ; if ( num % 2 == 0 ) current_sum *= -1 ; dp [ l ] [ r ] [ num ] = current_sum + max ( MaximumScoreDP ( l + 1 , r , prefix_sum , num + 1 ) , MaximumScoreDP ( l , r - 1 , prefix_sum , num + 1 ) ) ; return dp [ l ] [ r ] [ num ] ; } int findMaxScore ( int a [ ] , int n ) { int prefix_sum [ n ] = { 0 } ; prefix_sum [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; } memset ( dp , -1 , sizeof ( dp ) ) ; return MaximumScoreDP ( 0 , n - 1 , prefix_sum , 1 ) ; } int main ( ) { int n = 6 ; int A [ n ] = { 1 , 2 , 3 , 4 , 2 , 6 } ; cout << findMaxScore ( A , n ) ; return 0 ; }
Count number of binary strings without consecutive 1 â €™ s : Set 2 | C ++ program to count number of binary strings without consecutive 1 aTMs ; Table to store the solution of every sub problem ; Here , pos : keeps track of current position . f1 : is the flag to check if current number is less than N or not . pr : represents the previous digit ; Base case ; Check if this subproblem has already been solved ; Placing 0 at the current position as it does not violate the condition ; Here flag will be 1 for the next recursive call ; Placing 1 at this position only if the previously inserted number is 0 ; If the number is smaller than N ; If the digit at current position is 1 ; Storing the solution to this subproblem ; Function to find the number of integers less than or equal to N with no consecutive 1 aTMs in binary representation ; Convert N to binary form ; Loop to convert N from Decimal to binary ; Initialising the table with - 1. ; Calling the function ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int memo [ 32 ] [ 2 ] [ 2 ] ; int dp ( int pos , int fl , int pr , string & bin ) { if ( pos == bin . length ( ) ) return 1 ; if ( memo [ pos ] [ fl ] [ pr ] != -1 ) return memo [ pos ] [ fl ] [ pr ] ; int val = 0 ; if ( bin [ pos ] == '0' ) val = val + dp ( pos + 1 , fl , 0 , bin ) ; else if ( bin [ pos ] == '1' ) val = val + dp ( pos + 1 , 1 , 0 , bin ) ; if ( pr == 0 ) { if ( fl == 1 ) { val += dp ( pos + 1 , fl , 1 , bin ) ; } else if ( bin [ pos ] == '1' ) { val += dp ( pos + 1 , fl , 1 , bin ) ; } } return memo [ pos ] [ fl ] [ pr ] = val ; } int findIntegers ( int num ) { string bin ; while ( num > 0 ) { if ( num % 2 ) bin += "1" ; else bin += "0" ; num /= 2 ; } reverse ( bin . begin ( ) , bin . end ( ) ) ; memset ( memo , -1 , sizeof ( memo ) ) ; return dp ( 0 , 0 , 0 , bin ) ; } int main ( ) { int N = 12 ; cout << findIntegers ( N ) ; return 0 ; }
Count ways to reach end from start stone with at most K jumps at each step | C ++ program to find total no . of ways to reach nth step ; Function which returns total no . of ways to reach nth step from sth steps ; Initialize dp array ; filling all the elements with 0 ; Initialize ( s - 1 ) th index to 1 ; Iterate a loop from s to n ; starting range for counting ranges ; Calculate Maximum moves to Reach ith step ; For nth step return dp [ n - 1 ] ; Driver Code ; no of steps ; Atmost steps allowed ; starting range
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int TotalWays ( int n , int s , int k ) { int dp [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ s - 1 ] = 1 ; for ( int i = s ; i < n ; i ++ ) { int idx = max ( s - 1 , i - k ) ; for ( int j = idx ; j < i ; j ++ ) { dp [ i ] += dp [ j ] ; } } return dp [ n - 1 ] ; } int main ( ) { int n = 5 ; int k = 2 ; int s = 2 ; cout << " Total ▁ Ways ▁ = ▁ " << TotalWays ( n , s , k ) ; }
Maximum subsequence sum with adjacent elements having atleast K difference in index | C ++ implementation to find the maximum sum subsequence such that two adjacent element have atleast difference of K in their indices ; Function to find the maximum sum subsequence such that two adjacent element have atleast difference of K in their indices ; DP Array to store the maximum sum obtained till now ; Either select the first element or Nothing ; Either Select the ( i - 1 ) element or let the previous best answer be the current best answer ; Either select the best sum till previous_index or select the current element + best_sum till index - k ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_sum ( int arr [ ] , int n , int k ) { int dp [ n ] ; dp [ 0 ] = maxi ( 0 , arr [ 0 ] ) ; int i = 1 ; while ( i < k ) { dp [ i ] = maxi ( dp [ i - 1 ] , arr [ i ] ) ; i ++ ; } i = k ; while ( i < n ) { dp [ i ] = maxi ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ; i ++ ; } return dp [ n - 1 ] ; } int main ( ) { int arr [ ] = { 1 , 2 , -2 , 4 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 4 ; cout << max_sum ( arr , n , k ) ; return 0 ; }
Number of binary strings such that there is no substring of length â ‰¥ 3 | C ++ implementation of the approach ; Function to return the count of all possible binary strings ; Fill 0 's in the dp array ; Base cases ; dp [ i ] [ j ] is the number of possible strings such that '1' just appeared consecutively j times upto ith index ; Taking previously calculated value ; Taking all the possible cases that can appear at the Nth position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const long MOD = 1000000007 ; long countStr ( long N ) { long dp [ N + 1 ] [ 3 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = 1 ; dp [ 1 ] [ 2 ] = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] + dp [ i - 1 ] [ 2 ] ) % MOD ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] % MOD ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 1 ] % MOD ; } long ans = ( dp [ N ] [ 0 ] + dp [ N ] [ 1 ] + dp [ N ] [ 2 ] ) % MOD ; return ans ; } int main ( ) { long N = 8 ; cout << countStr ( N ) ; return 0 ; }
Count number of ways to get Odd Sum | C ++ implementation ; Count the ways to sum up with odd by choosing one element form each pair ; Initialize two array with 0 ; if element is even ; store count of even number in i 'th pair ; if the element is odd ; store count of odd number in i 'th pair ; Initial state of dp array ; dp [ i ] [ 0 ] = total number of ways to get even sum upto i 'th pair ; dp [ i ] [ 1 ] = total number of ways to odd even sum upto i 'th pair ; dp [ n - 1 ] [ 1 ] = total number of ways to get odd sum upto n 'th pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountOfOddSum ( int a [ ] [ 2 ] , int n ) { int dp [ n ] [ 2 ] , cnt [ n ] [ 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( a [ i ] [ j ] % 2 == 0 ) { cnt [ i ] [ 0 ] ++ ; } else { cnt [ i ] [ 1 ] ++ ; } } } dp [ 0 ] [ 0 ] = cnt [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] = cnt [ 0 ] [ 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 0 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 1 ] ) ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 1 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 0 ] ) ; } return dp [ n - 1 ] [ 1 ] ; } int main ( ) { int a [ ] [ 2 ] = { { 1 , 2 } , { 3 , 6 } } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int ans = CountOfOddSum ( a , n ) ; cout << ans << " STRNEWLINE " ; return 0 ; }
Longest Consecuetive Subsequence when only one insert operation is allowed | C ++ implementation of above approach ; Function to return the length of longest consecuetive subsequence after inserting an element ; Variable to find maximum value of the array ; Calculating maximum value of the array ; Declaring the DP table ; Variable to store the maximum length ; Iterating for every value present in the array ; Recurrence for dp [ val ] [ 0 ] ; No value can be inserted before 1 , hence the element value should be greater than 1 for this recurrance relation ; Recurrence for dp [ val ] [ 1 ] ; Maximum length of consecutive sequence ending at 1 is equal to 1 ; Update the ans variable with the new maximum length possible ; Return the ans ; Driver code ; Input array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LongestConsSeq ( int arr [ ] , int N ) { int maxval = 1 ; for ( int i = 0 ; i < N ; i += 1 ) { maxval = max ( maxval , arr [ i ] ) ; } int dp [ maxval + 1 ] [ 2 ] = { 0 } ; int ans = 1 ; for ( int i = 0 ; i < N ; i += 1 ) { dp [ arr [ i ] ] [ 0 ] = ( 1 + dp [ arr [ i ] - 1 ] [ 0 ] ) ; if ( arr [ i ] >= 2 ) dp [ arr [ i ] ] [ 1 ] = max ( 1 + dp [ arr [ i ] - 1 ] [ 1 ] , 2 + dp [ arr [ i ] - 2 ] [ 0 ] ) ; else dp [ arr [ i ] ] [ 1 ] = 1 ; ans = max ( ans , dp [ arr [ i ] ] [ 1 ] ) ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 1 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << LongestConsSeq ( arr , N ) ; return 0 ; }
Stepping Numbers | A C ++ program to find all the Stepping Number in [ n , m ] ; This function checks if an integer n is a Stepping Number ; Initalize prevDigit with - 1 ; Iterate through all digits of n and compare difference between value of previous and current digits ; Get Current digit ; Single digit is consider as a Stepping Number ; Check if absolute difference between prev digit and current digit is 1 ; A brute force approach based function to find all stepping numbers . ; Iterate through all the numbers from [ N , M ] and check if its a stepping number . ; Driver program to test above function ; Display Stepping Numbers in the range [ n , m ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isStepNum ( int n ) { int prevDigit = -1 ; while ( n ) { int curDigit = n % 10 ; if ( prevDigit == -1 ) prevDigit = curDigit ; else { if ( abs ( prevDigit - curDigit ) != 1 ) return false ; } prevDigit = curDigit ; n /= 10 ; } return true ; } void displaySteppingNumbers ( int n , int m ) { for ( int i = n ; i <= m ; i ++ ) if ( isStepNum ( i ) ) cout << i << " ▁ " ; } int main ( ) { int n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ; return 0 ; }
Count numbers in given range such that sum of even digits is greater than sum of odd digits | C ++ code to count number in the range having the sum of even digits greater than the sum of odd digits ; Base Case ; check if condition satisfied or not ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 0 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; if current digit is odd ; if current digit is even ; Function to convert n into its digit vector and uses memo ( ) function to return the required count ; Initialize DP ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define int  long long NEW_LINE using namespace std ; vector < int > v ; int dp [ 18 ] [ 180 ] [ 180 ] [ 2 ] ; int memo ( int index , int evenSum , int oddSum , int tight ) { if ( index == v . size ( ) ) { if ( evenSum > oddSum ) return 1 ; else return 0 ; } if ( dp [ index ] [ evenSum ] [ oddSum ] [ tight ] != -1 ) return dp [ index ] [ evenSum ] [ oddSum ] [ tight ] ; int limit = ( tight ) ? v [ index ] : 9 ; int ans = 0 ; for ( int d = 0 ; d <= limit ; d ++ ) { int currTight = 0 ; if ( d == v [ index ] ) currTight = tight ; if ( d % 2 != 0 ) ans += memo ( index + 1 , evenSum , oddSum + d , currTight ) ; else ans += memo ( index + 1 , evenSum + d , oddSum , currTight ) ; } dp [ index ] [ evenSum ] [ oddSum ] [ tight ] = ans ; return ans ; } int CountNum ( int n ) { v . clear ( ) ; while ( n ) { v . push_back ( n % 10 ) ; n = n / 10 ; } reverse ( v . begin ( ) , v . end ( ) ) ; memset ( dp , -1 , sizeof ( dp ) ) ; return memo ( 0 , 0 , 0 , 1 ) ; } int32_t main ( ) { int L , R ; L = 2 ; R = 10 ; cout << CountNum ( R ) - CountNum ( L - 1 ) << " STRNEWLINE " ; return 0 ; }
Maximum sub | C ++ implementation of the approach ; Function to return the maximum sum of the sub - sequence such that two consecutive elements have a difference of at least 3 in their indices in the given array ; If there is a single element in the array ; Either select it or don 't ; If there are two elements ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select first , second , third or nothing ; For the rest of the elements ; Either select the best sum till previous_index or select the current element + best_sum till index - 3 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_sum ( int a [ ] , int n ) { int dp [ n ] ; if ( n == 1 ) { dp [ 0 ] = max ( 0 , a [ 0 ] ) ; } else if ( n == 2 ) { dp [ 0 ] = max ( 0 , a [ 0 ] ) ; dp [ 1 ] = max ( a [ 1 ] , dp [ 0 ] ) ; } else if ( n >= 3 ) { dp [ 0 ] = max ( 0 , a [ 0 ] ) ; dp [ 1 ] = max ( a [ 1 ] , max ( 0 , a [ 0 ] ) ) ; dp [ 2 ] = max ( a [ 2 ] , max ( a [ 1 ] , max ( 0 , a [ 0 ] ) ) ) ; int i = 3 ; while ( i < n ) { dp [ i ] = max ( dp [ i - 1 ] , a [ i ] + dp [ i - 3 ] ) ; i ++ ; } } return dp [ n - 1 ] ; } int main ( ) { int arr [ ] = { 1 , 2 , -2 , 4 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << max_sum ( arr , n ) ; return 0 ; }
Minimum count of elements that sums to a given number | C ++ implementation of the above approach ; we will only store min counts of sum upto 100 ; initialize with INT_MAX ; memo [ 0 ] = 0 as 0 is made from 0 elements ; fill memo array with min counts of elements that will constitute sum upto 100 ; min_count will store min count of elements chosen ; starting from end iterate over each 2 digits and add min count of elements to min_count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCount ( int K ) { int memo [ 100 ] ; for ( int i = 0 ; i < 100 ; i ++ ) { memo [ i ] = INT_MAX ; } memo [ 0 ] = 0 ; for ( int i = 1 ; i < 100 ; i ++ ) { memo [ i ] = min ( memo [ i - 1 ] + 1 , memo [ i ] ) ; } for ( int i = 10 ; i < 100 ; i ++ ) { memo [ i ] = min ( memo [ i - 10 ] + 1 , memo [ i ] ) ; } for ( int i = 25 ; i < 100 ; i ++ ) { memo [ i ] = min ( memo [ i - 25 ] + 1 , memo [ i ] ) ; } long min_count = 0 ; while ( K > 0 ) { min_count += memo [ K % 100 ] ; K /= 100 ; } return min_count ; } int main ( ) { int K = 69 ; cout << minCount ( K ) << endl ; return 0 ; }
Number of sub | C ++ implementation of the approach ; Function to return the number of subsequences which have at least one consecutive pair with difference less than or equal to 1 ; Not required sub - sequences which turn required on adding i ; Required sub - sequence till now will be required sequence plus sub - sequence which turns required ; Similarly for not required ; Also updating total required and not required sub - sequences ; Also , storing values in dp ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 10000 ; int count_required_sequence ( int n , int arr [ ] ) { int total_required_subsequence = 0 ; int total_n_required_subsequence = 0 ; int dp [ N ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { int turn_required = 0 ; for ( int j = -1 ; j <= 1 ; j ++ ) turn_required += dp [ arr [ i ] + j ] [ 0 ] ; int required_end_i = ( total_required_subsequence + turn_required ) ; int n_required_end_i = ( 1 + total_n_required_subsequence - turn_required ) ; total_required_subsequence += required_end_i ; total_n_required_subsequence += n_required_end_i ; dp [ arr [ i ] ] [ 1 ] += required_end_i ; dp [ arr [ i ] ] [ 0 ] += n_required_end_i ; } return total_required_subsequence ; } int main ( ) { int arr [ ] = { 1 , 6 , 2 , 1 , 9 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << count_required_sequence ( n , arr ) << " STRNEWLINE " ; return 0 ; }