codeparrot/xlcost-text-to-code huggingface dataset:
Generate all unique partitions of an integer | Set 2 | C # implementation of the approach ; Array to store the numbers used to form the required sum ; Function to print the array which contains the unique partitions which are used to form the required sum ; Function to find all the unique partitions remSum = remaining sum to form maxVal is the maximum number that can be used to make the partition ; If remSum == 0 that means the sum is achieved so print the array ; i will begin from maxVal which is the maximum value which can be used to form the sum ; Store the number used in forming sum gradually in the array ; Since i used the rest of partition cant have any number greater than i hence second parameter is i ; Driver code
using System ; class GFG { static int [ ] dp = new int [ 200 ] ; static void print ( int idx ) { for ( int i = 1 ; i < idx ; i ++ ) { Console . Write ( dp [ i ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } static void solve ( int remSum , int maxVal , int idx , int count ) { if ( remSum == 0 ) { print ( idx ) ; count ++ ; return ; } for ( int i = maxVal ; i >= 1 ; i -- ) { if ( i > remSum ) { continue ; } else if ( i <= remSum ) { dp [ idx ] = i ; solve ( remSum - i , i , idx + 1 , count ) ; } } } public static void Main ( ) { int n = 4 , count = 0 ; solve ( n , n , 1 , count ) ; } }
Queries for bitwise OR in the given matrix | C # implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix - count for each row ; Finding column - wise prefix count ; Function to return the result for a query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; If count of variables with ith bit set is greater than 0 ; Driver code
using System ; class GFG { static int bitscount = 32 ; static int n = 3 ; static int [ , , ] prefix_count = new int [ bitscount , n , n ] ; static void findPrefixCount ( int [ , ] arr ) { for ( int i = 0 ; i < bitscount ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { prefix_count [ i , j , 0 ] = ( ( arr [ j , 0 ] >> i ) & 1 ) ; for ( int k = 1 ; k < n ; k ++ ) { prefix_count [ i , j , k ] = ( ( arr [ j , k ] >> i ) & 1 ) ; prefix_count [ i , j , k ] += prefix_count [ i , j , k - 1 ] ; } } } for ( int i = 0 ; i < bitscount ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) prefix_count [ i , j , k ] += prefix_count [ i , j - 1 , k ] ; } static int rangeOr ( int x1 , int y1 , int x2 , int y2 ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int p ; if ( x1 == 0 && y1 == 0 ) p = prefix_count [ i , x2 , y2 ] ; else if ( x1 == 0 ) p = prefix_count [ i , x2 , y2 ] - prefix_count [ i , x2 , y1 - 1 ] ; else if ( y1 == 0 ) p = prefix_count [ i , x2 , y2 ] - prefix_count [ i , x1 - 1 , y2 ] ; else p = prefix_count [ i , x2 , y2 ] - prefix_count [ i , x1 - 1 , y2 ] - prefix_count [ i , x2 , y1 - 1 ] + prefix_count [ i , x1 - 1 , y1 - 1 ] ; if ( p != 0 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; findPrefixCount ( arr ) ; int [ , ] queries = { { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 2 } } ; int q = queries . GetLength ( 0 ) ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( rangeOr ( queries [ i , 0 ] , queries [ i , 1 ] , queries [ i , 2 ] , queries [ i , 3 ] ) ) ; } }
Queries for bitwise AND in the given matrix | C # implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix - count for each row ; Finding column - wise prefix count ; Function to return the result for a query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; If count of variables whose ith bit is set equals to the total elements in the sub - matrix ; Driver code
using System ; class GFG { static int bitscount = 32 ; static int n = 3 ; static int [ , , ] prefix_count = new int [ bitscount , n , n ] ; static void findPrefixCount ( int [ , ] arr ) { for ( int i = 0 ; i < bitscount ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { prefix_count [ i , j , 0 ] = ( ( arr [ j , 0 ] >> i ) & 1 ) ; for ( int k = 1 ; k < n ; k ++ ) { prefix_count [ i , j , k ] = ( ( arr [ j , k ] >> i ) & 1 ) ; prefix_count [ i , j , k ] += prefix_count [ i , j , k - 1 ] ; } } } for ( int i = 0 ; i < bitscount ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) prefix_count [ i , j , k ] += prefix_count [ i , j - 1 , k ] ; } static int rangeAnd ( int x1 , int y1 , int x2 , int y2 ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int p ; if ( x1 == 0 && y1 == 0 ) p = prefix_count [ i , x2 , y2 ] ; else if ( x1 == 0 ) p = prefix_count [ i , x2 , y2 ] - prefix_count [ i , x2 , y1 - 1 ] ; else if ( y1 == 0 ) p = prefix_count [ i , x2 , y2 ] - prefix_count [ i , x1 - 1 , y2 ] ; else p = prefix_count [ i , x2 , y2 ] - prefix_count [ i , x1 - 1 , y2 ] - prefix_count [ i , x2 , y1 - 1 ] + prefix_count [ i , x1 - 1 , y1 - 1 ] ; if ( p == ( x2 - x1 + 1 ) * ( y2 - y1 + 1 ) ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; findPrefixCount ( arr ) ; int [ , ] queries = { { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 2 } } ; int q = queries . GetLength ( 0 ) ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( rangeAnd ( queries [ i , 0 ] , queries [ i , 1 ] , queries [ i , 2 ] , queries [ i , 3 ] ) ) ; } }
Maximum sum such that no two elements are adjacent | Set 2 | C # program to implement above approach ; variable to store states of dp ; variable to check if a given state has been solved ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code
using System ; class GFG { static int maxLen = 10 ; static int [ ] dp = new int [ maxLen ] ; static bool [ ] v = new bool [ maxLen ] ; static int maxSum ( int [ ] arr , int i , int n ) { if ( i >= n ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = Math . Max ( maxSum ( arr , i + 1 , n ) , arr [ i ] + maxSum ( arr , i + 2 , n ) ) ; return dp [ i ] ; } public static void Main ( ) { int [ ] arr = { 12 , 9 , 7 , 33 } ; int n = arr . Length ; Console . Write ( maxSum ( arr , 0 , n ) ) ; } }
Optimal Strategy for a Game | Set 2 | C # program to find out maximum value from a given sequence of coins ; For both of your choices , the opponent gives you total sum minus maximum of his value ; Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Compute sum of elements ; Initialize memoization table ; Driver Code
using System ; class GFG { static int MAX = 100 ; static int [ , ] memo = new int [ MAX , MAX ] ; static int oSRec ( int [ ] arr , int i , int j , int sum ) { if ( j == i + 1 ) return Math . Max ( arr [ i ] , arr [ j ] ) ; if ( memo [ i , j ] != - 1 ) return memo [ i , j ] ; memo [ i , j ] = Math . Max ( ( sum - oSRec ( arr , i + 1 , j , sum - arr [ i ] ) ) , ( sum - oSRec ( arr , i , j - 1 , sum - arr [ j ] ) ) ) ; return memo [ i , j ] ; } static int accumulate ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) sum += arr [ i ] ; return sum ; } static int optimalStrategyOfGame ( int [ ] arr , int n ) { int sum = 0 ; sum = accumulate ( arr , 0 , n ) ; for ( int j = 0 ; j < MAX ; j ++ ) { for ( int k = 0 ; k < MAX ; k ++ ) memo [ j , k ] = - 1 ; } return oSRec ( arr , 0 , n - 1 , sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 8 , 15 , 3 , 7 } ; int n = arr1 . Length ; Console . Write ( " { 0 } STRNEWLINE " , optimalStrategyOfGame ( arr1 , n ) ) ; int [ ] arr2 = { 2 , 2 , 2 , 2 } ; n = arr2 . Length ; Console . Write ( " { 0 } STRNEWLINE " , optimalStrategyOfGame ( arr2 , n ) ) ; int [ ] arr3 = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . Length ; Console . Write ( " { 0 } STRNEWLINE " , optimalStrategyOfGame ( arr3 , n ) ) ; } }
Maximize the subarray sum after multiplying all elements of any subarray with X | C # implementation of the approach ; Function to return the maximum sum ; Base case ; If already calculated ; If no elements have been chosen ; Do not choose any element and use Kadane 's algorithm by taking max ; Choose the sub - array and multiply x ; Choose the sub - array and multiply x ; End the sub - array multiplication ; else No more multiplication ; Memoize and return the answer ; Function to get the maximum sum ; Initialize dp with - 1 ; Iterate from every position and find the maximum sum which is possible ; Driver code
using System ; class GFG { static int N = 5 ; static int func ( int idx , int cur , int [ ] a , int [ , ] dp , int n , int x ) { if ( idx == n ) { return 0 ; } if ( dp [ idx , cur ] != - 1 ) { return dp [ idx , cur ] ; } int ans = 0 ; if ( cur == 0 ) { ans = Math . Max ( ans , a [ idx ] + func ( idx + 1 , 0 , a , dp , n , x ) ) ; ans = Math . Max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; } else if ( cur == 1 ) { ans = Math . Max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; ans = Math . Max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; } { ans = Math . Max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; } return dp [ idx , cur ] = ans ; } static int getMaximumSum ( int [ ] a , int n , int x ) { int [ , ] dp = new int [ n , 3 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { dp [ i , j ] = - 1 ; } } int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxi = Math . Max ( maxi , func ( i , 0 , a , dp , n , x ) ) ; } return maxi ; } public static void Main ( String [ ] args ) { int [ ] a = { - 3 , 8 , - 2 , 1 , - 6 } ; int n = a . Length ; int x = - 1 ; Console . WriteLine ( getMaximumSum ( a , n , x ) ) ; } }
Count pairs of non | C # implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to return the number of pairs ; Create the dp table initially ; Declare the left array ; Declare the right array ; Initially left [ 0 ] is 1 ; Count the number of palindrome pairs to the left ; Initially right most as 1 ; Count the number of palindrome pairs to the right ; Count the number of pairs ; Driver code
using System ; class GFG { static int N = 100 ; static void pre_process ( bool [ , ] dp , char [ ] s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i , j ] = false ; } } for ( int j = 1 ; j <= n ; j ++ ) { for ( int i = 0 ; i <= n - j ; i ++ ) { if ( j <= 2 ) { if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i , i + j - 1 ] = true ; } } else if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i , i + j - 1 ] = dp [ i + 1 , i + j - 2 ] ; } } } } static int countPairs ( String s ) { bool [ , ] dp = new bool [ N , N ] ; pre_process ( dp , s . ToCharArray ( ) ) ; int n = s . Length ; int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; left [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( dp [ j , i ] == true ) { left [ i ] ++ ; } } } right [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { right [ i ] = right [ i + 1 ] ; for ( int j = n - 1 ; j >= i ; j -- ) { if ( dp [ i , j ] == true ) { right [ i ] ++ ; } } } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += left [ i ] * right [ i + 1 ] ; } return ans ; } public static void Main ( String [ ] args ) { String s = " abacaba " ; Console . Write ( countPairs ( s ) ) ; } }
Queries to check if substring [ L ... R ] is palindrome or not | C # implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to answer every query in O ( 1 ) ; Driver code
using System ; class GFG { static int N = 100 ; static void pre_process ( bool [ , ] dp , char [ ] s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i , j ] = false ; } } for ( int j = 1 ; j <= n ; j ++ ) { for ( int i = 0 ; i <= n - j ; i ++ ) { if ( j <= 2 ) { if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i , i + j - 1 ] = true ; } } else if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i , i + j - 1 ] = dp [ i + 1 , i + j - 2 ] ; } } } } static void answerQuery ( int l , int r , bool [ , ] dp ) { if ( dp [ l , r ] ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( ) { string s = " abaaab " ; bool [ , ] dp = new bool [ N , N ] ; pre_process ( dp , s . ToCharArray ( ) ) ; int [ , ] queries = { { 0 , 1 } , { 1 , 5 } } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) { answerQuery ( queries [ i , 0 ] , queries [ i , 1 ] , dp ) ; } } }
Length of the longest increasing subsequence such that no two adjacent elements are coprime | C # program to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; Function to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; To store dp and d value ; To store required answer ; For all elements in the array ; Initially answer is one ; For all it 's divisors ; Update the dp value ; Update the divisor value ; Check for required answer ; Update divisor of a [ i ] ; Return required answer ; Driver code
using System ; class GFG { static int N = 100005 ; static int LIS ( int [ ] a , int n ) { int [ ] dp = new int [ N ] ; int [ ] d = new int [ N ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { dp [ a [ i ] ] = 1 ; for ( int j = 2 ; j * j <= a [ i ] ; j ++ ) { if ( a [ i ] % j == 0 ) { dp [ a [ i ] ] = Math . Max ( dp [ a [ i ] ] , dp [ d [ j ] ] + 1 ) ; dp [ a [ i ] ] = Math . Max ( dp [ a [ i ] ] , dp [ d [ a [ i ] / j ] ] + 1 ) ; d [ j ] = a [ i ] ; d [ a [ i ] / j ] = a [ i ] ; } } ans = Math . Max ( ans , dp [ a [ i ] ] ) ; d [ a [ i ] ] = a [ i ] ; } return ans ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = a . Length ; Console . WriteLine ( LIS ( a , n ) ) ; } }
Find the number of binary strings of length N with at least 3 consecutive 1 s | C # implementation of the approach value x ; Function that returns true if s contains three consecutive 1 's ; Function to return the count of required strings ; Driver code
using System ; class GFG { static bool check ( String s ) { int n = s . Length ; for ( int i = 2 ; i < n ; i ++ ) { if ( s [ i ] == '1' && s [ i - 1 ] == '1' && s [ i - 2 ] == '1' ) return true ; } return false ; } static int countStr ( int i , String s ) { if ( i < 0 ) { if ( check ( s ) ) return 1 ; return 0 ; } char [ ] myNameChars = s . ToCharArray ( ) ; myNameChars [ i ] = '0' ; s = String . Join ( " " , myNameChars ) ; int ans = countStr ( i - 1 , s ) ; char [ ] myChar = s . ToCharArray ( ) ; myChar [ i ] = '1' ; s = String . Join ( " " , myChar ) ; ans += countStr ( i - 1 , s ) ; char [ ] myChar1 = s . ToCharArray ( ) ; myChar1 [ i ] = '0' ; s = String . Join ( " " , myChar1 ) ; return ans ; } public static void Main ( String [ ] args ) { int N = 4 ; String s = "0000" ; Console . WriteLine ( countStr ( N - 1 , s ) ) ; } }
Find the number of binary strings of length N with at least 3 consecutive 1 s | C # implementation of the above approach ; Function to return the count of required strings ; '0' at ith position ; '1' at ith position ; Driver code ; Base condition : 2 ^ ( i + 1 ) because of 0 indexing
using System ; class GFG { static int n ; static int solve ( int i , int x , int [ , ] dp ) { if ( i < 0 ) { return x == 3 ? 1 : 0 ; } if ( dp [ i , x ] != - 1 ) { return dp [ i , x ] ; } dp [ i , x ] = solve ( i - 1 , 0 , dp ) ; dp [ i , x ] += solve ( i - 1 , x + 1 , dp ) ; return dp [ i , x ] ; } public static void Main ( String [ ] args ) { n = 4 ; int [ , ] dp = new int [ n , 4 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 4 ; j ++ ) { dp [ i , j ] = - 1 ; } } for ( int i = 0 ; i < n ; i ++ ) { dp [ i , 3 ] = ( 1 << ( i + 1 ) ) ; } Console . Write ( solve ( n - 1 , 0 , dp ) ) ; } }
Find the sum of the diagonal elements of the given N X N spiral matrix | C # implementation of the approach ; Function to return the sum of both the diagonal elements of the required matrix ; Array to store sum of diagonal elements ; Base cases ; Computing the value of dp ; Driver code
class GFG { static int findSum ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 1 ] = 1 ; dp [ 0 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( 4 * ( i * i ) ) - 6 * ( i - 1 ) + dp [ i - 2 ] ; } return dp [ n ] ; } static void Main ( ) { int n = 4 ; System . Console . WriteLine ( findSum ( n ) ) ; } }
Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming | C # program to find maximum sum of a subset of nodes that are not adjacent ; Function to find the diameter of the tree using Dynamic Programming ; Traverse for all children of node ; Call DFS function again ; Include the current node then donot include the children ; Donot include current node , then include children or not include them ; Recurrence value ; Driver program to test above functions ; Constructed tree is 1 / \ 2 3 / \ 4 5 ; create undirected edges ; Numbers to node ; Find maximum sum by calling function
using System ; using System . Collections ; class GFG { public static void dfs ( int node , int parent , int [ ] dp1 , int [ ] dp2 , ArrayList [ ] adj , int [ ] tree ) { int sum1 = 0 , sum2 = 0 ; foreach ( int i in adj [ node ] ) { if ( i == parent ) continue ; dfs ( i , node , dp1 , dp2 , adj , tree ) ; sum1 += dp2 [ i ] ; sum2 += Math . Max ( dp1 [ i ] , dp2 [ i ] ) ; } dp1 [ node ] = tree [ node ] + sum1 ; dp2 [ node ] = sum2 ; } public static void Main ( string [ ] arg ) { int n = 5 ; ArrayList [ ] adj = new ArrayList [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { adj [ i ] = new ArrayList ( ) ; } adj [ 1 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 1 ) ; adj [ 1 ] . Add ( 3 ) ; adj [ 3 ] . Add ( 1 ) ; adj [ 2 ] . Add ( 4 ) ; adj [ 4 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 5 ) ; adj [ 5 ] . Add ( 2 ) ; int [ ] tree = new int [ n + 1 ] ; tree [ 1 ] = 10 ; tree [ 2 ] = 5 ; tree [ 3 ] = 11 ; tree [ 4 ] = 6 ; tree [ 5 ] = 8 ; int [ ] dp1 = new int [ n + 1 ] ; int [ ] dp2 = new int [ n + 1 ] ; Array . Fill ( dp1 , 0 ) ; Array . Fill ( dp2 , 0 ) ; dfs ( 1 , 1 , dp1 , dp2 , adj , tree ) ; Console . Write ( " Maximum ▁ sum : ▁ " + Math . Max ( dp1 [ 1 ] , dp2 [ 1 ] ) ) ; } }
Count number of ways to reach a given score in a Matrix | C # implementation of the approach ; To store the states of dp ; To check whether a particular state of dp has been solved ; Function to find the ways using memoization ; Base cases ; If required score becomes negative ; If current state has been reached before ; Set current state to visited ; Driver code
using System ; class GFG { static int n = 3 ; static int MAX = 30 ; static int [ , , ] dp = new int [ n , n , MAX ] ; static bool [ , , ] v = new bool [ n , n , MAX ] ; static int findCount ( int [ , ] mat , int i , int j , int m ) { if ( i == 0 && j == 0 ) { if ( m == mat [ 0 , 0 ] ) return 1 ; else return 0 ; } if ( m < 0 ) return 0 ; if ( i < 0 j < 0 ) return 0 ; if ( v [ i , j , m ] ) return dp [ i , j , m ] ; v [ i , j , m ] = true ; dp [ i , j , m ] = findCount ( mat , i - 1 , j , m - mat [ i , j ] ) + findCount ( mat , i , j - 1 , m - mat [ i , j ] ) ; return dp [ i , j , m ] ; } public static void Main ( ) { int [ , ] mat = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int m = 5 ; Console . WriteLine ( findCount ( mat , n - 1 , n - 1 , m ) ) ; } }
Number of ways of scoring R runs in B balls with at most W wickets | C # implementation of the approach ; Function to return the number of ways to score R runs in B balls with at most W wickets ; If the wickets lost are more ; If runs scored are more ; If condition is met ; If no run got scored ; Already visited state ; If scored 0 run ; If scored 1 run ; If scored 2 runs ; If scored 3 runs ; If scored 4 runs ; If scored 6 runs ; If scored no run and lost a wicket ; Memoize and return ; Driver code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int mod = 1000000007 ; static int RUNMAX = 300 ; static int BALLMAX = 50 ; static int WICKETMAX = 10 ; static int CountWays ( int r , int b , int l , int R , int B , int W , int [ , , ] dp ) { if ( l > W ) return 0 ; if ( r > R ) return 0 ; if ( b == B && r == R ) return 1 ; if ( b == B ) return 0 ; if ( dp [ r , b , l ] != - 1 ) return dp [ r , b , l ] ; int ans = 0 ; ans += CountWays ( r , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 1 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 2 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 3 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 4 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 6 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r , b + 1 , l + 1 , R , B , W , dp ) ; ans = ans % mod ; return dp [ r , b , l ] = ans ; } static void Main ( ) { int R = 40 , B = 10 , W = 4 ; int [ , , ] dp = new int [ RUNMAX , BALLMAX , WICKETMAX ] ; for ( int i = 0 ; i < RUNMAX ; i ++ ) for ( int j = 0 ; j < BALLMAX ; j ++ ) for ( int k = 0 ; k < WICKETMAX ; k ++ ) dp [ i , j , k ] = - 1 ; Console . WriteLine ( CountWays ( 0 , 0 , 0 , R , B , W , dp ) ) ; } }
Minimum steps to delete a string by deleting substring comprising of same characters | C # implementation of the approach ; Function to return the minimum number of delete operations ; When a single character is deleted ; When a group of consecutive characters are deleted if any of them matches ; When both the characters are same then delete the letters in between them ; Memoize ; Driver code
using System ; class GFG { static int N = 10 ; static int findMinimumDeletion ( int l , int r , int [ , ] dp , String s ) { if ( l > r ) { return 0 ; } if ( l == r ) { return 1 ; } if ( dp [ l , r ] != - 1 ) { return dp [ l , r ] ; } int res = 1 + findMinimumDeletion ( l + 1 , r , dp , s ) ; for ( int i = l + 1 ; i <= r ; ++ i ) { if ( s [ l ] == s [ i ] ) { res = Math . Min ( res , findMinimumDeletion ( l + 1 , i - 1 , dp , s ) + findMinimumDeletion ( i , r , dp , s ) ) ; } } return dp [ l , r ] = res ; } public static void Main ( String [ ] args ) { String s = " abcddcba " ; int n = s . Length ; int [ , ] dp = new int [ N , N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( findMinimumDeletion ( 0 , n - 1 , dp , s ) ) ; } }