codeparrot/xlcost-text-to-code huggingface dataset:
Divide 1 to n into two groups with minimum sum difference | CPP program to divide n integers in two groups such that absolute difference of their sum is minimum ; To print vector along size ; Print vector size ; Print vector elements ; To divide n in two groups such that absolute difference of their sum is minimum ; Find sum of all elements upto n ; Sum of elements of group1 ; If sum is greater then or equal to 0 include i in group 1 otherwise include in group2 ; Decrease sum of group1 ; Print both the groups ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printVector ( vector < int > v ) { cout << v . size ( ) << endl ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) cout << v [ i ] << " ▁ " ; cout << endl ; } void findTwoGroup ( int n ) { int sum = n * ( n + 1 ) / 2 ; int group1Sum = sum / 2 ; vector < int > group1 , group2 ; for ( int i = n ; i > 0 ; i -- ) { if ( group1Sum - i >= 0 ) { group1 . push_back ( i ) ; group1Sum -= i ; } else { group2 . push_back ( i ) ; } } printVector ( group1 ) ; printVector ( group2 ) ; } int main ( ) { int n = 5 ; findTwoGroup ( n ) ; return 0 ; }
Maximum number of customers that can be satisfied with given quantity | CPP program to find maximum number of customers that can be satisfied ; print maximum number of satisfied customers and their indexes ; Creating an vector of pair of total demand and customer number ; Sorting the customers according to their total demand ; Taking the first k customers that can be satisfied by total amount d ; Driver program ; Initializing variables
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < pair < long long , int > > v ; void solve ( int n , int d , int a , int b , int arr [ ] [ 2 ] ) { for ( int i = 0 ; i < n ; i ++ ) { int m = arr [ i ] [ 0 ] , t = arr [ i ] [ 1 ] ; v . push_back ( make_pair ( ( a * m + b * t ) , i + 1 ) ) ; } sort ( v . begin ( ) , v . end ( ) ) ; vector < int > ans ; for ( int i = 0 ; i < n ; i ++ ) { if ( v [ i ] . first <= d ) { ans . push_back ( v [ i ] . second ) ; d -= v [ i ] . first ; } } cout << ans . size ( ) << endl ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) cout << ans [ i ] << " ▁ " ; } int main ( ) { int n = 5 ; long d = 5 ; int a = 1 , b = 1 ; int arr [ ] [ 2 ] = { { 2 , 0 } , { 3 , 2 } , { 4 , 4 } , { 10 , 0 } , { 0 , 1 } } ; solve ( n , d , a , b , arr ) ; return 0 ; }
Partition into two subarrays of lengths k and ( N | C ++ program to calculate max_difference between the sum of two subarrays of length k and N - k ; Function to calculate max_difference ; Sum of the array ; Sort the array in descending order ; Calculating max_difference ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDifference ( int arr [ ] , int N , int k ) { int M , S = 0 , S1 = 0 , max_difference = 0 ; for ( int i = 0 ; i < N ; i ++ ) S += arr [ i ] ; sort ( arr , arr + N , greater < int > ( ) ) ; M = max ( k , N - k ) ; for ( int i = 0 ; i < M ; i ++ ) S1 += arr [ i ] ; max_difference = S1 - ( S - S1 ) ; return max_difference ; } int main ( ) { int arr [ ] = { 8 , 4 , 5 , 2 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << maxDifference ( arr , N , k ) << endl ; return 0 ; }
Minimum sum of product of two arrays | CPP program to find minimum sum of product of two arrays with k operations allowed on first array . ; Function to find the minimum product ; Find product of current elements and update result . ; If both product and b [ i ] are negative , we must increase value of a [ i ] to minimize result . ; If both product and a [ i ] are negative , we must decrease value of a [ i ] to minimize result . ; Similar to above two cases for positive product . ; Check if current difference becomes higher than the maximum difference so far . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minproduct ( int a [ ] , int b [ ] , int n , int k ) { int diff = 0 , res = 0 ; int temp ; for ( int i = 0 ; i < n ; i ++ ) { int pro = a [ i ] * b [ i ] ; res = res + pro ; if ( pro < 0 && b [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ; else if ( pro < 0 && a [ i ] < 0 ) temp = ( a [ i ] - 2 * k ) * b [ i ] ; else if ( pro > 0 && a [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ; else if ( pro > 0 && a [ i ] > 0 ) temp = ( a [ i ] - 2 * k ) * b [ i ] ; int d = abs ( pro - temp ) ; if ( d > diff ) diff = d ; } return res - diff ; } int main ( ) { int a [ ] = { 2 , 3 , 4 , 5 , 4 } ; int b [ ] = { 3 , 4 , 2 , 3 , 2 } ; int n = 5 , k = 3 ; cout << minproduct ( a , b , n , k ) << endl ; return 0 ; }
Split n into maximum composite numbers | CPP program to split a number into maximum number of composite numbers . ; function to calculate the maximum number of composite numbers adding upto n ; 4 is the smallest composite number ; stores the remainder when n is divided by 4 ; if remainder is 0 , then it is perfectly divisible by 4. ; if the remainder is 1 ; If the number is less then 9 , that is 5 , then it cannot be expressed as 4 is the only composite number less than 5 ; If the number is greater then 8 , and has a remainder of 1 , then express n as n - 9 a and it is perfectly divisible by 4 and for 9 , count 1. ; When remainder is 2 , just subtract 6 from n , so that n is perfectly divisible by 4 and count 1 for 6 which is subtracted . ; if the number is 7 , 11 which cannot be expressed as sum of any composite numbers ; when the remainder is 3 , then subtract 15 from it and n becomes perfectly divisible by 4 and we add 2 for 9 and 6 , which is getting subtracted to make n perfectly divisible by 4. ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int n ) { if ( n < 4 ) return -1 ; int rem = n % 4 ; if ( rem == 0 ) return n / 4 ; if ( rem == 1 ) { if ( n < 9 ) return -1 ; return ( n - 9 ) / 4 + 1 ; } if ( rem == 2 ) return ( n - 6 ) / 4 + 1 ; if ( rem == 3 ) { if ( n < 15 ) return -1 ; return ( n - 15 ) / 4 + 2 ; } } int main ( ) { int n = 90 ; cout << count ( n ) << endl ; n = 143 ; cout << count ( n ) << endl ; return 0 ; }
Policemen catch thieves | C ++ program to find maximum number of thieves caught ; Returns maximum number of thieves that can be caught . ; store indices in the vector ; track lowest current indices of thief : thi [ l ] , police : pol [ r ] ; can be caught ; increment the minimum index ; Driver program
#include <iostream> NEW_LINE #include <vector> NEW_LINE #include <cmath> NEW_LINE using namespace std ; int policeThief ( char arr [ ] , int n , int k ) { int res = 0 ; vector < int > thi ; vector < int > pol ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == ' P ' ) pol . push_back ( i ) ; else if ( arr [ i ] == ' T ' ) thi . push_back ( i ) ; } int l = 0 , r = 0 ; while ( l < thi . size ( ) && r < pol . size ( ) ) { if ( abs ( thi [ l ] - pol [ r ] ) <= k ) { res ++ ; l ++ ; r ++ ; } else if ( thi [ l ] < pol [ r ] ) l ++ ; else r ++ ; } return res ; } int main ( ) { int k , n ; char arr1 [ ] = { ' P ' , ' T ' , ' T ' , ' P ' , ' T ' } ; k = 2 ; n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << " Maximum ▁ thieves ▁ caught : ▁ " << policeThief ( arr1 , n , k ) << endl ; char arr2 [ ] = { ' T ' , ' T ' , ' P ' , ' P ' , ' T ' , ' P ' } ; k = 2 ; n = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << " Maximum ▁ thieves ▁ caught : ▁ " << policeThief ( arr2 , n , k ) << endl ; char arr3 [ ] = { ' P ' , ' T ' , ' P ' , ' T ' , ' T ' , ' P ' } ; k = 3 ; n = sizeof ( arr3 ) / sizeof ( arr3 [ 0 ] ) ; cout << " Maximum ▁ thieves ▁ caught : ▁ " << policeThief ( arr3 , n , k ) << endl ; return 0 ; }
Minimum rotations to unlock a circular lock | CPP program for min rotation to unlock ; function for min rotation ; iterate till input and unlock code become 0 ; input and unlock last digit as reminder ; find min rotation ; update code and input ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minRotation ( int input , int unlock_code ) { int rotation = 0 ; int input_digit , code_digit ; while ( input unlock_code ) { input_digit = input % 10 ; code_digit = unlock_code % 10 ; rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) ; input /= 10 ; unlock_code /= 10 ; } return rotation ; } int main ( ) { int input = 28756 ; int unlock_code = 98234 ; cout << " Minimum ▁ Rotation ▁ = ▁ " << minRotation ( input , unlock_code ) ; return 0 ; }
Job Scheduling with two jobs allowed at a time | CPP program to check if all jobs can be scheduled if two jobs are allowed at a time . ; making a pair of starting and ending time of job ; sorting according to starting time of job ; starting first and second job simultaneously ; Checking if starting time of next new job is greater than ending time of currently scheduled first job ; Checking if starting time of next new job is greater than ending time of ocurrently scheduled second job ; Driver code ; int startin [ ] = { 1 , 2 , 4 } ; starting time of jobs int endin [ ] = { 2 , 3 , 5 } ; ending times of jobs
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkJobs ( int startin [ ] , int endin [ ] , int n ) { vector < pair < int , int > > a ; for ( int i = 0 ; i < n ; i ++ ) a . push_back ( make_pair ( startin [ i ] , endin [ i ] ) ) ; sort ( a . begin ( ) , a . end ( ) ) ; long long tv1 = a [ 0 ] . second , tv2 = a [ 1 ] . second ; for ( int i = 2 ; i < n ; i ++ ) { if ( a [ i ] . first >= tv1 ) { tv1 = tv2 ; tv2 = a [ i ] . second ; } else if ( a [ i ] . first >= tv2 ) tv2 = a [ i ] . second ; else return false ; } return true ; } int main ( ) { int n = sizeof ( startin ) / sizeof ( startin [ 0 ] ) ; cout << checkJobs ( startin , endin , n ) ; return 0 ; }
Minimum cost for acquiring all coins with k extra coins allowed with every coin | C ++ program to acquire all n coins at minimum cost with multiple values of k . ; Converts coin [ ] to prefix sum array ; sort the coins value ; Maintain prefix sum array ; Function to calculate min cost when we can get k extra coins after paying cost of one . ; calculate no . of coins needed ; return sum of from prefix array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void preprocess ( int coin [ ] , int n ) { sort ( coin , coin + n ) ; for ( int i = 1 ; i <= n - 1 ; i ++ ) coin [ i ] += coin [ i - 1 ] ; } int minCost ( int coin [ ] , int n , int k ) { int coins_needed = ceil ( 1.0 * n / ( k + 1 ) ) ; return coin [ coins_needed - 1 ] ; } int main ( ) { int coin [ ] = { 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 } ; int n = sizeof ( coin ) / sizeof ( coin [ 0 ] ) ; preprocess ( coin , n ) ; int k = 3 ; cout << minCost ( coin , n , k ) << endl ; k = 7 ; cout << minCost ( coin , n , k ) << endl ; return 0 ; }
Program for Worst Fit algorithm in Memory Management | C ++ implementation of worst - Fit algorithm ; Function to allocate memory to blocks as per worst fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; Find the best fit block for current process ; If we could find a block for current process ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void worstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] ; memset ( allocation , -1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int wstIdx = -1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( wstIdx == -1 ) wstIdx = j ; else if ( blockSize [ wstIdx ] < blockSize [ j ] ) wstIdx = j ; } } if ( wstIdx != -1 ) { allocation [ i ] = wstIdx ; blockSize [ wstIdx ] -= processSize [ i ] ; } } cout << " Process No . Process Size Block no . " ; for ( int i = 0 ; i < n ; i ++ ) { cout << " ▁ " << i + 1 << " TABSYMBOL TABSYMBOL " << processSize [ i ] << " TABSYMBOL TABSYMBOL " ; if ( allocation [ i ] != -1 ) cout << allocation [ i ] + 1 ; else cout << " Not ▁ Allocated " ; cout << endl ; } } int main ( ) { int blockSize [ ] = { 100 , 500 , 200 , 300 , 600 } ; int processSize [ ] = { 212 , 417 , 112 , 426 } ; int m = sizeof ( blockSize ) / sizeof ( blockSize [ 0 ] ) ; int n = sizeof ( processSize ) / sizeof ( processSize [ 0 ] ) ; worstFit ( blockSize , m , processSize , n ) ; return 0 ; }
Maximize array sum after K negations | Set 1 | C ++ program to maximize array sum after k operations . ; This function does k operations on array in a way that maximize the array sum . index -- > stores the index of current minimum element for j 'th operation ; Modify array K number of times ; Find minimum element in array for current operation and modify it i . e ; arr [ j ] -- > - arr [ j ] ; this the condition if we find 0 as minimum element , so it will useless to replace 0 by - ( 0 ) for remaining operations ; Modify element of array ; Calculate sum of array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSum ( int arr [ ] , int n , int k ) { for ( int i = 1 ; i <= k ; i ++ ) { int min = INT_MAX ; int index = -1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] < min ) { min = arr [ j ] ; index = j ; } } if ( min == 0 ) break ; arr [ index ] = - arr [ index ] ; } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } int main ( ) { int arr [ ] = { -2 , 0 , 5 , -1 , 2 } ; int k = 4 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumSum ( arr , n , k ) ; return 0 ; }
Maximize array sum after K negations | Set 1 | C ++ program to find maximum array sum after at most k negations . ; Sorting given array using in - built sort function ; If we find a 0 in our sorted array , we stop ; Calculating sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sol ( int arr [ ] , int n , int k ) { int sum = 0 ; int i = 0 ; sort ( arr , arr + n ) ; while ( k > 0 ) { if ( arr [ i ] >= 0 ) k = 0 ; else { arr [ i ] = ( -1 ) * arr [ i ] ; k = k - 1 ; } i ++ ; } for ( int j = 0 ; j < n ; j ++ ) { sum += arr [ j ] ; } return sum ; } int main ( ) { int arr [ ] = { -2 , 0 , 5 , -1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sol ( arr , n , 4 ) << endl ; return 0 ; }
Maximize array sum after K negations | Set 1 | C ++ program for the above approach ; Function to calculate sum of the array ; Iterate from 0 to n - 1 ; Function to maximize sum ; Iterate from 0 to n - 1 ; Driver Code ; Function Call
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; long long int sumArray ( long long int * arr , int n ) { long long int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } return sum ; } long long int maximizeSum ( long long int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( k && arr [ i ] < 0 ) { arr [ i ] *= -1 ; k -- ; continue ; } break ; } if ( i == n ) i -- ; if ( k == 0 k % 2 == 0 ) { return sumArray ( arr , n ) ; } if ( i != 0 && abs ( arr [ i ] ) >= abs ( arr [ i - 1 ] ) ) { i -- ; } arr [ i ] *= -1 ; return sumArray ( arr , n ) ; } int main ( ) { int n = 5 ; int k = 4 ; long long int arr [ 5 ] = { -3 , -2 , -1 , 5 , 6 } ; cout << maximizeSum ( arr , n , k ) << endl ; return 0 ; }
Bin Packing Problem ( Minimize number of used Bins ) | C ++ program to find number of bins required using First Fit algorithm . ; Returns number of bins required using first fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the first bin that can accommodate weight [ i ] ; If no bin could accommodate weight [ i ] ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstFit ( int weight [ ] , int n , int c ) { int res = 0 ; int bin_rem [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] ) { bin_rem [ j ] = bin_rem [ j ] - weight [ i ] ; break ; } } if ( j == res ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } } return res ; } int main ( ) { int weight [ ] = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = sizeof ( weight ) / sizeof ( weight [ 0 ] ) ; cout << " Number ▁ of ▁ bins ▁ required ▁ in ▁ First ▁ Fit ▁ : ▁ " << firstFit ( weight , n , c ) ; return 0 ; }
Minimum cost to reduce A and B to 0 using square root or divide by 2 | C ++ program for the above approach ; Function to return the minimum cost of converting A and B to 0 ; If both A and B doesn 't change in  this recusrive call, then return INT_MAX  to save the code from going into  infinite loop ; Base Case ; If the answer of this recursive call is already memoised ; If A is reduced to A / 2 ; If B is reduced to B / 2 ; If both A and B is reduced to sqrt ( A * B ) ; Return the minimum of the value given by the above three subproblems , also memoize the value while returning ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinOperations ( int A , int B , int prevA , int prevB , vector < vector < int > > & dp ) { if ( A == prevA and B == prevB ) { return INT_MAX ; } if ( A == 0 and B == 0 ) { return 0 ; } if ( dp [ A ] [ B ] != -1 ) { return dp [ A ] [ B ] ; } int ans1 = getMinOperations ( A / 2 , B , A , B , dp ) ; if ( ans1 != INT_MAX ) { ans1 += 1 ; } int ans2 = getMinOperations ( A , B / 2 , A , B , dp ) ; if ( ans2 != INT_MAX ) { ans2 += 1 ; } int ans3 = getMinOperations ( sqrt ( A * B ) , sqrt ( A * B ) , A , B , dp ) ; if ( ans3 != INT_MAX ) { ans3 += 2 ; } return dp [ A ] [ B ] = min ( { ans1 , ans2 , ans3 } ) ; } int main ( ) { int A = 53 , B = 16 ; int mx = max ( A , B ) ; vector < vector < int > > dp ( mx + 1 , vector < int > ( mx + 1 , -1 ) ) ; cout << getMinOperations ( A , B , -1 , -1 , dp ) ; }
Maximize count of indices with same element by pairing rows from given Matrices | C ++ program for the above approach ; Function to find the maximum score ; Stores the maximum sum of scores ; For each permutation of pos vector calculate the score ; If values at current indexes are same then increment the current score ; Print the maximum score ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxScoreSum ( vector < vector < int > > & a , vector < vector < int > > & b ) { int maxSum = 0 ; vector < int > pos ; for ( int i = 0 ; i < a . size ( ) ; i ++ ) { pos . push_back ( i ) ; } do { int curSum = 0 ; for ( int i = 0 ; i < a . size ( ) ; i ++ ) { for ( int j = 0 ; j < a [ pos [ i ] ] . size ( ) ; j ++ ) { curSum += ( a [ pos [ i ] ] [ j ] == b [ i ] [ j ] ) ; maxSum = max ( maxSum , curSum ) ; } } } while ( next_permutation ( pos . begin ( ) , pos . end ( ) ) ) ; cout << maxSum ; } int main ( ) { int N = 3 , M = 3 ; vector < vector < int > > a = { { 1 , 1 , 0 } , { 1 , 0 , 1 } , { 0 , 0 , 1 } } ; vector < vector < int > > b = { { 1 , 0 , 0 } , { 0 , 0 , 1 } , { 1 , 1 , 0 } } ; maxScoreSum ( a , b ) ; return 0 ; }
Maximize count of indices with same element by pairing rows from given Matrices | C ++ program for the above approach ; Function to find the maximum defined score ; If all students are assigned ; Check if row is not paired yet ; Check for all indexes ; If values at current indexes are same increase curSum ; Further recursive call ; Store the ans for current mask and return ; Utility function to find the maximum defined score ; Create a mask with all set bits 1 -> row is not paired yet 0 -> row is already paired ; Initialise dp array with - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxScoreSum ( vector < vector < int > > & a , vector < vector < int > > & b , int row , int mask , vector < int > & dp ) { if ( row >= a . size ( ) ) { return 0 ; } if ( dp [ mask ] != -1 ) { return dp [ mask ] ; } int ans = 0 ; for ( int i = 0 ; i < a . size ( ) ; i ++ ) { if ( mask & ( 1 << i ) ) { int newMask = mask ^ ( 1 << i ) ; int curSum = 0 ; for ( int j = 0 ; j < a [ i ] . size ( ) ; j ++ ) { if ( a [ row ] [ j ] == b [ i ] [ j ] ) { curSum ++ ; } } ans = max ( ans , curSum + maxScoreSum ( a , b , row + 1 , newMask , dp ) ) ; } } return dp [ mask ] = ans ; } int maxScoreSumUtil ( vector < vector < int > > & a , vector < vector < int > > & b , int N , int M ) { int row = 0 ; int mask = pow ( 2 , M ) - 1 ; vector < int > dp ( mask + 1 , -1 ) ; return maxScoreSum ( a , b , row , mask , dp ) ; } int main ( ) { int N = 3 , M = 3 ; vector < vector < int > > a = { { 1 , 1 , 0 } , { 1 , 0 , 1 } , { 0 , 0 , 1 } } ; vector < vector < int > > b = { { 1 , 0 , 0 } , { 0 , 0 , 1 } , { 1 , 1 , 0 } } ; cout << maxScoreSumUtil ( a , b , N , M ) ; return 0 ; }
Split given arrays into subarrays to maximize the sum of maximum and minimum in each subarrays | C ++ program for the above approach ; Function to find the maximum sum of differences of subarrays by splitting array into non - overlapping subarrays ; Stores the answer for prefix and initialize with zero ; Assume i - th index as right endpoint ; Choose the current value as the maximum and minimum ; Find the left endpoint and update the array dp [ ] ; Return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDiffSum ( int arr [ ] , int n ) { int dp [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int maxVal = arr [ i ] , minVal = arr [ i ] ; for ( int j = i ; j >= 0 ; j -- ) { minVal = min ( minVal , arr [ j ] ) ; maxVal = max ( maxVal , arr [ j ] ) ; if ( j - 1 >= 0 ) dp [ i ] = max ( dp [ i ] , maxVal - minVal + dp [ j - 1 ] ) ; else dp [ i ] = max ( dp [ i ] , maxVal - minVal ) ; } } return dp [ n - 1 ] ; } int main ( ) { int arr [ ] = { 8 , 1 , 7 , 9 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxDiffSum ( arr , N ) ; return 0 ; }
Count of N | C ++ program for the above approach ; Find the value of x raised to the yth power modulo MOD ; Stores the value of x ^ y ; Iterate until y is positive ; Function to perform the Modular Multiplicative Inverse using the Fermat 's little theorem ; Modular division x / y , find modular multiplicative inverse of y and multiply by x ; Function to find Binomial Coefficient C ( n , k ) in O ( k ) time ; Base Case ; Update the value of p and q ; Function to find the count of arrays having K as the first element satisfying the given criteria ; Stores the resultant count of arrays ; Find the factorization of K ; Stores the count of the exponent of the currentprime factor ; N is one last prime factor , for c = 1 -> C ( N - 1 + 1 , 1 ) = N ; Return the totol count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MOD = 1000000007 ; long modPow ( long x , long y ) { long r = 1 , a = x ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) { r = ( r * a ) % MOD ; } a = ( a * a ) % MOD ; y /= 2 ; } return r ; } long modInverse ( long x ) { return modPow ( x , MOD - 2 ) ; } long modDivision ( long p , long q ) { return ( p * modInverse ( q ) ) % MOD ; } long C ( long n , int k ) { if ( k > n ) { return 0 ; } long p = 1 , q = 1 ; for ( int i = 1 ; i <= k ; i ++ ) { q = ( q * i ) % MOD ; p = ( p * ( n - i + 1 ) ) % MOD ; } return modDivision ( p , q ) ; } int countArrays ( int N , int K ) { long res = 1 ; for ( int p = 2 ; p <= K / p ; p ++ ) { int c = 0 ; while ( K % p == 0 ) { K /= p ; c ++ ; } res = ( res * C ( N - 1 + c , c ) ) % MOD ; } if ( N > 1 ) { res = ( res * N ) % MOD ; } return res ; } int main ( ) { int N = 3 , K = 5 ; cout << countArrays ( N , K ) ; return 0 ; }