codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of given operations required to reduce a number to 2 | C ++ program for the above approach ; Function to find the minimum number of operations required to reduce n to 2 ; Initialize a dp array ; Handle the base case ; Iterate in the range [ 2 , n ] ; Check if i * 5 <= n ; Check if i + 3 <= n ; Return the result ; Driver code ; Given Input ; Function Call ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinOperations ( int n ) { int i , dp [ n + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = 999999 ; } dp [ 2 ] = 0 ; for ( i = 2 ; i < n + 1 ; i ++ ) { if ( i * 5 <= n ) { dp [ i * 5 ] = min ( dp [ i * 5 ] , dp [ i ] + 1 ) ; } if ( i + 3 <= n ) { dp [ i + 3 ] = min ( dp [ i + 3 ] , dp [ i ] + 1 ) ; } } return dp [ n ] ; } int main ( ) { int n = 28 ; int m = findMinOperations ( n ) ; if ( m != 9999 ) cout << m ; else cout << -1 ; return 0 ; }
Number of M | C ++ program for the above approach ; Function to find the number of M - length sorted arrays possible using numbers from the range [ 1 , N ] ; Create an array of size M + 1 ; Base cases ; Fill the dp table ; dp [ j ] will be equal to maximum number of sorted array of size j when elements are taken from 1 to i ; Here dp [ m ] will be equal to the maximum number of sorted arrays when element are taken from 1 to i ; Return the result ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSortedArrays ( int n , int m ) { vector < int > dp ( m + 1 , 0 ) ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ j ] = dp [ j - 1 ] + dp [ j ] ; } } return dp [ m ] ; } int main ( ) { int n = 2 , m = 3 ; cout << countSortedArrays ( n , m ) ; return 0 ; }
Split array into subarrays such that sum of difference between their maximums and minimums is maximum | C ++ program for the above approach ; Function to find maximum sum of difference between maximums and minimums in the splitted subarrays ; Base Case ; Traverse the array ; Stores the maximum and minimum elements upto the i - th index ; Traverse the range [ 0 , i ] ; Update the minimum ; Update the maximum ; Update dp [ i ] ; Return the maximum sum of difference ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getValue ( int arr [ ] , int N ) { int dp [ N ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { int minn = arr [ i ] ; int maxx = arr [ i ] ; for ( int j = i ; j >= 0 ; j -- ) { minn = min ( arr [ j ] , minn ) ; maxx = max ( arr [ j ] , maxx ) ; dp [ i ] = max ( dp [ i ] , maxx - minn + ( ( j >= 1 ) ? dp [ j - 1 ] : 0 ) ) ; } } return dp [ N - 1 ] ; } int main ( ) { int arr [ ] = { 8 , 1 , 7 , 9 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getValue ( arr , N ) ; return 0 ; }
Length of longest subset consisting of A 0 s and B 1 s from an array of strings | C ++ program for the above approach ; Function to count 0 's in a string ; Stores count of 0 s ; Iterate over characters of string ; If current character is '0' ; Recursive function to find the length of longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s ; If idx is equal to N or A + B is equal to 0 ; Stores the count of 0 's in arr[idx] ; Stores the count of 1 's in arr[idx] ; Stores the length of the subset if arr [ i ] is included ; If zero is less than or equal to A and one is less than or equal to B ; Stores the length of the subset if arr [ i ] is excluded ; Returns max of inc and exc ; Function to find the length of the longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s ; Return ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count0 ( string s ) { int count = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == '0' ) { count ++ ; } } return count ; } int solve ( vector < string > vec , int A , int B , int idx ) { if ( idx == vec . size ( ) A + B == 0 ) { return 0 ; } int zero = count0 ( vec [ idx ] ) ; int one = vec [ idx ] . size ( ) - zero ; int inc = 0 ; if ( zero <= A && one <= B ) { inc = 1 + solve ( vec , A - zero , B - one , idx + 1 ) ; } int exc = solve ( vec , A , B , idx + 1 ) ; return max ( inc , exc ) ; } int MaxSubsetlength ( vector < string > arr , int A , int B ) { return solve ( arr , A , B , 0 ) ; } int main ( ) { vector < string > arr = { "1" , "0" , "10" } ; int A = 1 , B = 1 ; cout << MaxSubsetlength ( arr , A , B ) ; return 0 ; }
Maximum score possible by removing substrings made up of single distinct character | C ++ program for the above approach ; Function to check if the string s consists of a single distinct character or not ; Function to calculate the maximum score possible by removing substrings ; If string is empty ; If length of string is 1 ; Store the maximum result ; Try to remove all substrings that satisfy the condition and check for resultant string after removal ; Store the substring { s [ i ] , . . , s [ j ] } ; Check if the substring contains only a single distinct character ; Return the maximum score ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isUnique ( string s ) { set < char > Set ; for ( char c : s ) { Set . insert ( c ) ; } return Set . size ( ) == 1 ; } int maxScore ( string s , int a [ ] ) { int n = s . length ( ) ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; int mx = -1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { string sub = s . substr ( i , j + 1 ) ; if ( isUnique ( sub ) ) mx = max ( mx , a [ sub . length ( ) - 1 ] + maxScore ( s . substr ( 0 , i ) + s . substr ( j + 1 ) , a ) ) ; } } return mx ; } int main ( ) { string s = "011" ; int a [ ] = { 1 , 3 , 1 } ; cout << maxScore ( s , a ) - 1 ; return 0 ; }
Maximum score possible by removing substrings made up of single distinct character | C ++ program for the above approach ; Initialize a dictionary to store the precomputed results ; Function to calculate the maximum score possible by removing substrings ; If s is present in dp [ ] array ; Base Cases : ; If length of string is 0 ; If length of string is 1 ; Put head pointer at start ; Initialize the max variable ; Generate the substrings using two pointers ; If s [ head ] and s [ tail ] are different ; Move head to tail and break ; Store the substring ; Update the maximum ; Move the tail ; Store the score ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; map < string , int > dp ; int maxScore ( string s , vector < int > a ) { if ( dp . find ( s ) != dp . end ( ) ) return dp [ s ] ; int n = s . size ( ) ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; int head = 0 ; int mx = -1 ; while ( head < n ) { int tail = head ; while ( tail < n ) { if ( s [ tail ] != s [ head ] ) { head = tail ; break ; } string sub = s . substr ( head , tail + 1 ) ; mx = max ( mx , a [ sub . size ( ) - 1 ] + maxScore ( s . substr ( 0 , head ) + s . substr ( tail + 1 , s . size ( ) ) , a ) ) ; tail += 1 ; } if ( tail == n ) break ; } dp [ s ] = mx ; return mx ; } int main ( ) { string s = " abb " ; vector < int > a = { 1 , 3 , 1 } ; cout << ( maxScore ( s , a ) - 1 ) ; }
Count all unique outcomes possible by performing S flips on N coins | C ++ program for the above approach ; Dimensions of the DP table ; Stores the dp states ; Function to recursively count the number of unique outcomes possible by performing S flips on N coins ; Base Case ; If the count for the current state is not calculated , then calculate it recursively ; Otherwise return the already calculated value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define size  1001 NEW_LINE int ans [ size ] [ size ] = { 0 } ; int numberOfUniqueOutcomes ( int n , int s ) { if ( s < n ) ans [ n ] [ s ] = 0 ; else if ( n == 1 n == s ) ans [ n ] [ s ] = 1 ; else if ( ! ans [ n ] [ s ] ) { ans [ n ] [ s ] = numberOfUniqueOutcomes ( n - 1 , s - 1 ) + numberOfUniqueOutcomes ( n - 1 , s - 2 ) ; } return ans [ n ] [ s ] ; } int main ( ) { int N = 5 , S = 8 ; cout << numberOfUniqueOutcomes ( N , S ) ; return 0 ; }
Minimize removals to remove another string as a subsequence of a given string | C ++ implementation of the above approach ; Function to print the minimum number of character removals required to remove X as a subsequence from the string str ; Length of the string str ; Length of the string X ; Stores the dp states ; Fill first row of dp [ ] [ ] ; If X [ j ] matches with str [ 0 ] ; If str [ i ] is equal to X [ j ] ; Update state after removing str [ i [ ; Update state after keeping str [ i ] ; If str [ i ] is not equal to X [ j ] ; Print the minimum number of characters removals ; Driver Code ; Input ; Function call to get minimum number of character removals
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMinimumRemovals ( string str , string X ) { int N = str . size ( ) ; int M = X . size ( ) ; int dp [ N ] [ M ] = { } ; for ( int j = 0 ; j < M ; j ++ ) { if ( str [ 0 ] == X [ j ] ) { dp [ 0 ] [ j ] = 1 ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( str [ i ] == X [ j ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 ; if ( j != 0 ) dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } cout << dp [ N - 1 ] [ M - 1 ] ; } int main ( ) { string str = " btagd " ; string X = " bad " ; printMinimumRemovals ( str , X ) ; return 0 ; }
Railway Station | TCS CodeVita 2020 | C ++ program of the above approach ; Dp table for memoization ; Function to count the number of ways to N - th station ; Base Cases ; If current state is already evaluated ; Count ways in which train 1 can be chosen ; Count ways in which train 2 can be chosen ; Count ways in which train 3 can be chosen ; Store the current state ; Return the number of ways ; Driver Code ; Given Input ; Initialize DP table with - 1 ; Function call to count the number of ways to reach the n - th station
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100000 ] ; int findWays ( int x ) { if ( x < 0 ) return 0 ; if ( x == 0 ) return 1 ; if ( x == 1 ) return 2 ; if ( x == 2 ) return 4 ; if ( dp [ x ] != -1 ) return dp [ x ] ; int count = findWays ( x - 1 ) ; count += findWays ( x - 2 ) ; count += findWays ( x - 3 ) ; dp [ x ] = count ; return dp [ x ] ; } int main ( ) { int n = 4 ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << findWays ( n ) << " STRNEWLINE " ; }
Maximum sum not exceeding K possible for any rectangle of a Matrix | C ++ program for the above approach ; Function to find the maximum possible sum of arectangle which is less than K ; Stores the values ( cum_sum - K ) ; Insert 0 into the set sumSet ; Traverse over the rows ; Get cumulative sum from [ 0 to i ] ; Search for upperbound of ( cSum - K ) in the hashmap ; If upper_bound of ( cSum - K ) exists , then update max sum ; Insert cummulative value in the hashmap ; Return the maximum sum which is less than K ; Function to find the maximum sum of rectangle such that its sum is no larger than K ; Stores the number of rows and columns ; Store the required result ; Set the left column ; Set the right column for the left column set by outer loop ; Calculate sum between the current left and right for every row ; Stores the sum of rectangle ; Update the overall maximum sum ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubarraySum ( vector < int > & sum , int k , int row ) { int curSum = 0 , curMax = INT_MIN ; set < int > sumSet ; sumSet . insert ( 0 ) ; for ( int r = 0 ; r < row ; ++ r ) { curSum += sum [ r ] ; auto it = sumSet . lower_bound ( curSum - k ) ; if ( it != sumSet . end ( ) ) { curMax = max ( curMax , curSum - * it ) ; } sumSet . insert ( curSum ) ; } return curMax ; } void maxSumSubmatrix ( vector < vector < int > > & matrix , int k ) { int row = matrix . size ( ) ; int col = matrix [ 0 ] . size ( ) ; int ret = INT_MIN ; for ( int i = 0 ; i < col ; ++ i ) { vector < int > sum ( row , 0 ) ; for ( int j = i ; j < col ; ++ j ) { for ( int r = 0 ; r < row ; ++ r ) { sum [ r ] += matrix [ r ] [ j ] ; } int curMax = maxSubarraySum ( sum , k , row ) ; ret = max ( ret , curMax ) ; } } cout << ret ; } int main ( ) { vector < vector < int > > matrix = { { 1 , 0 , 1 } , { 0 , -2 , 3 } } ; int K = 2 ; maxSumSubmatrix ( matrix , K ) ; return 0 ; }
Maximum sum submatrix | C ++ program for the above approach ; Function to find maximum continuous maximum sum in the array ; Stores current and maximum sum ; Traverse the array v ; Add the value of the current element ; Update the maximum sum ; Return the maximum sum ; Function to find the maximum submatrix sum ; Store the rows and columns of the matrix ; Create an auxiliary matrix ; Traverse the matrix , prefix and initialize it will all 0 s ; Calculate prefix sum of all rows of matrix A [ ] [ ] and store in matrix prefix [ ] ; Update the prefix [ ] [ ] ; Store the maximum submatrix sum ; Iterate for starting column ; Iterate for last column ; To store current array elements ; Traverse every row ; Store the sum of the kth row ; Update the prefix sum ; Push it in a vector ; Update the maximum overall sum ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kadane ( vector < int > v ) { int currSum = 0 ; int maxSum = INT_MIN ; for ( int i = 0 ; i < ( int ) v . size ( ) ; i ++ ) { currSum += v [ i ] ; if ( currSum > maxSum ) { maxSum = currSum ; } if ( currSum < 0 ) { currSum = 0 ; } } return maxSum ; } void maxSubmatrixSum ( vector < vector < int > > A ) { int r = A . size ( ) ; int c = A [ 0 ] . size ( ) ; int * * prefix = new int * [ r ] ; for ( int i = 0 ; i < r ; i ++ ) { prefix [ i ] = new int ; for ( int j = 0 ; j < c ; j ++ ) { prefix [ i ] [ j ] = 0 ; } } for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { if ( j == 0 ) prefix [ i ] [ j ] = A [ i ] [ j ] ; else prefix [ i ] [ j ] = A [ i ] [ j ] + prefix [ i ] [ j - 1 ] ; } } int maxSum = INT_MIN ; for ( int i = 0 ; i < c ; i ++ ) { for ( int j = i ; j < c ; j ++ ) { vector < int > v ; for ( int k = 0 ; k < r ; k ++ ) { int el = 0 ; if ( i == 0 ) el = prefix [ k ] [ j ] ; else el = prefix [ k ] [ j ] - prefix [ k ] [ i - 1 ] ; v . push_back ( el ) ; } maxSum = max ( maxSum , kadane ( v ) ) ; } } cout << maxSum << " STRNEWLINE " ; } int main ( ) { vector < vector < int > > matrix = { { 0 , -2 , -7 , 0 } , { 9 , 2 , -6 , 2 } , { -4 , 1 , -4 , 1 } , { -1 , 8 , 0 , -2 } } ; maxSubmatrixSum ( matrix ) ; return 0 ; }
Minimize cost of painting N houses such that adjacent houses have different colors | C ++ program for the above approach ; Function to find the minimum cost of coloring the houses such that no two adjacent houses has the same color ; Corner Case ; Auxiliary 2D dp array ; Base Case ; If current house is colored with red the take min cost of previous houses colored with ( blue and green ) ; If current house is colored with blue the take min cost of previous houses colored with ( red and green ) ; If current house is colored with green the take min cost of previous houses colored with ( red and blue ) ; Print the min cost of the last painted house ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( vector < vector < int > > & costs , int N ) { if ( N == 0 ) return 0 ; vector < vector < int > > dp ( N , vector < int > ( 3 , 0 ) ) ; dp [ 0 ] [ 0 ] = costs [ 0 ] [ 0 ] ; dp [ 0 ] [ 1 ] = costs [ 0 ] [ 1 ] ; dp [ 0 ] [ 2 ] = costs [ 0 ] [ 2 ] ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 0 ] ; dp [ i ] [ 1 ] = min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 1 ] ; dp [ i ] [ 2 ] = min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + costs [ i ] [ 2 ] ; } cout << min ( dp [ N - 1 ] [ 0 ] , min ( dp [ N - 1 ] [ 1 ] , dp [ N - 1 ] [ 2 ] ) ) ; } int main ( ) { vector < vector < int > > costs { { 14 , 2 , 11 } , { 11 , 14 , 5 } , { 14 , 3 , 10 } } ; int N = costs . size ( ) ; minCost ( costs , N ) ; return 0 ; }
Minimize count of flips required to make sum of the given array equal to 0 | C ++ program for the above approach ; Initialize dp [ ] [ ] ; Function to find the minimum number of operations to make sum of A [ ] 0 ; Initialize answer ; Base case ; Otherwise , return 0 ; Pre - computed subproblem ; Recurrence relation for finding the minimum of the sum of subsets ; Return the result ; Function to find the minimum number of elements required to be flipped to amke sum the array equal to 0 ; Find the sum of array ; Initialise dp [ ] [ ] with - 1 ; No solution exists ; Otherwise ; If sum is odd , no subset is possible ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 2001 ] [ 2001 ] ; int solve ( vector < int > & A , int i , int sum , int N ) { int res = 2001 ; if ( sum < 0 or ( i == N and sum != 0 ) ) { return 2001 ; } if ( sum == 0 or i >= N ) { return dp [ i ] [ sum ] = 0 ; } if ( dp [ i ] [ sum ] != -1 ) { return dp [ i ] [ sum ] ; } res = min ( solve ( A , i + 1 , sum - A [ i ] , N ) + 1 , solve ( A , i + 1 , sum , N ) ) ; return dp [ i ] [ sum ] = res ; } void minOp ( vector < int > & A , int N ) { int sum = 0 ; for ( auto it : A ) { sum += it ; } if ( sum % 2 == 0 ) { memset ( dp , -1 , sizeof ( dp ) ) ; int ans = solve ( A , 0 , sum / 2 , N ) ; if ( ans < 0 ans > N ) { cout << " - 1" << endl ; } else { cout << ans << endl ; } } else { cout << " - 1" << endl ; } } int main ( ) { vector < int > A = { 2 , 3 , 1 , 4 } ; int N = A . size ( ) ; minOp ( A , N ) ; return 0 ; }
Count subsequences having average of its elements equal to K | C ++ program for the above approach ; Stores the dp states ; Function to find the count of subsequences having average K ; Base condition ; Three loops for three states ; Recurrence relation ; Stores the sum of dp [ n ] [ j ] [ K * j ] all possible values of j with average K and sum K * j ; Iterate over the range [ 1 , N ] ; Return the final count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 101 ] [ 101 ] [ 1001 ] ; int countAverage ( int n , int K , int * arr ) { dp [ 0 ] [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = 0 ; k < n ; k ++ ) { for ( int s = 0 ; s <= 1000 ; s ++ ) { dp [ i + 1 ] [ k + 1 ] [ s + arr [ i ] ] += dp [ i ] [ k ] [ s ] ; dp [ i + 1 ] [ k ] [ s ] += dp [ i ] [ k ] [ s ] ; } } } int cnt = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { cnt += dp [ n ] [ j ] [ K * j ] ; } return cnt ; } int main ( ) { int arr [ ] = { 9 , 7 , 8 , 9 } ; int K = 8 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countAverage ( N , K , arr ) ; return 0 ; }
Count ways to remove pairs from a matrix such that remaining elements can be grouped in vertical or horizontal pairs | C ++ program for the above approach ; Function to count ways to remove pairs such that the remaining elements can be arranged in pairs vertically or horizontally ; Store the size of matrix ; If N is odd , then no such pair exists ; Store the number of required pairs ; Initialize an auxiliary matrix and fill it with 0 s ; Traverse the matrix v [ ] [ ] ; Check if i + j is odd or even ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 0 ] by 1 ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 1 ] by 1 ; Iterate in range [ 0 , k - 1 ] using i ; Iterate in range [ i + 1 , k - 1 ] using j ; Update the ans ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void numberofpairs ( vector < vector < int > > v , int k ) { int n = v . size ( ) ; if ( n % 2 == 1 ) { cout << 0 ; return ; } int ans = 0 ; int dp [ k ] [ 2 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( i + j ) % 2 == 0 ) dp [ v [ i ] [ j ] - 1 ] [ 0 ] ++ ; else dp [ v [ i ] [ j ] - 1 ] [ 1 ] ++ ; } } for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i + 1 ; j < k ; j ++ ) { ans += dp [ i ] [ 0 ] * dp [ j ] [ 1 ] ; ans += dp [ i ] [ 1 ] * dp [ j ] [ 0 ] ; } } cout << ans ; } int main ( ) { vector < vector < int > > mat = { { 1 , 2 } , { 3 , 4 } } ; int K = 4 ; numberofpairs ( mat , K ) ; return 0 ; }
Maximize sum by selecting X different | C ++ program for the above approach ; Store overlapping subproblems of the recurrence relation ; Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; If the subproblem already computed ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 50 ] [ 50 ] [ 50 ] [ 50 ] ; int FindMaxS ( int X , int Y , int Z , int n , vector < int > & A , vector < int > & B , vector < int > & C ) { if ( X < 0 or Y < 0 or Z < 0 ) return INT_MIN ; if ( n < 0 ) return 0 ; if ( dp [ n ] [ X ] [ Y ] [ Z ] != -1 ) { return dp [ n ] [ X ] [ Y ] [ Z ] ; } int ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; int ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; int co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; int no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; int maximum = max ( ch , max ( ca , max ( co , no ) ) ) ; return dp [ n ] [ X ] [ Y ] [ Z ] = maximum ; } int main ( ) { int X = 1 ; int Y = 1 ; int Z = 1 ; vector < int > A = { 10 , 0 , 5 } ; vector < int > B = { 5 , 10 , 0 } ; vector < int > C = { 0 , 5 , 10 } ; int n = B . size ( ) ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; }
Minimize swaps of adjacent characters to sort every possible rearrangement of given Binary String | C ++ program for the above approach ; Precalculate the values of power of 2 ; Function to calculate 2 ^ N % mod ; Function to find sum of inversions ; Initialise a list of 0 s and ? s ; Traverse the string in the reversed manner ; If the current character is 1 ; Effectively calculate a * b ^ ( b - 1 ) ; If the current character is 0 ; Increment count of zeroes ; Double count the zeroes ; Find b * 2 ^ ( b - 1 ) ; Increment count of questions ; Return the final count ; Driver Code ; Given string S ; Function Call
#include <bits/stdc++.h> NEW_LINE #define MOD  1000000007 NEW_LINE using namespace std ; vector < int > MEM = { 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 } ; int mod_pow2 ( int n ) { while ( n >= MEM . size ( ) ) MEM . push_back ( ( MEM [ -1 ] * 2 ) % MOD ) ; return MEM [ n ] ; } int inversions ( string bstr ) { int total = 0 , zeros = 0 , questions = 0 ; reverse ( bstr . begin ( ) , bstr . end ( ) ) ; for ( char x : bstr ) { int q ; if ( x == '1' ) { int z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; } else if ( x == '0' ) { zeros += 1 ; } else { total *= 2 ; int z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; questions += 1 ; } } return total ; } int main ( ) { string S = " ? 0 ? " ; cout << inversions ( S ) ; }