codeparrot/xlcost-text-to-code huggingface dataset:
Sum of product of consecutive Binomial Coefficients | CPP Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient upto nth term ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; finding the sum of product of consecutive coefficient . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE void binomialCoeff ( int C [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = min ( i , n ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } } int sumOfproduct ( int n ) { int sum = 0 ; int C [ MAX ] = { 0 } ; binomialCoeff ( C , n ) ; for ( int i = 0 ; i <= n ; i ++ ) sum += C [ i ] * C [ i + 1 ] ; return sum ; } int main ( ) { int n = 3 ; cout << sumOfproduct ( n ) << endl ; return 0 ; }
Sum of product of r and rth Binomial Coefficient ( r * nCr ) | CPP Program to find sum of product of r and rth Binomial Coefficient i . e summation r * nCr ; Return the first n term of binomial coefficient . ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return summation of r * nCr ; finding the first n term of binomial coefficient ; Iterate a loop to find the sum . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE void binomialCoeff ( int n , int C [ ] ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = min ( i , n ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } } int summation ( int n ) { int C [ MAX ] ; memset ( C , 0 , sizeof ( C ) ) ; binomialCoeff ( n , C ) ; int sum = 0 ; for ( int i = 0 ; i <= n ; i ++ ) sum += ( i * C [ i ] ) ; return sum ; } int main ( ) { int n = 2 ; cout << summation ( n ) << endl ; return 0 ; }
Number of arrays of size N whose elements are positive integers and sum is K | CPP Program to find the number of arrays of size N whose elements are positive integers and sum is K ; Return nCr ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the number of array that can be formed of size n and sum equals to k . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ k + 1 ] ; memset ( C , 0 , sizeof ( C ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } int countArray ( int N , int K ) { return binomialCoeff ( K - 1 , N - 1 ) ; } int main ( ) { int N = 2 , K = 3 ; cout << countArray ( N , K ) << endl ; return 0 ; }
Partitioning into two contiguous element subarrays with equal sums | CPP Program to find the minimum element to be added such that the array can be partitioned into two contiguous subarrays with equal sums ; Structure to store the minimum element and its position ; initialize prefix and suffix sum arrays with 0 ; add current element to Sum ; add current element to Sum ; initialize the minimum element to be a large value ; check for the minimum absolute difference between current prefix sum and the next suffix sum element ; if prefixsum has a greater value then position is the next element , else it 's the same element. ; return the data in struct . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct data { int element ; int position ; } ; struct data findMinElement ( int arr [ ] , int n ) { struct data result ; int prefixSum [ n ] = { 0 } ; int suffixSum [ n ] = { 0 } ; prefixSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; } suffixSum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] ; } int min = suffixSum [ 0 ] ; int pos ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) < min ) { min = abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) ; if ( suffixSum [ i + 1 ] < prefixSum [ i ] ) pos = i + 1 ; else pos = i ; } } result . element = min ; result . position = pos ; return result ; } int main ( ) { int arr [ ] = { 10 , 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; struct data values ; values = findMinElement ( arr , n ) ; cout << " Minimum ▁ element ▁ : ▁ " << values . element << endl << " Position ▁ : ▁ " << values . position ; return 0 ; }
Number of circular tours that visit all petrol pumps | C ++ Program to find the number of circular tour that visits all petrol pump ; Return the number of pumps from where we can start the journey . ; Making Circular Array . ; for each of the petrol pump . ; If tank is less than 0. ; If starting pump is greater than n , return ans as 0. ; For each of the petrol pump ; Finding the need array ; If need is 0 , increment the count . ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100 NEW_LINE int count ( int n , int c , int a [ ] , int b [ ] ) { int need [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i + n ] = a [ i ] ; b [ i + n ] = b [ i ] ; } int s = 0 ; int tank = 0 ; for ( int i = 0 ; i < 2 * n ; i ++ ) { tank += a [ i ] ; tank = min ( tank , c ) ; tank -= b [ i ] ; if ( tank < 0 ) { tank = 0 ; s = i + 1 ; } } if ( s >= n ) return 0 ; int ans = 1 ; need [ s + n ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int id = s + n - i ; need [ id ] = max ( 0 , need [ id + 1 ] + b [ id ] - min ( a [ id ] , c ) ) ; if ( need [ id ] == 0 ) ans ++ ; } return ans ; } int main ( ) { int n = 3 ; int c = 3 ; int a [ 2 * N ] = { 3 , 1 , 2 } ; int b [ 2 * N ] = { 2 , 2 , 2 } ; cout << count ( n , c , a , b ) << endl ; return 0 ; }
Print equal sum sets of array ( Partition Problem ) | Set 2 | CPP program to print equal sum sets of array . ; Function to print equal sum sets of array . ; Finding sum of array elements ; Check sum is even or odd . If odd then array cannot be partitioned . Print - 1 and return . ; Divide sum by 2 to find sum of two possible subsets . ; Boolean DP table to store result of states . dp [ i ] [ j ] = true if there is a subset of elements in first i elements of array that has sum equal to j . ; If number of elements are zero , then no sum can be obtained . ; Sum 0 can be obtained by not selecting any element . ; Fill the DP table in bottom up manner . ; Excluding current element . ; Including current element ; Required sets set1 and set2 . ; If partition is not possible print - 1 and return . ; Start from last element in dp table . ; If current element does not contribute to k , then it belongs to set 2. ; If current element contribute to k then it belongs to set 1. ; Print elements of both the sets . ; Driver program .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printEqualSumSets ( int arr [ ] , int n ) { int i , currSum ; int sum = accumulate ( arr , arr + n , 0 ) ; if ( sum & 1 ) { cout << " - 1" ; return ; } int k = sum >> 1 ; bool dp [ n + 1 ] [ k + 1 ] ; for ( i = 1 ; i <= k ; i ++ ) dp [ 0 ] [ i ] = false ; for ( i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( currSum = 1 ; currSum <= k ; currSum ++ ) { dp [ i ] [ currSum ] = dp [ i - 1 ] [ currSum ] ; if ( arr [ i - 1 ] <= currSum ) dp [ i ] [ currSum ] = dp [ i ] [ currSum ] | dp [ i - 1 ] [ currSum - arr [ i - 1 ] ] ; } } vector < int > set1 , set2 ; if ( ! dp [ n ] [ k ] ) { cout << " - 1 STRNEWLINE " ; return ; } i = n ; currSum = k ; while ( i > 0 && currSum >= 0 ) { if ( dp [ i - 1 ] [ currSum ] ) { i -- ; set2 . push_back ( arr [ i ] ) ; } else if ( dp [ i - 1 ] [ currSum - arr [ i - 1 ] ] ) { i -- ; currSum -= arr [ i ] ; set1 . push_back ( arr [ i ] ) ; } } cout << " Set ▁ 1 ▁ elements : ▁ " ; for ( i = 0 ; i < set1 . size ( ) ; i ++ ) cout << set1 [ i ] << " ▁ " ; cout << " Set 2 elements : " for ( i = 0 ; i < set2 . size ( ) ; i ++ ) cout << set2 [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 5 , 5 , 1 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printEqualSumSets ( arr , n ) ; return 0 ; }
Maximum average sum partition of an array | CPP program for maximum average sum partition ; storing prefix sums ; for each i to n storing averages ; Driver code ; int K = 3 ; atmost partitioning size
#include <bits/stdc++.h> NEW_LINE using namespace std ; double largestSumOfAverages ( vector < int > & A , int K ) { int n = A . size ( ) ; double pre_sum [ n + 1 ] ; pre_sum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ; double dp [ n ] = { 0 } ; double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = ( pre_sum [ n ] - pre_sum [ i ] ) / ( n - i ) ; for ( int k = 0 ; k < K - 1 ; k ++ ) for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) dp [ i ] = max ( dp [ i ] , ( pre_sum [ j ] - pre_sum [ i ] ) / ( j - i ) + dp [ j ] ) ; return dp [ 0 ] ; } int main ( ) { vector < int > A = { 9 , 1 , 2 , 3 , 9 } ; cout << largestSumOfAverages ( A , K ) << endl ; return 0 ; }
Find maximum sum array of length less than or equal to m | A Dynamic Programming based C ++ program to find maximum sum of array of size less than or equal to m from given n arrays ; N and M to define sizes of arr , dp , current_arr and maxSum ; INF to define min value ; Function to find maximum sum ; dp array of size N x M ; current_arr of size M ; maxsum of size M ; if we have 0 elements from 0 th array ; compute the cumulative sum array ; calculating the maximum contiguous array for every length j , j is from 1 to lengtn of the array ; every state is depending on its previous state ; computation of dp table similar approach as knapsack problem ; now we have done processing with the last array lets find out what is the maximum sum possible ; Driver program ; first element of each row is the size of that row
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  105  NEW_LINE #define M  1001  NEW_LINE #define INF  -1111111111  NEW_LINE int maxSum ( int arr [ ] [ N ] ) { int dp [ N ] [ M ] ; int current_arr [ M ] ; int maxsum [ M ] ; memset ( dp , -1 , sizeof ( dp [ 0 ] [ 0 ] ) * N * M ) ; current_arr [ 0 ] = 0 ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= 5 ; i ++ ) { int len = arr [ i - 1 ] [ 0 ] ; for ( int j = 1 ; j <= len ; j ++ ) { current_arr [ j ] = arr [ i - 1 ] [ j ] ; current_arr [ j ] += current_arr [ j - 1 ] ; maxsum [ j ] = INF ; } for ( int j = 1 ; j <= len && j <= 6 ; j ++ ) for ( int k = 1 ; k <= len ; k ++ ) if ( j + k - 1 <= len ) maxsum [ j ] = max ( maxsum [ j ] , current_arr [ j + k - 1 ] - current_arr [ k - 1 ] ) ; for ( int j = 0 ; j <= 6 ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; for ( int j = 1 ; j <= 6 ; j ++ ) for ( int cur = 1 ; cur <= j && cur <= len ; cur ++ ) dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - cur ] + maxsum [ cur ] ) ; } int ans = 0 ; for ( int i = 0 ; i <= 6 ; i ++ ) ans = max ( ans , dp [ 5 ] [ i ] ) ; return ans ; } int main ( ) { int arr [ ] [ N ] = { { 3 , 2 , 3 , 5 } , { 2 , 7 , -1 } , { 2 , 8 , 10 } , { 4 , 5 , 2 , 6 , 1 } , { 3 , 2 , 3 , -2 } } ; cout << " Maximum ▁ sum ▁ can ▁ be ▁ obtained ▁ " << " is ▁ : ▁ " << maxSum ( arr ) << " STRNEWLINE " ; }
Maximize array elements upto given number | C ++ program to find maximum value of number obtained by using array elements by using dynamic programming . ; Function to find maximum possible value of number that can be obtained using array elements . ; Variable to represent current index . ; Variable to show value between 0 and maxLimit . ; Table to store whether a value can be obtained or not upto a certain index . 1. dp [ i ] [ j ] = 1 if value j can be obtained upto index i . 2. dp [ i ] [ j ] = 0 if value j cannot be obtained upto index i . ; Check for index 0 if given value val can be obtained by either adding to or subtracting arr [ 0 ] from num . ; 1. If arr [ ind ] is added to obtain given val then val - arr [ ind ] should be obtainable from index ind - 1. 2. If arr [ ind ] is subtracted to obtain given val then val + arr [ ind ] should be obtainable from index ind - 1. Check for both the conditions . ; If either of one condition is true , then val is obtainable at index ind . ; Find maximum value that is obtained at index n - 1. ; If no solution exists return - 1. ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxVal ( int arr [ ] , int n , int num , int maxLimit ) { int ind ; int val ; int dp [ n ] [ maxLimit + 1 ] ; for ( ind = 0 ; ind < n ; ind ++ ) { for ( val = 0 ; val <= maxLimit ; val ++ ) { if ( ind == 0 ) { if ( num - arr [ ind ] == val num + arr [ ind ] == val ) { dp [ ind ] [ val ] = 1 ; } else { dp [ ind ] [ val ] = 0 ; } } else { if ( val - arr [ ind ] >= 0 && val + arr [ ind ] <= maxLimit ) { dp [ ind ] [ val ] = dp [ ind - 1 ] [ val - arr [ ind ] ] || dp [ ind - 1 ] [ val + arr [ ind ] ] ; } else if ( val - arr [ ind ] >= 0 ) { dp [ ind ] [ val ] = dp [ ind - 1 ] [ val - arr [ ind ] ] ; } else if ( val + arr [ ind ] <= maxLimit ) { dp [ ind ] [ val ] = dp [ ind - 1 ] [ val + arr [ ind ] ] ; } else { dp [ ind ] [ val ] = 0 ; } } } } for ( val = maxLimit ; val >= 0 ; val -- ) { if ( dp [ n - 1 ] [ val ] ) { return val ; } } return -1 ; } int main ( ) { int num = 1 ; int arr [ ] = { 3 , 10 , 6 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int maxLimit = 15 ; cout << findMaxVal ( arr , n , num , maxLimit ) ; return 0 ; }
Moser | CPP code to generate first ' n ' terms of the Moser - de Bruijn Sequence ; Function to generate nth term of Moser - de Bruijn Sequence ; S ( 0 ) = 0 ; S ( 1 ) = 1 ; S ( 2 * n ) = 4 * S ( n ) ; S ( 2 * n + 1 ) = 4 * S ( n ) + 1 ; Generating the first ' n ' terms of Moser - de Bruijn Sequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gen ( int n ) { if ( n == 0 ) return 0 ; else if ( n == 1 ) return 1 ; else if ( n % 2 == 0 ) return 4 * gen ( n / 2 ) ; else if ( n % 2 == 1 ) return 4 * gen ( n / 2 ) + 1 ; } void moserDeBruijn ( int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << gen ( i ) << " ▁ " ; cout << " STRNEWLINE " ; } int main ( ) { int n = 15 ; cout << " First ▁ " << n << " ▁ terms ▁ of ▁ " << " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁ STRNEWLINE " ; moserDeBruijn ( n ) ; return 0 ; }
Minimum Sum Path in a Triangle | C ++ program for Dynamic Programming implementation of Min Sum Path in a Triangle ; Util function to find minimum sum for a path ; For storing the result in a 1 - D array , and simultaneously updating the result . ; For the bottom row ; Calculation of the remaining rows , in bottom up manner . ; return the top element ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSumPath ( vector < vector < int > > & A ) { int memo [ A . size ( ) ] ; int n = A . size ( ) - 1 ; for ( int i = 0 ; i < A [ n ] . size ( ) ; i ++ ) memo [ i ] = A [ n ] [ i ] ; for ( int i = A . size ( ) - 2 ; i >= 0 ; i -- ) for ( int j = 0 ; j < A [ i ] . size ( ) ; j ++ ) memo [ j ] = A [ i ] [ j ] + min ( memo [ j ] , memo [ j + 1 ] ) ; return memo [ 0 ] ; } int main ( ) { vector < vector < int > > A { { 2 } , { 3 , 9 } , { 1 , 6 , 7 } } ; cout << minSumPath ( A ) ; return 0 ; }
Check if any valid sequence is divisible by M |  ; Base case ; check if sum is divisible by M ; 1. Try placing ' + ' ; 2. Try placing ' - '
bool isPossible ( int index , int sum ) { if ( index == n ) { if ( ( sum % M ) == 0 ) return true ; return false ; } bool placeAdd = isPossible ( index + 1 , sum + arr [ index ] ) ; bool placeMinus = isPossible ( index + 1 , sum - arr [ index ] ) ; if ( placeAdd placeMinus ) return true ; return false ; }
Minimum removals from array to make max | CPP program to find minimum removals to make max - min <= K ; function to check all possible combinations of removal and return the minimum one ; base case when all elements are removed ; if condition is satisfied , no more removals are required ; if the state has already been visited ; when Amax - Amin > d ; minimum is taken of the removal of minimum element or removal of the maximum element ; To sort the array and return the answer ; sort the array ; fill all stated with - 1 when only one element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE int dp [ MAX ] [ MAX ] ; int countRemovals ( int a [ ] , int i , int j , int k ) { if ( i >= j ) return 0 ; else if ( ( a [ j ] - a [ i ] ) <= k ) return 0 ; else if ( dp [ i ] [ j ] != -1 ) return dp [ i ] [ j ] ; else if ( ( a [ j ] - a [ i ] ) > k ) { dp [ i ] [ j ] = 1 + min ( countRemovals ( a , i + 1 , j , k ) , countRemovals ( a , i , j - 1 , k ) ) ; } return dp [ i ] [ j ] ; } int removals ( int a [ ] , int n , int k ) { sort ( a , a + n ) ; memset ( dp , -1 , sizeof ( dp ) ) ; if ( n == 1 ) return 0 ; else return countRemovals ( a , 0 , n - 1 , k ) ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 4 ; cout << removals ( a , n , k ) ; return 0 ; }
Minimum removals from array to make max | C ++ program for the above approach ; Function to find rightmost index which satisfies the condition arr [ j ] - arr [ i ] <= k ; Initialising start to i + 1 ; Initialising end to n - 1 ; Binary search implementation to find the rightmost element that satisfy the condition ; Check if the condition satisfies ; Store the position ; Make start = mid + 1 ; Make end = mid ; Return the rightmost position ; Function to calculate minimum number of elements to be removed ; Sort the given array ; Iterate from 0 to n - 1 ; Find j such that arr [ j ] - arr [ i ] <= k ; If there exist such j that satisfies the condition ; Number of elements to be removed for this particular case is ( j - i + 1 ) ; Return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findInd ( int key , int i , int n , int k , int arr [ ] ) { int start , end , mid , ind = -1 ; start = i + 1 ; end = n - 1 ; while ( start < end ) { mid = start + ( end - start ) / 2 ; if ( arr [ mid ] - key <= k ) { ind = mid ; start = mid + 1 ; } else { end = mid ; } } return ind ; } int removals ( int arr [ ] , int n , int k ) { int i , j , ans = n - 1 ; sort ( arr , arr + n ) ; for ( i = 0 ; i < n ; i ++ ) { j = findInd ( arr [ i ] , i , n , k , arr ) ; if ( j != -1 ) { ans = min ( ans , n - ( j - i + 1 ) ) ; } } return ans ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 4 ; cout << removals ( a , n , k ) ; return 0 ; }
Number of ordered pairs such that ( Ai & Aj ) = 0 | CPP program to calculate the number of ordered pairs such that their bitwise and is zero ; Naive function to count the number of ordered pairs such that their bitwise and is 0 ; check for all possible pairs ; add 2 as ( i , j ) and ( j , i ) are considered different ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( a [ i ] & a [ j ] ) == 0 ) count += 2 ; } return count ; } int main ( ) { int a [ ] = { 3 , 4 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countPairs ( a , n ) ; return 0 ; }
Number of ordered pairs such that ( Ai & Aj ) = 0 | CPP program to calculate the number of ordered pairs such that their bitwise and is zero ; efficient function to count pairs ; stores the frequency of each number ; count the frequency of every element ; iterate for al possible values that a [ i ] can be ; if the last bit is ON ; else is the last bit is OFF ; iterate till n ; if mask 's ith bit is set ; else if mask 's ith bit is not set ; iterate for all the array element and count the number of pairs ; return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 15 ; long long countPairs ( int a [ ] , int n ) { unordered_map < int , int > hash ; long long dp [ 1 << N ] [ N + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) hash [ a [ i ] ] += 1 ; for ( long long mask = 0 ; mask < ( 1 << N ) ; ++ mask ) { if ( mask & 1 ) dp [ mask ] [ 0 ] = hash [ mask ] + hash [ mask ^ 1 ] ; dp [ mask ] [ 0 ] = hash [ mask ] ; for ( int i = 1 ; i <= N ; ++ i ) { if ( mask & ( 1 << i ) ) { dp [ mask ] [ i ] = dp [ mask ] [ i - 1 ] + dp [ mask ^ ( 1 << i ) ] [ i - 1 ] ; } dp [ mask ] [ i ] = dp [ mask ] [ i - 1 ] ; } } long long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += dp [ ( ( 1 << N ) - 1 ) ^ a [ i ] ] [ N ] ; return ans ; } int main ( ) { int a [ ] = { 5 , 4 , 1 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countPairs ( a , n ) ; return 0 ; }
Minimum sum of multiplications of n numbers | CPP program to find the minimum sum of multiplication of n numbers ; Used in recursive memoized solution ; function to calculate the cumulative sum from a [ i ] to a [ j ] ; base case ; memoization , if the partition has been called before then return the stored value ; store a max value ; we break them into k partitions ; store the min of the formula thus obtained ; return the minimum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long dp [ 1000 ] [ 1000 ] ; long long sum ( int a [ ] , int i , int j ) { long long ans = 0 ; for ( int m = i ; m <= j ; m ++ ) ans = ( ans + a [ m ] ) % 100 ; return ans ; } long long solve ( int a [ ] , int i , int j ) { if ( i == j ) return 0 ; if ( dp [ i ] [ j ] != -1 ) return dp [ i ] [ j ] ; dp [ i ] [ j ] = INT_MAX ; for ( int k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = min ( dp [ i ] [ j ] , ( solve ( a , i , k ) + solve ( a , k + 1 , j ) + ( sum ( a , i , k ) * sum ( a , k + 1 , j ) ) ) ) ; } return dp [ i ] [ j ] ; } void initialize ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = -1 ; } int main ( ) { int a [ ] = { 40 , 60 , 20 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; initialize ( n ) ; cout << solve ( a , 0 , n - 1 ) << endl ; return 0 ; }
Print Fibonacci Series in reverse order | CPP Program to print Fibonacci series in reverse order ; assigning first and second elements ; storing sum in the preceding location ; printing array in reverse order ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reverseFibonacci ( int n ) { int a [ n ] ; a [ 0 ] = 0 ; a [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) { a [ i ] = a [ i - 2 ] + a [ i - 1 ] ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { cout << a [ i ] << " ▁ " ; } } int main ( ) { int n = 5 ; reverseFibonacci ( n ) ; return 0 ; }
Check if it is possible to transform one string to another | cpp program to check if a string can be converted to another string by performing operations ; function to check if a string can be converted to another string by performing following operations ; calculates length ; mark 1 st position as true ; traverse for all DPi , j ; if possible for to convert i characters of s1 to j characters of s2 ; if upper_case ( s1 [ i ] ) == s2 [ j ] is same ; if not upper then deletion is possible ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string s1 , string s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; bool dp [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { for ( int j = 0 ; j <= s2 . length ( ) ; j ++ ) { if ( dp [ i ] [ j ] ) { if ( j < s2 . length ( ) && ( toupper ( s1 [ i ] ) == s2 [ j ] ) ) dp [ i + 1 ] [ j + 1 ] = true ; if ( ! isupper ( s1 [ i ] ) ) dp [ i + 1 ] [ j ] = true ; } } } return ( dp [ n ] [ m ] ) ; } int main ( ) { string s1 = " daBcd " ; string s2 = " ABC " ; if ( check ( s1 , s2 ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Probability of reaching a point with 2 or 3 steps at a time | CPP Program to find probability to reach N with P probability to take 2 steps ( 1 - P ) to take 3 steps ; Returns probability to reach N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float find_prob ( int N , float P ) { double dp [ N + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 0 ; dp [ 2 ] = P ; dp [ 3 ] = 1 - P ; for ( int i = 4 ; i <= N ; ++ i ) dp [ i ] = ( P ) * dp [ i - 2 ] + ( 1 - P ) * dp [ i - 3 ] ; return dp [ N ] ; } int main ( ) { int n = 5 ; float p = 0.2 ; cout << find_prob ( n , p ) ; return 0 ; }