codeparrot/xlcost-text-to-code huggingface dataset:
Total numbers with no repeated digits in a range | C ++ implementation of above idea ; Maximum ; Prefix Array ; Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to pre calculate the Prefix array ; Traversing through the numbers from 2 to MAX ; Generating the Prefix array ; Calclute Function ; Answer ; Driver code ; Pre - calculating the Prefix array . ; Calling the calculate function to find the total number of number which has no repeated digit
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MAX = 1000 ; vector < int > Prefix = { 0 } ; int repeated_digit ( int n ) { unordered_set < int > a ; int d ; while ( n != 0 ) { d = n % 10 ; if ( a . find ( d ) != a . end ( ) ) return 0 ; a . insert ( d ) ; n = n / 10 ; } return 1 ; } void pre_calculation ( int MAX ) { Prefix . push_back ( repeated_digit ( 1 ) ) ; for ( int i = 2 ; i < MAX + 1 ; i ++ ) Prefix . push_back ( repeated_digit ( i ) + Prefix [ i - 1 ] ) ; } int calculate ( int L , int R ) { return Prefix [ R ] - Prefix [ L - 1 ] ; } int main ( ) { int L = 1 , R = 100 ; pre_calculation ( MAX ) ; cout << calculate ( L , R ) << endl ; return 0 ; }
Difference Array | Range update query in O ( 1 ) | C ++ code to demonstrate Difference Array ; Creates a diff array D [ ] for A [ ] and returns it after filling initial values . ; We use one extra space because update ( l , r , x ) updates D [ r + 1 ] ; Does range update ; Prints updated Array ; Note that A [ 0 ] or D [ 0 ] decides values of rest of the elements . ; Driver Code ; Array to be updated ; Create and fill difference Array ; After below update ( l , r , x ) , the elements should become 20 , 15 , 20 , 40 ; After below updates , the array should become 30 , 35 , 70 , 60
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > initializeDiffArray ( vector < int > & A ) { int n = A . size ( ) ; vector < int > D ( n + 1 ) ; D [ 0 ] = A [ 0 ] , D [ n ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) D [ i ] = A [ i ] - A [ i - 1 ] ; return D ; } void update ( vector < int > & D , int l , int r , int x ) { D [ l ] += x ; D [ r + 1 ] -= x ; } int printArray ( vector < int > & A , vector < int > & D ) { for ( int i = 0 ; i < A . size ( ) ; i ++ ) { if ( i == 0 ) A [ i ] = D [ i ] ; else A [ i ] = D [ i ] + A [ i - 1 ] ; cout << A [ i ] << " ▁ " ; } cout << endl ; } int main ( ) { vector < int > A { 10 , 5 , 20 , 40 } ; vector < int > D = initializeDiffArray ( A ) ; update ( D , 0 , 1 , 10 ) ; printArray ( A , D ) ; update ( D , 1 , 3 , 20 ) ; update ( D , 2 , 2 , 30 ) ; printArray ( A , D ) ; return 0 ; }
Find the value of the function Y = ( X ^ 6 + X ^ 2 + 9894845 ) % 971 | CPP implementation of above approach ; computing ( a ^ b ) % c ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int modpow ( long long int base , long long int exp , long long int modulus ) { base %= modulus ; long long int result = 1 ; while ( exp > 0 ) { if ( exp & 1 ) result = ( result * base ) % modulus ; base = ( base * base ) % modulus ; exp >>= 1 ; } return result ; } int main ( ) { long long int n = 654654 , mod = 971 ; cout << ( ( ( modpow ( n , 6 , mod ) + modpow ( n , 2 , mod ) ) % mod + 355 ) % mod ) ; return 0 ; }
Largest Sum Contiguous Subarray | C ++ program to print largest contiguous array sum ; Driver program to test maxSubArraySum
#include <iostream> NEW_LINE #include <climits> NEW_LINE using namespace std ; int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } int main ( ) { int a [ ] = { -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int max_sum = maxSubArraySum ( a , n ) ; cout << " Maximum ▁ contiguous ▁ sum ▁ is ▁ " << max_sum ; return 0 ; }
Place N ^ 2 numbers in matrix such that every row has an equal sum | C ++ program to distribute n ^ 2 numbers to n people ; 2D array for storing the final result ; Using modulo to go to the firs column after the last column ; Making a 2D array containing numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > solve ( vector < vector < int > > arr , int n ) { vector < vector < int > > ans ( n , vector < int > ( n , 0 ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { ans [ i ] [ j ] = arr [ j ] [ ( i + j ) % n ] ; } } return ans ; } void show ( vector < vector < int > > arr , int n ) { vector < vector < int > > res = solve ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { cout << res [ i ] [ j ] << " ▁ " ; } cout << endl ; } } vector < vector < int > > makeArray ( int n ) { vector < vector < int > > arr ( n , vector < int > ( n , 0 ) ) ; int c = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { arr [ i ] [ j ] = c ; c ++ ; } } return arr ; } int main ( ) { int n = 5 ; vector < vector < int > > arr = makeArray ( n ) ; show ( arr , n ) ; return 0 ; }
Largest Sum Contiguous Subarray |  ; Else start the max subarry from current element
#include <climits> NEW_LINE int maxSubarraySum ( int arr [ ] , int size ) { int max_ending_here = 0 , max_so_far = INT_MIN ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] <= max_ending_here + arr [ i ] ) { max_ending_here += arr [ i ] ; } else { max_ending_here = arr [ i ] ; } if ( max_ending_here > max_so_far ) max_so_far = max_ending_here ; } return max_so_far ; }
Minimum rooms for m events of n batches with given schedule | CPP program to find minimum number of rooms required ; Returns minimum number of rooms required to perform classes of n groups in m slots with given schedule . ; Store count of classes happening in every slot . ; initialize all values to zero ; Number of rooms required is equal to maximum classes happening in a particular slot . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinRooms ( string slots [ ] , int n , int m ) { int counts [ m ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( slots [ i ] [ j ] == '1' ) counts [ j ] ++ ; return * max_element ( counts , counts + m ) ; } int main ( ) { int n = 3 , m = 7 ; string slots [ n ] = { "0101011" , "0011001" , "0110111" } ; cout << findMinRooms ( slots , n , m ) ; return 0 ; }
Largest Sum Contiguous Subarray |  ; Driver program to test maxSubArraySum
#include <iostream> NEW_LINE using namespace std ; int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = a [ 0 ] ; int curr_max = a [ 0 ] ; for ( int i = 1 ; i < size ; i ++ ) { curr_max = max ( a [ i ] , curr_max + a [ i ] ) ; max_so_far = max ( max_so_far , curr_max ) ; } return max_so_far ; } int main ( ) { int a [ ] = { -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int max_sum = maxSubArraySum ( a , n ) ; cout << " Maximum ▁ contiguous ▁ sum ▁ is ▁ " << max_sum ; return 0 ; }
Largest Sum Contiguous Subarray | C ++ program to print largest contiguous array sum ; Driver program to test maxSubArraySum
#include <iostream> NEW_LINE #include <climits> NEW_LINE using namespace std ; int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = INT_MIN , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = i ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; s = i + 1 ; } } cout << " Maximum ▁ contiguous ▁ sum ▁ is ▁ " << max_so_far << endl ; cout << " Starting ▁ index ▁ " << start << endl << " Ending ▁ index ▁ " << end << endl ; } int main ( ) { int a [ ] = { -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int max_sum = maxSubArraySum ( a , n ) ; return 0 ; }
Find minimum number of currency notes and values that sum to given amount | C ++ program to accept an amount and count number of notes ; function to count and print currency notes ; count notes using Greedy approach ; Print notes ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countCurrency ( int amount ) { int notes [ 9 ] = { 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 } ; int noteCounter [ 9 ] = { 0 } ; for ( int i = 0 ; i < 9 ; i ++ ) { if ( amount >= notes [ i ] ) { noteCounter [ i ] = amount / notes [ i ] ; amount = amount - noteCounter [ i ] * notes [ i ] ; } } cout << " Currency ▁ Count ▁ - > " << endl ; for ( int i = 0 ; i < 9 ; i ++ ) { if ( noteCounter [ i ] != 0 ) { cout << notes [ i ] << " ▁ : ▁ " << noteCounter [ i ] << endl ; } } } int main ( ) { int amount = 868 ; countCurrency ( amount ) ; return 0 ; }
Minimum sum by choosing minimum of pairs from array | CPP program to minimize the cost of array minimization ; Returns minimum possible sum in array B [ ] ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSum ( int A [ ] , int n ) { int min_val = * min_element ( A , A + n ) ; return ( min_val * ( n - 1 ) ) ; } int main ( ) { int A [ ] = { 3 , 6 , 2 , 8 , 7 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minSum ( A , n ) ; return 0 ; }
Program for Next Fit algorithm in Memory Management | C / C ++ program for next fit memory management algorithm ; Function to allocate memory to blocks as per Next 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 ; Do not start from beginning ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; mod m will help in traversing the blocks from starting block after we reach the end . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void NextFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] , j = 0 ; memset ( allocation , -1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ] = j ; blockSize [ j ] -= processSize [ i ] ; break ; } j = ( j + 1 ) % m ; } } 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 [ ] = { 5 , 10 , 20 } ; int processSize [ ] = { 10 , 20 , 5 } ; int m = sizeof ( blockSize ) / sizeof ( blockSize [ 0 ] ) ; int n = sizeof ( processSize ) / sizeof ( processSize [ 0 ] ) ; NextFit ( blockSize , m , processSize , n ) ; return 0 ; }
Maximum profit by buying and selling a share at most twice |  ; adding array ; 80 / 30 / / \ 25 / 15 / / \ / 2 10 / \ / 8 ; traversing through array from ( i + 1 ) th position
#include <iostream> NEW_LINE using namespace std ; int main ( ) { int price [ ] = { 2 , 30 , 15 , 10 , 8 , 25 , 80 } ; int n = 7 ; int profit = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int sub = price [ i ] - price [ i - 1 ] ; if ( sub > 0 ) profit += sub ; } cout << " Maximum ▁ Profit = " << profit ; return 0 ; }
Find if there is a pair in root to a leaf path with sum equals to root 's data | C ++ program to find if there is a pair in any root to leaf path with sum equals to root 's key. ; A binary tree node has data , pointer to left child and a pointer to right child ; utility that allocates a new node with the given data and NULL left and right pointers . ; Function to print root to leaf path which satisfies the condition ; Base condition ; Check if current node makes a pair with any of the existing elements in set . ; Insert current node in set ; If result returned by either left or right child is true , return true . ; Remove current node from hash table ; A wrapper over printPathUtil ( ) ; create an empty hash table ; Recursively check in left and right subtrees . ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newnode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } bool printPathUtil ( Node * node , unordered_set < int > & s , int root_data ) { if ( node == NULL ) return false ; int rem = root_data - node -> data ; if ( s . find ( rem ) != s . end ( ) ) return true ; s . insert ( node -> data ) ; bool res = printPathUtil ( node -> left , s , root_data ) || printPathUtil ( node -> right , s , root_data ) ; s . erase ( node -> data ) ; return res ; } bool isPathSum ( Node * root ) { unordered_set < int > s ; return printPathUtil ( root -> left , s , root -> data ) || printPathUtil ( root -> right , s , root -> data ) ; } int main ( ) { struct Node * root = newnode ( 8 ) ; root -> left = newnode ( 5 ) ; root -> right = newnode ( 4 ) ; root -> left -> left = newnode ( 9 ) ; root -> left -> right = newnode ( 7 ) ; root -> left -> right -> left = newnode ( 1 ) ; root -> left -> right -> right = newnode ( 12 ) ; root -> left -> right -> right -> right = newnode ( 2 ) ; root -> right -> right = newnode ( 11 ) ; root -> right -> right -> left = newnode ( 3 ) ; isPathSum ( root ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Find the subarray with least average | A Simple C ++ program to find minimum average subarray ; Prints beginning and ending indexes of subarray of size k with minimum average ; k must be smaller than or equal to n ; Initialize beginning index of result ; Compute sum of first subarray of size k ; Initialize minimum sum as current sum ; Traverse from ( k + 1 ) ' th ▁ element ▁ to ▁ n ' th element ; Add current item and remove first item of previous subarray ; Update result if needed ; Driver program ; Subarray size
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinAvgSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; int min_sum = curr_sum ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr_sum < min_sum ) { min_sum = curr_sum ; res_index = ( i - k + 1 ) ; } } cout << " Subarray ▁ between ▁ [ " << res_index << " , ▁ " << res_index + k - 1 << " ] ▁ has ▁ minimum ▁ average " ; } int main ( ) { int arr [ ] = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int k = 3 ; int n = sizeof arr / sizeof arr [ 0 ] ; findMinAvgSubarray ( arr , n , k ) ; return 0 ; }
Find element using minimum segments in Seven Segment Display | C ++ program to find minimum number of segments required ; Precomputed values of segment used by digit 0 to 9. ; Return the number of segments used by x . ; Finding sum of the segment used by each digit of a number . ; Initialising the minimum segment and minimum number index . ; Finding and comparing segment used by each number arr [ i ] . ; If arr [ i ] used less segment then update minimum segment and minimum number . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int seg [ 10 ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int computeSegment ( int x ) { if ( x == 0 ) return seg [ 0 ] ; int count = 0 ; while ( x ) { count += seg [ x % 10 ] ; x /= 10 ; } return count ; } int elementMinSegment ( int arr [ ] , int n ) { int minseg = computeSegment ( arr [ 0 ] ) ; int minindex = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int temp = computeSegment ( arr [ i ] ) ; if ( temp < minseg ) { minseg = temp ; minindex = i ; } } return arr [ minindex ] ; } int main ( ) { int arr [ ] = { 489 , 206 , 745 , 123 , 756 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << elementMinSegment ( arr , n ) << endl ; return 0 ; }
Find the Largest number with given number of digits and sum of digits | C ++ program to find the largest number that can be formed from given sum of digits and number of digits . ; Prints the smalles possible number with digit sum ' s ' and ' m ' number of digits . ; If sum of digits is 0 , then a number is possible only if number of digits is 1. ; Sum greater than the maximum possible sum . ; Create an array to store digits of result ; Fill from most significant digit to least significant digit . ; Fill 9 first to make the number largest ; If remaining sum becomes less than 9 , then fill the remaining sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; void findLargest ( int m , int s ) { if ( s == 0 ) { ( m == 1 ) ? cout << " Largest ▁ number ▁ is ▁ " << 0 : cout << " Not ▁ possible " ; return ; } if ( s > 9 * m ) { cout << " Not ▁ possible " ; return ; } int res [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { if ( s >= 9 ) { res [ i ] = 9 ; s -= 9 ; } else { res [ i ] = s ; s = 0 ; } } cout << " Largest ▁ number ▁ is ▁ " ; for ( int i = 0 ; i < m ; i ++ ) cout << res [ i ] ; } int main ( ) { int s = 9 , m = 2 ; findLargest ( m , s ) ; return 0 ; }
Shortest Superstring Problem | C ++ program to find shortest superstring using Greedy Approximate Algorithm ; Utility function to calculate minimum of two numbers ; Function to calculate maximum overlap in two given strings ; Max will store maximum overlap i . e maximum length of the matching prefix and suffix ; Check suffix of str1 matches with prefix of str2 ; Compare last i characters in str1 with first i characters in str2 ; Update max and str ; Check prefix of str1 matches with suffix of str2 ; compare first i characters in str1 with last i characters in str2 ; Update max and str ; Function to calculate smallest string that contains each string in the given set as substring . ; Run len - 1 times to consider every pair ; To store maximum overlap ; To store array index of strings ; to store resultant string after maximum overlap ; res will store maximum length of the matching prefix and suffix str is passed by reference and will store the resultant string after maximum overlap of arr [ i ] and arr [ j ] , if any . ; check for maximum overlap ; Ignore last element in next cycle ; If no overlap , append arr [ len ] to arr [ 0 ] ; Copy resultant string to index l ; Copy string at last index to index r ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min ( int a , int b ) { return ( a < b ) ? a : b ; } int findOverlappingPair ( string str1 , string str2 , string & str ) { int max = INT_MIN ; int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; for ( int i = 1 ; i <= min ( len1 , len2 ) ; i ++ ) { if ( str1 . compare ( len1 - i , i , str2 , 0 , i ) == 0 ) { if ( max < i ) { max = i ; str = str1 + str2 . substr ( i ) ; } } } for ( int i = 1 ; i <= min ( len1 , len2 ) ; i ++ ) { if ( str1 . compare ( 0 , i , str2 , len2 - i , i ) == 0 ) { if ( max < i ) { max = i ; str = str2 + str1 . substr ( i ) ; } } } return max ; } string findShortestSuperstring ( string arr [ ] , int len ) { while ( len != 1 ) { int max = INT_MIN ; int l , r ; string resStr ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { string str ; int res = findOverlappingPair ( arr [ i ] , arr [ j ] , str ) ; if ( max < res ) { max = res ; resStr . assign ( str ) ; l = i , r = j ; } } } len -- ; if ( max == INT_MIN ) arr [ 0 ] += arr [ len ] ; else { arr [ l ] = resStr ; arr [ r ] = arr [ len ] ; } } return arr [ 0 ] ; } int main ( ) { string arr [ ] = { " catgc " , " ctaagt " , " gcta " , " ttca " , " atgcatc " } ; int len = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " The ▁ Shortest ▁ Superstring ▁ is ▁ " << findShortestSuperstring ( arr , len ) ; return 0 ; }
Minimum number of jumps to reach end | C ++ program to find Minimum number of jumps to reach end ; Function to return the minimum number of jumps to reach arr [ h ] from arr [ l ] ; Base case : when source and destination are same ; Traverse through all the points reachable from arr [ l ] Recursively , get the minimum number of jumps needed to reach arr [ h ] from these reachable points ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minJumps ( int arr [ ] , int n ) { if ( n == 1 ) return 0 ; int res = INT_MAX ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( i + arr [ i ] >= n - 1 ) { int sub_res = minJumps ( arr , i + 1 ) ; if ( sub_res != INT_MAX ) res = min ( res , sub_res + 1 ) ; } } return res ; } int main ( ) { int arr [ ] = { 1 , 3 , 6 , 3 , 2 , 3 , 6 , 8 , 9 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ number ▁ of ▁ jumps ▁ to " ; cout << " ▁ reach ▁ the ▁ end ▁ is ▁ " << minJumps ( arr , n ) ; return 0 ; }
Camel and Banana Puzzle | DP | C ++ program of the above approach ; Stores the overlapping state ; Recursive function to find the maximum number of bananas that can be transferred to A distance using memoization ; Base Case where count of bananas is less that the given distance ; Base Case where count of bananas is less that camel 's capacity ; Base Case where distance = 0 ; If the current state is already calculated ; Stores the maximum count of bananas ; Stores the number of trips to transfer B bananas using a camel of capacity C ; Loop to iterate over all the breakpoints in range [ 1 , A ] ; Recursive call over the remaining path ; Update the maxCount ; Memoize the current value ; Return answer ; Function to find the maximum number of bananas that can be transferred ; Initialize dp array with - 1 ; Function Call ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 1001 ] [ 3001 ] ; int recBananaCnt ( int A , int B , int C ) { if ( B <= A ) { return 0 ; } if ( B <= C ) { return B - A ; } if ( A == 0 ) { return B ; } if ( dp [ A ] [ B ] != -1 ) { return dp [ A ] [ B ] ; } int maxCount = INT_MIN ; int tripCount = B % C == 0 ? ( ( 2 * B ) / C ) - 1 : ( ( 2 * B ) / C ) + 1 ; for ( int i = 1 ; i <= A ; i ++ ) { int curCount = recBananaCnt ( A - i , B - tripCount * i , C ) ; if ( curCount > maxCount ) { maxCount = curCount ; dp [ A ] [ B ] = maxCount ; } } return maxCount ; } int maxBananaCnt ( int A , int B , int C ) { memset ( dp , -1 , sizeof ( dp ) ) ; return recBananaCnt ( A , B , C ) ; } int main ( ) { int A = 1000 ; int B = 3000 ; int C = 1000 ; cout << maxBananaCnt ( A , B , C ) ; return 0 ; }
Count of valid arrays of size P with elements in range [ 1 , N ] having duplicates at least M distance apart | C ++ program for the above approach ; Function to calculate the total number of arrays ; If the size of the array is P ; Check if all elements are used atlease once ; Check if this state is already calculated ; Initialize the result ; Use a number from the list of unused numbers ; There are ' unused ' number of favourable choices ; Use a number from already present number atlease M distance back ; There are ' used ▁ - ▁ M ' number of favourable choices ; Store the result ; Function to solve the problem ; Initialize DP table : dp [ i ] [ j ] [ j ] i : current position / index j : number of used elements k : number of unused elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculate ( int position , int used , int unused , int P , int M , vector < vector < vector < int > > > & dp ) { if ( position == P ) { return unused == 0 ? 1 : 0 ; } if ( dp [ position ] [ used ] [ unused ] != -1 ) return dp [ position ] [ used ] [ unused ] ; int result = 0 ; if ( unused > 0 ) { result += calculate ( position + 1 , used + 1 , unused - 1 , P , M , dp ) * unused ; } if ( used > M ) { result += calculate ( position + 1 , used , unused , P , M , dp ) * ( used - M ) ; } return dp [ position ] [ used ] [ unused ] = result ; } int solve ( int N , int P , int M ) { vector < vector < vector < int > > > dp ( 101 , vector < vector < int > > ( 101 , vector < int > ( 101 , -1 ) ) ) ; return calculate ( 0 , 0 , N , P , M , dp ) ; } int main ( ) { int N = 2 , M = 0 , P = 3 ; cout << solve ( N , P , M ) ; }