codeparrot/xlcost-text-to-code huggingface dataset:
Count all unique outcomes possible by performing S flips on N coins | C ++ program for the above approach ; Function to recursively count the number of unique outcomes possible S flips are performed on N coins ; Base Cases ; Recursive Calls ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfUniqueOutcomes ( int N , int S ) { if ( S < N ) return 0 ; if ( N == 1 N == S ) return 1 ; return ( numberOfUniqueOutcomes ( N - 1 , S - 1 ) + numberOfUniqueOutcomes ( N - 1 , S - 2 ) ) ; } int main ( ) { int N = 3 , S = 4 ; cout << numberOfUniqueOutcomes ( N , S ) ; return 0 ; }
Length of longest increasing subsequence in a string | C ++ program for the above approach ; Function to find length of longest increasing subsequence in a string ; Stores at every i - th index , the length of the longest increasing subsequence ending with character i ; Size of string ; Stores the length of LIS ; Iterate over each character of the string ; Store position of the current character ; Stores the length of LIS ending with current character ; Check for all characters less then current character ; Include current character ; Update length of longest increasing subsequence ; Updating LIS for current character ; Return the length of LIS ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lisOtimised ( string s ) { int dp [ 30 ] = { 0 } ; int N = s . size ( ) ; int lis = INT_MIN ; for ( int i = 0 ; i < N ; i ++ ) { int val = s [ i ] - ' a ' ; int curr = 0 ; for ( int j = 0 ; j < val ; j ++ ) { curr = max ( curr , dp [ j ] ) ; } curr ++ ; lis = max ( lis , curr ) ; dp [ val ] = max ( dp [ val ] , curr ) ; } return lis ; } int main ( ) { string s = " fdryutiaghfse " ; cout << lisOtimised ( s ) ; return 0 ; }
Sum of length of two smallest subsets possible from a given array with sum at least K | C ++ program for the above approach ; Function to calculate sum of lengths of two smallest subsets with sum >= K ; Sort the array in ascending order ; Stores suffix sum of the array ; Update the suffix sum array ; Stores all dp - states ; Initialize all dp - states with a maximum possible value ; Base Case ; Traverse the array arr [ ] ; Iterate over the range [ 0 , K ] ; If A [ i ] is equal to at least the required sum j for the current state ; If the next possible state doesn 't exist ; Otherwise , update the current state to the minimum of the next state and state including the current element A [ i ] ; Traverse the suffix sum array ; If suffix [ i ] - dp [ i ] [ K ] >= K ; Sum of lengths of the two smallest subsets is obtained ; Return - 1 , if there doesn 't  exist any subset of sum >= K ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1e9 ; int MinimumLength ( int A [ ] , int N , int K ) { sort ( A , A + N ) ; int suffix [ N + 1 ] = { 0 } ; for ( int i = N - 1 ; i >= 0 ; i -- ) suffix [ i ] = suffix [ i + 1 ] + A [ i ] ; int dp [ N + 1 ] [ K + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) for ( int j = 0 ; j <= K ; j ++ ) dp [ i ] [ j ] = MAX ; dp [ N ] [ 0 ] = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = K ; j >= 0 ; j -- ) { if ( j <= A [ i ] ) { dp [ i ] [ j ] = A [ i ] ; continue ; } if ( dp [ i + 1 ] [ j - A [ i ] ] == MAX ) dp [ i ] [ j ] = MAX ; else dp [ i ] [ j ] = min ( dp [ i + 1 ] [ j ] , dp [ i + 1 ] [ j - A [ i ] ] + A [ i ] ) ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( suffix [ i ] - dp [ i ] [ K ] >= K ) { return N - i ; } } return -1 ; } int main ( ) { int arr [ ] = { 7 , 4 , 5 , 6 , 8 } ; int K = 13 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MinimumLength ( arr , N , K ) ; return 0 ; }
Count distinct possible Bitwise XOR values of subsets of an array | C ++ program for the above approach ; Stores the mask of the vector ; Stores the current size of dp [ ] ; Function to store the mask of given integer ; Iterate over the range [ 0 , 20 ] ; If i - th bit 0 ; If dp [ i ] is zero ; Store the position in dp ; Increment the answer ; Return from the loop ; mask = mask XOR dp [ i ] ; Function to find the size of the set having Bitwise XOR of all the subset of the given array ; Traverse the array ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int const size = 20 ; int dp [ size ] ; int ans ; void insertVector ( int mask ) { for ( int i = 0 ; i < 20 ; i ++ ) { if ( ( mask & 1 << i ) == 0 ) continue ; if ( ! dp [ i ] ) { dp [ i ] = mask ; ++ ans ; return ; } mask ^= dp [ i ] ; } } void maxSizeSet ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { insertVector ( arr [ i ] ) ; } cout << ( 1 << ans ) << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxSizeSet ( arr , N ) ; return 0 ; }
Maximize sum possible from an array by jumps of length i + K * arr [ i ] from any ith index | C ++ program for the above approach ; Function to find the maximum sum possible by jumps of length i + K * arr [ i ] from any i - th index ; Initialize an array dp [ ] ; Stores the maximum sum ; Iterate over the range [ N - 1 , 0 ] ; If length of the jump exceeds N ; Set dp [ i ] as arr [ i ] ; Otherwise , update dp [ i ] as sum of dp [ i + K * arr [ i ] ] and arr [ i ] ; Update the overall maximum sum ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSum ( int arr [ ] , int N , int K ) { int dp [ N + 2 ] = { 0 } ; int maxval = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( ( i + K * arr [ i ] ) >= N ) { dp [ i ] = arr [ i ] ; } else { dp [ i ] = dp [ i + K * arr [ i ] ] + arr [ i ] ; } maxval = max ( maxval , dp [ i ] ) ; } cout << maxval ; } int main ( ) { int arr [ ] = { 2 , 1 , 3 , 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; maxSum ( arr , N , K ) ; return 0 ; }
Maximum sum submatrix | C ++ program for the above approach ; Function to find maximum sum submatrix ; Stores the number of rows and columns in the matrix ; Stores maximum submatrix sum ; Take each row as starting row ; Take each column as the starting column ; Take each row as the ending row ; Take each column as the ending column ; Stores the sum of submatrix having topleft index ( i , j ) and bottom right index ( k , l ) ; Iterate the submatrix row - wise and calculate its sum ; Update the maximum sum ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSubmatrixSum ( vector < vector < int > > matrix ) { int r = matrix . size ( ) ; int c = matrix [ 0 ] . size ( ) ; int maxSubmatrix = 0 ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { for ( int k = i ; k < r ; k ++ ) { for ( int l = j ; l < c ; l ++ ) { int sumSubmatrix = 0 ; for ( int m = i ; m <= k ; m ++ ) { for ( int n = j ; n <= l ; n ++ ) { sumSubmatrix += matrix [ m ] [ n ] ; } } maxSubmatrix = max ( maxSubmatrix , sumSubmatrix ) ; } } } } cout << maxSubmatrix ; } 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 ; }
Maximize sum of subsets from two arrays having no consecutive values | C ++ program for the above approach ; Function to calculate maximum subset sum ; Initialize array to store dp states ; Base Cases ; Pre initializing for dp [ 0 ] & dp [ 1 ] ; Calculating dp [ index ] based on above formula ; Print maximum subset sum ; Driver Code ; Given arrays ; Length of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSubsetSum ( int arr1 [ ] , int arr2 [ ] , int length ) { int dp [ length + 1 ] ; if ( length == 1 ) { cout << ( max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; return ; } if ( length == 2 ) { cout << ( max ( max ( arr1 [ 1 ] , arr2 [ 1 ] ) , max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ) ; return ; } else { dp [ 0 ] = max ( arr1 [ 0 ] , arr2 [ 0 ] ) ; dp [ 1 ] = max ( max ( arr1 [ 1 ] , arr2 [ 1 ] ) , max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; int index = 2 ; while ( index < length ) { dp [ index ] = max ( max ( arr1 [ index ] , arr2 [ index ] ) , max ( max ( arr1 [ index ] + dp [ index - 2 ] , arr2 [ index ] + dp [ index - 2 ] ) , dp [ index - 1 ] ) ) ; ++ index ; } cout << ( dp [ length - 1 ] ) ; } } int main ( ) { int arr1 [ ] = { -1 , -2 , 4 , -4 , 5 } ; int arr2 [ ] = { -1 , -2 , -3 , 4 , 10 } ; int length = 5 ; maximumSubsetSum ( arr1 , arr2 , length ) ; return 0 ; }
Modify array by replacing every array element with minimum possible value of arr [ j ] + | j | C ++ program for the above approach ; Function to find minimum value of arr [ j ] + | j - i | for every array index ; Stores minimum of a [ j ] + | i - j | upto position i ; Stores minimum of a [ j ] + | i - j | upto position i from the end ; Traversing and storing minimum of a [ j ] + | i - j | upto i ; Traversing and storing minimum of a [ j ] + | i - j | upto i from the end ; Traversing from [ 0 , N ] and storing minimum of a [ j ] + | i - j | from starting and end ; Print the required array ; Driver code ; Given array arr [ ] ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minAtEachIndex ( int n , int arr [ ] ) { int dp1 [ n ] ; int dp2 [ n ] ; int i ; dp1 [ 0 ] = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) dp1 [ i ] = min ( arr [ i ] , dp1 [ i - 1 ] + 1 ) ; dp2 [ n - 1 ] = arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) dp2 [ i ] = min ( arr [ i ] , dp2 [ i + 1 ] + 1 ) ; vector < int > v ; for ( i = 0 ; i < n ; i ++ ) v . push_back ( min ( dp1 [ i ] , dp2 [ i ] ) ) ; for ( auto x : v ) cout << x << " ▁ " ; } int main ( ) { int arr [ ] = { 1 , 4 , 2 , 5 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minAtEachIndex ( N , arr ) ; return 0 ; }
Count all N | C ++ program for the above approach ; Function to print the count of arrays satisfying given condition ; First element of array is set as 1 ; Since the first element of arr [ ] is 1 , the second element can 't be 1 ; Traverse the remaining indices ; If arr [ i ] = 1 ; If arr [ i ] a 1 ; Since last element needs to be 1 ; Driver Code ; Stores the count of arrays where arr [ 0 ] = arr [ N - 1 ] = 1 ; Since arr [ 0 ] and arr [ N - 1 ] can be any number from 1 to M ; Print answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalArrays ( int N , int M ) { int end_with_one [ N + 1 ] ; int end_not_with_one [ N + 1 ] ; end_with_one [ 0 ] = 1 ; end_not_with_one [ 0 ] = 0 ; end_with_one [ 1 ] = 0 ; end_not_with_one [ 1 ] = M - 1 ; for ( int i = 2 ; i < N ; i ++ ) { end_with_one [ i ] = end_not_with_one [ i - 1 ] ; end_not_with_one [ i ] = end_with_one [ i - 1 ] * ( M - 1 ) + end_not_with_one [ i - 1 ] * ( M - 2 ) ; } return end_with_one [ N - 1 ] ; } int main ( ) { int N = 3 , M = 3 ; int temp = totalArrays ( N , M ) ; int ans = M * temp ; cout << ans << " STRNEWLINE " ; return 0 ; }
Count numbers from a given range whose product of digits is K | C ++ program to implement the above approach ; Function to find the product of digits of a number ; Stores product of digits of N ; Update res ; Update N ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; Stores count of numbers in the range [ L , R ] whose product of digit is K ; Iterate over the range [ L , R ] ; If product of digits of i equal to K ; Update cnt ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prodOfDigit ( int N ) { int res = 1 ; while ( N ) { res = res * ( N % 10 ) ; N /= 10 ; } return res ; } int cntNumRange ( int L , int R , int K ) { int cnt = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( prodOfDigit ( i ) == K ) { cnt ++ ; } } return cnt ; } int main ( ) { int L = 20 , R = 10000 , K = 14 ; cout << cntNumRange ( L , R , K ) ; }
Median of Bitwise XOR of all submatrices starting from the top left corner | C ++ program to implement the above approach ; Function to find the median of bitwise XOR of all the submatrix whose topmost leftmost corner is ( 0 , 0 ) ; dp [ i ] [ j ] : Stores the bitwise XOR of submatrix having top left corner at ( 0 , 0 ) and bottom right corner at ( i , j ) ; Stores count of submatrix ; Base Case ; Base Case ; Fill dp [ ] [ ] using tabulation ; Fill dp [ i ] [ j ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; double findMedXOR ( int mat [ ] [ 2 ] , int N , int M ) { int dp [ N ] [ M ] ; int med [ N * M ] ; dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; med [ 0 ] = dp [ 0 ] [ 0 ] ; int len = 1 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ^ mat [ i ] [ 0 ] ; med [ len ++ ] = dp [ i ] [ 0 ] ; } for ( int i = 1 ; i < M ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] ^ mat [ 0 ] [ i ] ; med [ len ++ ] = dp [ 0 ] [ i ] ; } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ^ dp [ i ] [ j - 1 ] ^ dp [ i - 1 ] [ j - 1 ] ^ mat [ i ] [ j ] ; med [ len ++ ] = dp [ i ] [ j ] ; } } sort ( med , med + len ) ; if ( len % 2 == 0 ) { return ( med [ ( len / 2 ) ] + med [ ( len / 2 ) - 1 ] ) / 2.0 ; } return med [ len / 2 ] ; } int main ( ) { int mat [ ] [ 2 ] = { { 1 , 2 } , { 2 , 3 } } ; int N = sizeof ( mat ) / sizeof ( mat [ 0 ] ) ; int M = 2 ; cout << findMedXOR ( mat , N , M ) ; return 0 ; }
Count ways to tile an N | C ++ Program to implement the above approach ; Function to count the ways to tile N * 1 board using 1 * 1 and 2 * 1 tiles ; dp [ i ] : Stores count of ways to tile i * 1 board using given tiles ; Base Case ; Iterate over the range [ 2 , N ] ; Fill dp [ i ] using the recurrence relation ; Driver Code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long countWaysToTileBoard ( long N ) { long dp [ N + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 2 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = ( 2 * dp [ i - 1 ] + dp [ i - 2 ] ) ; } cout << dp [ N ] ; } int main ( ) { long N = 2 ; countWaysToTileBoard ( N ) ; return 0 ; }
Subsequences generated by including characters or ASCII value of characters of given string | C ++ program to implement the above approach ; Function to print subsequences containing ASCII value of the characters or the the characters of the given string ; Base Case ; If length of the subsequence exceeds 0 ; Print the subsequence ; Stores character present at i - th index of str ; If the i - th character is not included in the subsequence ; Including the i - th character in the subsequence ; Include the ASCII value of the ith character in the subsequence ; Driver Code ; Stores length of str
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FindSub ( string str , string res , int i ) { if ( i == str . length ( ) ) { if ( res . length ( ) > 0 ) { cout << res << " ▁ " ; } return ; } char ch = str [ i ] ; FindSub ( str , res , i + 1 ) ; FindSub ( str , res + ch , i + 1 ) ; FindSub ( str , res + to_string ( int ( ch ) ) , i + 1 ) ; } int main ( ) { string str = " ab " ; string res = " " ; int N = str . length ( ) ; FindSub ( str , res , 0 ) ; }
Minimize given flips required to reduce N to 0 | C ++ program to implement the above approach ; Function to find the minimum count of operations required to Reduce N to 0 ; Stores count of bits in N ; Recurrence relation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinOp ( int N ) { if ( N <= 1 ) return N ; int bit = log2 ( N ) + 1 ; return ( ( 1 << bit ) - 1 ) - MinOp ( N - ( 1 << ( bit - 1 ) ) ) ; } int main ( ) { int N = 4 ; cout << MinOp ( N ) ; return 0 ; }
Maximum non | C ++ program for the above approach ; Function to find the maximum product from the top left and bottom right cell of the given matrix grid [ ] [ ] ; Store dimension of grid ; Stores maximum product path ; Stores minimum product path ; Traverse the grid and update maxPath and minPath array ; Initialize to inf and - inf ; Base Case ; Calculate for row : ; Update the maximum ; Update the minimum ; Calculate for column : ; Update the maximum ; Update the minimum ; Update maxPath and minPath ; If negative product ; Otherwise ; Driver Code ; Given matrix mat [ ] [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProductPath ( vector < vector < int > > grid ) { int n = grid . size ( ) ; int m = grid [ 0 ] . size ( ) ; vector < vector < int > > maxPath ( n , vector < int > ( m , 0 ) ) ; vector < vector < int > > minPath ( n , vector < int > ( m , 0 ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int mn = INT_MAX ; int mx = INT_MIN ; if ( i == 0 and j == 0 ) { mx = grid [ i ] [ j ] ; mn = grid [ i ] [ j ] ; } if ( i > 0 ) { int tempmx = max ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) ) ; mx = max ( mx , tempmx ) ; int tempmn = min ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) ) ; mn = min ( mn , tempmn ) ; } if ( j > 0 ) { int tempmx = max ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) ) ; mx = max ( mx , tempmx ) ; int tempmn = min ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) ) ; mn = min ( mn , tempmn ) ; } maxPath [ i ] [ j ] = mx ; minPath [ i ] [ j ] = mn ; } } if ( maxPath [ n - 1 ] [ m - 1 ] < 0 ) return -1 ; else return ( maxPath [ n - 1 ] [ m - 1 ] ) ; } int main ( ) { vector < vector < int > > mat = { { 1 , -2 , 1 } , { 1 , -2 , 1 } , { 3 , -4 , 1 } } ; cout << ( maxProductPath ( mat ) ) ; }
Smallest submatrix required to be removed such that sum of the remaining matrix is divisible by K | C ++ program to implement the above approach ; Function to find the length of the smallest subarray to be removed such that sum of elements is equal to S % K ; Remainder when total_sum is divided by K ; Stores curr_remainder and the most recent index at which curr_remainder has occured ; Stores required answer ; Add current element to curr_sum and take mod ; Update current remainder index ; If mod already exists in map the subarray exists ; Update res ; If not possible ; Return the result ; Function to find the smallest submatrix rqured to be deleted to make the sum of the matrix divisible by K ; Stores the sum of element of the matrix ; Traverse the matrix mat [ ] [ ] ; Update S ; Stores smallest area need to be deleted to get sum divisible by K ; Stores leftmost column of each matrix ; Stores rightmost column of each matrix ; Stores number of coulmns deleted of a matrix ; Store area of the deleted matrix ; prefixRowSum [ i ] : Store sum of sub matrix whose topmost left and bottommost right position is ( 0 , left ) ( i , right ) ; Iterate over all possible values of ( left , right ) ; Initialize all possible values of prefixRowSum [ ] to 0 ; Traverse each row from left to right column ; Update row_sum [ i ] ; ; Update width ; If no submatrix of the length ( right - left + 1 ) found to get the required output ; Update area ; If area is less than min_area ; Update min_area ; Driver Code ; Stores number of rows in the matrix ; Stores number of column in the matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; int removeSmallestSubarray ( int arr [ ] , int S , int n , int k ) { int target_remainder = S % k ; unordered_map < int , int > map1 ; map1 [ 0 ] = -1 ; int curr_remainder = 0 ; int res = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { curr_remainder = ( curr_remainder + arr [ i ] + k ) % k ; map1 [ curr_remainder ] = i ; int mod = ( curr_remainder - target_remainder + k ) % k ; if ( map1 . find ( mod ) != map1 . end ( ) ) { res = min ( res , i - map1 [ mod ] ) ; } } if ( res == INT_MAX res == n ) { res = -1 ; } return res ; } int smstSubmatDeleted ( vector < vector < int > > & mat , int N , int M , int K ) { int S = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) S += mat [ i ] [ j ] ; } int min_area = N * M ; int left = 0 ; int right = 0 ; int width ; int area ; int prefixRowSum [ N ] ; for ( left = 0 ; left < M ; left ++ ) { memset ( prefixRowSum , 0 , sizeof ( prefixRowSum ) ) ; for ( right = left ; right < M ; right ++ ) { for ( int i = 0 ; i < N ; i ++ ) { prefixRowSum [ i ] += mat [ i ] [ right ] ; } width = removeSmallestSubarray ( prefixRowSum , S , N , K ) ; if ( width != -1 ) { area = ( right - left + 1 ) * ( width ) ; if ( area < min_area ) { min_area = area ; } } } } return min_area ; } int main ( ) { vector < vector < int > > mat = { { 6 , 2 , 6 } , { 3 , 2 , 8 } , { 2 , 5 , 3 } } ; int K = 3 ; int N = mat . size ( ) ; int M = mat [ 0 ] . size ( ) ; cout << smstSubmatDeleted ( mat , N , M , K ) ; return 0 ; }
Count N | C ++ program to illustrate Count of N - length strings consisting only of vowels sorted lexicographically ; to keep the string in lexicographically sorted order use start index to add the vowels starting the from that index ; base case : if string length is 0 add to the count ; if last character in string is ' e ' add vowels starting from ' e ' i . e ' e ' , ' i ' , ' o ' , ' u ' ; decrease the length of string ; char arr [ 5 ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' } ; starting from index 0 add the vowels to strings
#include <iostream> NEW_LINE using namespace std ; int countstrings ( int n , int start ) { if ( n == 0 ) { return 1 ; } int cnt = 0 ; for ( int i = start ; i < 5 ; i ++ ) { cnt += countstrings ( n - 1 , i ) ; } return cnt ; } int countVowelStrings ( int n ) { return countstrings ( n , 0 ) ; } int main ( ) { int n = 2 ; cout << countVowelStrings ( n ) ; return 0 ; }
Count N | C ++ program for the above approach ; Function to count N - length strings consisting of vowels only sorted lexicographically ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumberOfStrings ( int n ) { return ( n + 1 ) * ( n + 2 ) * ( n + 3 ) * ( n + 4 ) / 24 ; } int main ( ) { int N = 2 ; cout << findNumberOfStrings ( N ) ; return 0 ; }
Count unique paths with given sum in an N | C ++ program for the above approach ; Function for counting total no of paths possible with the sum is equal to X ; If the path of the sum from the root to current node is stored in sum ; If already computed ; Count different no of paths using all possible ways ; Return total no of paths ; Driver Code ; Stores the number of ways to obtains sums 0 to X ; Function call
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; const int mod = ( int ) 1e9 + 7 ; ll findTotalPath ( int X , int n , vector < int > & dp ) { if ( X == 0 ) { return 1 ; } ll ans = 0 ; if ( dp [ X ] != -1 ) { return dp [ X ] ; } for ( int i = 1 ; i <= min ( X , n ) ; ++ i ) { ans += findTotalPath ( X - i , n , dp ) % mod ; ans %= mod ; } return dp [ X ] = ans ; } int main ( ) { int n = 3 , X = 2 ; vector < int > dp ( X + 1 , -1 ) ; cout << findTotalPath ( X , n , dp ) ; }
Count sequences of positive integers having product X | C ++ program for the above approach ; Function to print the total number of possible sequences with product X ; Precomputation of binomial coefficients ; Max length of a subsequence ; Ways dp array ; Fill i slots using all the primes ; Subtract ways for all slots that exactly fill less than i slots ; Total possible sequences ; Print the resultant count ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bin [ 3000 ] [ 3000 ] ; void countWays ( const vector < int > & arr ) { int mod = 1e9 + 7 ; bin [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i < 3000 ; i ++ ) { bin [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { bin [ i ] [ j ] = ( bin [ i - 1 ] [ j ] + bin [ i - 1 ] [ j - 1 ] ) % mod ; } } int n = 0 ; for ( auto x : arr ) n += x ; vector < int > ways ( n + 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { ways [ i ] = 1 ; for ( int j = 0 ; j < ( int ) arr . size ( ) ; j ++ ) { ways [ i ] = ( ways [ i ] * bin [ arr [ j ] + i - 1 ] [ i - 1 ] ) % mod ; } for ( int j = 1 ; j < i ; j ++ ) { ways [ i ] = ( ( ways [ i ] - bin [ i ] [ j ] * ways [ j ] ) % mod + mod ) % mod ; } } int ans = 0 ; for ( auto x : ways ) ans = ( ans + x ) % mod ; cout << ans << endl ; } int main ( ) { vector < int > arr = { 1 , 1 } ; countWays ( arr ) ; return 0 ; }