codeparrot/xlcost-text-to-code huggingface dataset:
Minimum repeated addition of even divisors of N required to convert N to M | C ++ program for the above approach ; INF is the maximum value which indicates Impossible state ; Stores the dp states ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Check dp [ cur ] is already calculated or not ; Initially it is set to INF that meanswe cur can 't be transform to M ; Loop to find even divisors of cur ; if i is divisor of cur ; if i is even ; Find recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Finally store the current state result and return the answer ; Function that counts the minimum operation to reduce N to M ; Initialise dp state ; Function Call ; Driver Code ; Given N and M ; Function Call ; INF indicates impossible state
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int INF = 1e7 ; const int max_size = 100007 ; int dp [ max_size ] ; int min_op ( int cur , int M ) { if ( cur > M ) return INF ; if ( cur == M ) return 0 ; if ( dp [ cur ] != -1 ) return dp [ cur ] ; int op = INF ; for ( int i = 2 ; i * i <= cur ; i ++ ) { if ( cur % i == 0 ) { if ( i % 2 == 0 ) { op = min ( op , 1 + min_op ( cur + i , M ) ) ; } if ( ( cur / i ) != i && ( cur / i ) % 2 == 0 ) { op = min ( op , 1 + min_op ( cur + ( cur / i ) , M ) ) ; } } } return dp [ cur ] = op ; } int min_operations ( int N , int M ) { for ( int i = N ; i <= M ; i ++ ) { dp [ i ] = -1 ; } return min_op ( N , M ) ; } int main ( ) { int N = 6 , M = 24 ; int op = min_operations ( N , M ) ; if ( op >= INF ) cout << " - 1" ; else cout << op << " STRNEWLINE " ; return 0 ; }
Maximize Sum possible from an Array by the given moves | C ++ program to implement the above approach ; Function to find the maximum sum possible by given moves from the array ; Checking for boundary ; If previously computed subproblem occurs ; If element can be moved left ; Calculate maximum possible sum by moving left from current index ; If element can be moved right ; Calculate maximum possible sum by moving right from current index and update the maximum sum ; Store the maximum sum ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int k = 1 ; const int m = 4 ; int maxValue ( int a [ ] , int n , int pos , int moves , int left , int dp [ ] [ k + 1 ] ) { if ( moves == 0 || ( pos > n - 1 pos < 0 ) ) return 0 ; if ( dp [ pos ] [ left ] != -1 ) return dp [ pos ] [ left ] ; int value = 0 ; if ( left > 0 && pos >= 1 ) value = max ( value , a [ pos ] + maxValue ( a , n , pos - 1 , moves - 1 , left - 1 , dp ) ) ; if ( pos <= n - 1 ) value = max ( value , a [ pos ] + maxValue ( a , n , pos + 1 , moves - 1 , left , dp ) ) ; return dp [ pos ] [ left ] = value ; } int main ( ) { int n = 5 ; int a [ ] = { 1 , 5 , 4 , 3 , 2 } ; int dp [ n + 1 ] [ k + 1 ] ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << ( a [ 0 ] + maxValue ( a , n , 1 , m , k , dp ) ) << endl ; }
Maximize the Sum of a Subsequence from an Array based on given conditions | C ++ program to implement the above approach ; Function to select the array elements to maximize the sum of the selected elements ; If the entire array is solved ; Memoized subproblem ; Calculate sum considering the current element in the subsequence ; Calculate sum without considering the current element in the subsequence ; Update the maximum of the above sums in the dp [ ] [ ] table ; Driver Code ; Initialize the dp array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 6 ; int maximumSum ( int a [ ] , int count , int index , int n , int dp [ N ] [ N ] ) { if ( index == n ) return 0 ; if ( dp [ index ] [ count ] != -1 ) return dp [ index ] [ count ] ; int take_element = a [ index ] * count + maximumSum ( a , count + 1 , index + 1 , n , dp ) ; int dont_take = maximumSum ( a , count , index + 1 , n , dp ) ; return dp [ index ] [ count ] = max ( take_element , dont_take ) ; } int main ( ) { int n = 5 ; int a [ ] = { -1 , -9 , 0 , 5 , -7 } ; int dp [ N ] [ N ] ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << ( maximumSum ( a , 1 , 0 , n , dp ) ) ; }
Queries to find sum of distance of a given node to every leaf node in a Weighted Tree | C ++ program for the above problem ; MAX size ; graph with { destination , weight } ; ; for storing the sum for ith node ; leaves in subtree of ith . ; dfs to find sum of distance of leaves in the subtree of a node ; flag is the node is leaf or not ; ; skipping if parent ; setting flag to false ; doing dfs call ; doing calculation in postorder . ; if the node is leaf then we just increment the no . of leaves under the subtree of a node ; adding num of leaves ; calculating answer for the sum in the subtree ; dfs function to find the sum of distance of leaves outside the subtree ; number of leaves other than the leaves in the subtree of i ; adding the contribution of leaves outside to the ith node ; adding the leafs outside to ith node 's leaves. ; calculating the sum of distance of leaves in the subtree of a node assuming the root of the tree is 1 ; calculating the sum of distance of leaves outside the subtree of node assuming the root of the tree is 1 ; answering the queries ; ; Driver Code ; 1 ( 4 ) / \ ( 2 ) / \ 4 2 ( 5 ) / \ ( 3 ) / \ 5 3 ; initialising tree ; System . out . println ( v ) ;
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1e5 + 5 ; vector < vector < pair < int , int > > > v ( N ) ; vector < int > dp ( N ) ; vector < int > leaves ( N ) ; int n ; void dfs ( int a , int par ) { bool leaf = 1 ; for ( auto & i : v [ a ] ) { if ( i . first == par ) continue ; leaf = 0 ; dfs ( i . first , a ) ; } if ( leaf == 1 ) { leaves [ a ] += 1 ; } else { for ( auto & i : v [ a ] ) { if ( i . first == par ) continue ; leaves [ a ] += leaves [ i . first ] ; dp [ a ] = dp [ a ] + dp [ i . first ] + leaves [ i . first ] * i . second ; } } } void dfs2 ( int a , int par ) { for ( auto & i : v [ a ] ) { if ( i . first == par ) continue ; int leafOutside = leaves [ a ] - leaves [ i . first ] ; dp [ i . first ] += ( dp [ a ] - dp [ i . first ] ) ; dp [ i . first ] += i . second * ( leafOutside - leaves [ i . first ] ) ; leaves [ i . first ] += leafOutside ; dfs2 ( i . first , a ) ; } } void answerQueries ( vector < int > queries ) { dfs ( 1 , 0 ) ; dfs2 ( 1 , 0 ) ; for ( int i = 0 ; i < queries . size ( ) ; i ++ ) { cout << dp [ queries [ i ] ] << endl ; } } int main ( ) { n = 5 ; v [ 1 ] . push_back ( make_pair ( 4 , 4 ) ) ; v [ 4 ] . push_back ( make_pair ( 1 , 4 ) ) ; v [ 1 ] . push_back ( make_pair ( 2 , 2 ) ) ; v [ 2 ] . push_back ( make_pair ( 1 , 2 ) ) ; v [ 2 ] . push_back ( make_pair ( 3 , 3 ) ) ; v [ 3 ] . push_back ( make_pair ( 2 , 3 ) ) ; v [ 2 ] . push_back ( make_pair ( 5 , 5 ) ) ; v [ 5 ] . push_back ( make_pair ( 2 , 5 ) ) ; vector < int > queries = { 1 , 3 , 5 } ; answerQueries ( queries ) ; }
Minimum changes required to make each path in a matrix palindrome | C ++ Program to implement the above approach ; Function for counting changes ; Maximum distance possible is ( n - 1 + m - 1 ) ; Stores the maximum element ; Update the maximum ; Stores frequencies of values for respective distances ; Initialize frequencies of cells as 0 ; Count frequencies of cell values in the matrix ; Increment frequency of value at distance i + j ; Store the most frequent value at i - th distance from ( 0 , 0 ) and ( N - 1 , M - 1 ) ; Calculate max frequency and total cells at distance i ; Count changes required to convert all cells at i - th distance to most frequent value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  7 NEW_LINE int countChanges ( int matrix [ ] [ N ] , int n , int m ) { int dist = n + m - 1 ; int Max_element = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { Max_element = max ( Max_element , matrix [ i ] [ j ] ) ; } } int freq [ dist ] [ Max_element + 1 ] ; for ( int i = 0 ; i < dist ; i ++ ) { for ( int j = 0 ; j < Max_element + 1 ; j ++ ) freq [ i ] [ j ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { freq [ i + j ] [ matrix [ i ] [ j ] ] ++ ; } } int min_changes_sum = 0 ; for ( int i = 0 ; i < dist / 2 ; i ++ ) { int maximum = 0 ; int total_values = 0 ; for ( int j = 0 ; j < Max_element + 1 ; j ++ ) { maximum = max ( maximum , freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] ) ; total_values += freq [ i ] [ j ] + freq [ n + m - 2 - i ] [ j ] ; } min_changes_sum += total_values - maximum ; } return min_changes_sum ; } int main ( ) { int mat [ ] [ N ] = { { 7 , 0 , 3 , 1 , 8 , 1 , 3 } , { 0 , 4 , 0 , 1 , 0 , 4 , 0 } , { 3 , 1 , 8 , 3 , 1 , 0 , 7 } } ; int minChanges = countChanges ( mat , 3 , 7 ) ; cout << minChanges ; return 0 ; }
Count of numbers upto N having absolute difference of at most K between any two adjacent digits | C ++ program to get the count of numbers upto N having absolute difference at most K between any two adjacent digits ; Table to store solution of each subproblem ; Function to calculate all possible numbers ; Check if position reaches end that is is equal to length of N ; Check if the result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is false , means number has already become smaller so we can place any digit , otherwise N [ pos ] ; Chekc if start is false the number has not started yet ; Check if we do not start the number at pos then recur forward ; If we start the number we can place any digit from 1 to upper_limit ; Finding the new tight ; Condition if the number has already started ; We can place digit upto upperbound & absolute difference with previous digit much be atmost K ; Absolute difference atmost K ; Store the solution to this subproblem ; Driver code ; Initialising the table with - 1 ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long dp [ 1002 ] [ 10 ] [ 2 ] [ 2 ] ; long long possibleNumbers ( int pos , int previous , bool tight , bool start , string N , int K ) { if ( pos == N . length ( ) ) return 1 ; if ( dp [ pos ] [ previous ] [ tight ] [ start ] != -1 ) return dp [ pos ] [ previous ] [ tight ] [ start ] ; int res = 0 ; int upper_limit = ( tight ) ? ( N [ pos ] - '0' ) : 9 ; int new_tight ; if ( ! start ) { res = possibleNumbers ( pos + 1 , previous , false , false , N , K ) ; for ( int i = 1 ; i <= upper_limit ; i ++ ) { new_tight = ( tight && i == upper_limit ) ? 1 : 0 ; res += possibleNumbers ( pos + 1 , i , new_tight , true , N , K ) ; } } else { for ( int i = 0 ; i <= upper_limit ; i ++ ) { new_tight = ( tight && i == upper_limit ) ? 1 : 0 ; if ( abs ( i - previous ) <= K ) res += possibleNumbers ( pos + 1 , i , new_tight , true , N , K ) ; } } dp [ pos ] [ previous ] [ tight ] [ start ] = res ; return dp [ pos ] [ previous ] [ tight ] [ start ] ; } int main ( void ) { string N = "20" ; int K = 2 ; memset ( dp , -1 , sizeof dp ) ; cout << possibleNumbers ( 0 , 0 , true , false , N , K ) << endl ; }
Minimum cost of reducing Array by merging any adjacent elements repetitively | C ++ program for the above approach ; Function to find the total minimum cost of merging two consecutive numbers ; Find the size of numbers [ ] ; If array is empty , return 0 ; To store the prefix Sum of numbers array numbers [ ] ; Traverse numbers [ ] to find the prefix sum ; dp table to memoised the value ; For single numbers cost is zero ; Iterate for length >= 1 ; Find sum in range [ i . . j ] ; Initialise dp [ i ] [ j ] to INT_MAX ; Iterate for all possible K to find the minimum cost ; Update the minimum sum ; Return the final minimum cost ; Driver Code ; Given set of numbers ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mergeTwoNumbers ( vector < int > & numbers ) { int len , i , j , k ; int n = numbers . size ( ) ; if ( numbers . size ( ) == 0 ) { return 0 ; } vector < int > prefixSum ( n + 1 , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + numbers [ i - 1 ] ; } vector < vector < int > > dp ( n + 1 , vector < int > ( n + 1 ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] [ i ] = 0 ; } for ( len = 2 ; len <= n ; len ++ ) { for ( i = 1 ; i <= n - len + 1 ; i ++ ) { j = i + len - 1 ; int sum = prefixSum [ j ] - prefixSum [ i - 1 ] ; dp [ i ] [ j ] = INT_MAX ; for ( k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k + 1 ] [ j ] + sum ) ; } } } return dp [ 1 ] [ n ] ; } int main ( ) { vector < int > arr1 = { 6 , 4 , 4 , 6 } ; cout << mergeTwoNumbers ( arr1 ) << endl ; return 0 ; }
Maximum sum possible for every node by including it in a segment of N | C ++ program to calculate the maximum sum possible for every node by including it in a segment of the N - Ary Tree ; Stores the maximum sum possible for every node by including them in a segment with their successors ; Stores the maximum sum possible for every node by including them in a segment with their ancestors ; Store the maximum sum for every node by including it in a segment with its successors ; Update the maximum sums for each node by including them in a sequence with their ancestors ; Condition to check , if current node is not root ; Add edges ; Function to find the maximum answer for each node ; Compute the maximum sums with successors ; Store the computed maximums ; Update the maximum sums by including their ancestors ; Print the desired result ; Driver Program ; Number of nodes ; graph ; Add edges ; Weight of each node ; Compute the max sum of segments for each node ; Print the answer for every node
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp1 [ 100005 ] ; int dp2 [ 100005 ] ; void dfs1 ( int u , int par , vector < int > g [ ] , int weight [ ] ) { dp1 [ u ] = weight [ u ] ; for ( auto c : g [ u ] ) { if ( c != par ) { dfs1 ( c , u , g , weight ) ; dp1 [ u ] += max ( 0 , dp1 ) ; } } } void dfs2 ( int u , int par , vector < int > g [ ] , int weight [ ] ) { if ( par != 0 ) { int maxSumAncestors = dp2 [ par ] - max ( 0 , dp1 [ u ] ) ; dp2 [ u ] = dp1 [ u ] + max ( 0 , maxSumAncestors ) ; } for ( auto c : g [ u ] ) { if ( c != par ) { dfs2 ( c , u , g , weight ) ; } } } void addEdge ( int u , int v , vector < int > g [ ] ) { g [ u ] . push_back ( v ) ; g [ v ] . push_back ( u ) ; } void maxSumSegments ( vector < int > g [ ] , int weight [ ] , int n ) { dfs1 ( 1 , 0 , g , weight ) ; for ( int i = 1 ; i <= n ; i ++ ) { dp2 [ i ] = dp1 [ i ] ; } dfs2 ( 1 , 0 , g , weight ) ; } void printAns ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { cout << dp2 [ i ] << " ▁ " ; } } int main ( ) { int n = 6 ; int u , v ; vector < int > g [ 100005 ] ; addEdge ( 1 , 2 , g ) ; addEdge ( 1 , 3 , g ) ; addEdge ( 2 , 4 , g ) ; addEdge ( 2 , 5 , g ) ; addEdge ( 3 , 6 , g ) ; addEdge ( 4 , 7 , g ) ; int weight [ n + 1 ] ; weight [ 1 ] = -8 ; weight [ 2 ] = 9 ; weight [ 3 ] = 7 ; weight [ 4 ] = -4 ; weight [ 5 ] = 5 ; weight [ 6 ] = -10 ; weight [ 7 ] = -6 ; maxSumSegments ( g , weight , n ) ; printAns ( n ) ; return 0 ; }
Count array elements that can be maximized by adding any permutation of first N natural numbers | C ++ program for the above approach ; Function to get the count of values that can have the maximum value ; Sort array in decreasing order ; Stores the answer ; mark stores the maximum value till each index i ; Check if arr [ i ] can be maximum ; Update the mark ; Print the total count ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool cmp ( int x , int y ) { return x > y ; } void countMaximum ( int * a , int n ) { sort ( a , a + n , cmp ) ; int count = 0 ; int mark = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( a [ i ] + n >= mark ) ) { count += 1 ; } mark = max ( mark , a [ i ] + i + 1 ) ; } cout << count ; } int main ( ) { int arr [ ] = { 8 , 9 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countMaximum ( arr , N ) ; }
Count of pairs in an Array with same number of set bits | C ++ Program to count possible number of pairs of elements with same number of set bits . ; Function to return the count of Pairs ; Get the maximum element ; Array to store count of bits of all elements upto maxm ; Store the set bits for powers of 2 ; Compute the set bits for the remaining elements ; Store the frequency of respective counts of set bits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int N ) { int maxm = * max_element ( arr , arr + N ) ; int i , k ; int bitscount [ maxm + 1 ] = { 0 } ; for ( i = 1 ; i <= maxm ; i *= 2 ) bitscount [ i ] = 1 ; for ( i = 1 ; i <= maxm ; i ++ ) { if ( bitscount [ i ] == 1 ) k = i ; if ( bitscount [ i ] == 0 ) { bitscount [ i ] = bitscount [ k ] + bitscount [ i - k ] ; } } map < int , int > setbits ; for ( int i = 0 ; i < N ; i ++ ) { setbits [ bitscount [ arr [ i ] ] ] ++ ; } int ans = 0 ; for ( auto it : setbits ) { ans += it . second * ( it . second - 1 ) / 2 ; } return ans ; } int main ( ) { int N = 12 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 } ; cout << countPairs ( arr , N ) ; return 0 ; }
Count of Binary strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times | C ++ program to find the count of Binary strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times ; Array to contain the final result ; Function to get the number of desirable binary strings ; if we reach end of string and groups are exhausted , return 1 ; if length is exhausted but groups are still to be made , return 0 ; if length is not exhausted but groups are exhausted , return 0 ; if both are negative just return 0 ; if already calculated , return it ; initialise answer for each state ; loop through every possible m ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 1000 ] [ 1000 ] ; int solve ( int n , int k , int m ) { if ( n == 0 && k == 0 ) return 1 ; if ( n == 0 && k != 0 ) return 0 ; if ( n != 0 && k == 0 ) return 0 ; if ( n < 0 k < 0 ) return 0 ; if ( dp [ n ] [ k ] ) return dp [ n ] [ k ] ; int ans = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { ans += solve ( n - j , k - 1 , m ) ; } return dp [ n ] [ k ] = ans ; } int main ( ) { int N = 7 , K = 4 , M = 3 ; cout << solve ( N , K , M ) ; }
Maximum neighbor element in a matrix within distance K | C ++ implementation to find the maximum neighbor element within the distance of less than K ; Function to print the matrix ; Loop to iterate over the matrix ; Function to find the maximum neighbor within the distance of less than equal to K ; Loop to find the maximum element within the distance of less than K ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMatrix ( vector < vector < int > > A ) { for ( int i = 0 ; i < A . size ( ) ; i ++ ) { for ( int j = 0 ; j < A [ 0 ] . size ( ) ; j ++ ) cout << A [ i ] [ j ] << ' ▁ ' ; cout << ' ' ; } } vector < vector < int > > getMaxNeighbour ( vector < vector < int > > & A , int K ) { vector < vector < int > > ans = A ; for ( int q = 1 ; q <= K ; q ++ ) { for ( int i = 0 ; i < A . size ( ) ; i ++ ) { for ( int j = 0 ; j < A [ 0 ] . size ( ) ; j ++ ) { int maxi = ans [ i ] [ j ] ; if ( i > 0 ) maxi = max ( maxi , ans [ i - 1 ] [ j ] ) ; if ( j > 0 ) maxi = max ( maxi , ans [ i ] [ j - 1 ] ) ; if ( i < A . size ( ) - 1 ) maxi = max ( maxi , ans [ i + 1 ] [ j ] ) ; if ( j < A [ 0 ] . size ( ) - 1 ) maxi = max ( maxi , ans [ i ] [ j + 1 ] ) ; A [ i ] [ j ] = maxi ; } } ans = A ; } return ans ; } int main ( ) { vector < vector < int > > B = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printMatrix ( getMaxNeighbour ( B , 2 ) ) ; return 0 ; }
Number of distinct ways to represent a number as sum of K unique primes | C ++ program to count the Number of distinct ways to represent a number as K different primes ; Prime vector ; Sieve array of prime ; DP array ; Initialise all numbers as prime ; Sieve of Eratosthenes . ; Push all the primes into prime vector ; Function to get the number of distinct ways to get sum as K different primes ; If index went out of prime array size or the sum became larger than n return 0 ; If sum becomes equal to n and j becomes exactly equal to k . Return 1 , else if j is still not equal to k , return 0 ; If sum != n and still j as exceeded , return 0 ; If that state is already calculated , return directly the ans ; Include the current prime ; Exclude the current prime ; Return by memoizing the ans ; Driver code ; Precompute primes by sieve
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > prime ; bool isprime [ 1000 ] ; int dp [ 200 ] [ 20 ] [ 1000 ] ; void sieve ( ) { memset ( isprime , true , sizeof ( isprime ) ) ; for ( int i = 2 ; i * i <= 1000 ; i ++ ) { if ( isprime [ i ] ) { for ( int j = i * i ; j <= 1000 ; j += i ) { isprime [ j ] = false ; } } } for ( int i = 2 ; i <= 1000 ; i ++ ) { if ( isprime [ i ] ) { prime . push_back ( i ) ; } } } int CountWays ( int i , int j , int sum , int n , int k ) { if ( i > prime . size ( ) sum > n ) { return 0 ; } if ( sum == n ) { if ( j == k ) { return 1 ; } return 0 ; } if ( j == k ) return 0 ; if ( dp [ i ] [ j ] [ sum ] ) return dp [ i ] [ j ] [ sum ] ; int inc = 0 , exc = 0 ; inc = CountWays ( i + 1 , j + 1 , sum + prime [ i ] , n , k ) ; exc = CountWays ( i + 1 , j , sum , n , k ) ; return dp [ i ] [ j ] [ sum ] = inc + exc ; } int main ( ) { sieve ( ) ; int N = 100 , K = 5 ; cout << CountWays ( 0 , 0 , 0 , N , K ) ; }
Count minimum factor jumps required to reach the end of an Array | C ++ program for bottom up approach ; Vector to store factors of each integer ; Initialize the dp array ; Precompute all the factors of every integer ; Function to count the minimum factor jump ; Initialise minimum jumps to reach each cell as INT_MAX ; 0 jumps required to reach the first cell ; Iterate over all cells ; calculating for each jump ; If a cell is in bound ; Return minimum jumps to reach last cell ; Driver code ; Pre - calculating the factors ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > factors [ 100005 ] ; int dp [ 100005 ] ; void precompute ( ) { for ( int i = 1 ; i <= 100000 ; i ++ ) { for ( int j = i ; j <= 100000 ; j += i ) factors [ j ] . push_back ( i ) ; } } int solve ( int arr [ ] , int n ) { for ( int i = 0 ; i <= 100005 ; i ++ ) { dp [ i ] = INT_MAX ; } dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( auto j : factors [ arr [ i ] ] ) { if ( i + j < n ) dp [ i + j ] = min ( dp [ i + j ] , 1 + dp [ i ] ) ; } } return dp [ n - 1 ] ; } int main ( ) { precompute ( ) ; int arr [ ] = { 2 , 8 , 16 , 55 , 99 , 100 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << solve ( arr , n ) ; }