codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to make exactly C components in a 2 * N matrix | C # implementation to find the number of ways to make exactly C components in a 2 * N matrix ; row1 and row2 are one when both are same colored ; Function to find the number of ways to make exactly C components in a 2 * N matrix ; If No of components at any stage exceeds the given number then base case ; Condition to check if already visited ; If not visited previously ; At the first column ; color { white , white } or { black , black } ; Color { white , black } or { black , white } ; If previous both rows have same color ; Fill with { same , same } and { white , black } and { black , white } ; Fill with same without increase in component as it has been counted previously ; When previous rows had { white , black } ; When previous rows had { black , white } ; Memoization ; Driver Code ; Initially at first column with 0 components
using System ; class GFG { static int n , k ; static int [ , , , ] dp = new int [ 1024 , 2048 , 2 , 2 ] ; static int Ways ( int col , int comp , int row1 , int row2 ) { if ( comp > k ) return 0 ; if ( col > n ) { if ( comp == k ) return 1 ; else return 0 ; } if ( dp [ col , comp , row1 , row2 ] != - 1 ) return dp [ col , comp , row1 , row2 ] ; else { int ans = 0 ; if ( col == 1 ) { ans = ( ans + Ways ( col + 1 , comp + 1 , 0 , 0 ) + Ways ( col + 1 , comp + 1 , 1 , 1 ) ) ; ans = ( ans + Ways ( col + 1 , comp + 2 , 0 , 1 ) + Ways ( col + 1 , comp + 2 , 1 , 0 ) ) ; } else { if ( ( row1 > 0 && row2 > 0 ) || ( row1 == 0 && row2 == 0 ) ) { ans = ( ( ( ans + Ways ( col + 1 , comp + 1 , 0 , 0 ) ) + Ways ( col + 1 , comp + 1 , 1 , 0 ) ) + Ways ( col + 1 , comp + 1 , 0 , 1 ) ) ; ans = ( ans + Ways ( col + 1 , comp , 1 , 1 ) ) ; } if ( row1 > 0 && row2 == 0 ) { ans = ( ( ( ans + Ways ( col + 1 , comp , 0 , 0 ) ) + Ways ( col + 1 , comp , 1 , 1 ) ) + Ways ( col + 1 , comp , 1 , 0 ) ) ; ans = ( ans + Ways ( col + 1 , comp + 2 , 0 , 1 ) ) ; } if ( row1 == 0 && row2 > 0 ) { ans = ( ( ( ans + Ways ( col + 1 , comp , 0 , 0 ) ) + Ways ( col + 1 , comp , 1 , 1 ) ) + Ways ( col + 1 , comp , 0 , 1 ) ) ; ans = ( ans + Ways ( col + 1 , comp + 2 , 1 , 0 ) ) ; } } return dp [ col , comp , row1 , row2 ] = ans ; } } public static void Main ( String [ ] args ) { n = 2 ; k = 1 ; for ( int i = 0 ; i < 1024 ; i ++ ) for ( int j = 0 ; j < 2048 ; j ++ ) for ( int K = 0 ; K < 2 ; K ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i , j , K , l ] = - 1 ; Console . Write ( Ways ( 1 , 0 , 0 , 0 ) ) ; } }
Find all possible ways to Split the given string into Primes | C # program to Find all the ways to split the given string into Primes . ; Sieve of Eratosthenes ; Function Convert integer to binary string ; Function print all the all the ways to split the given string into Primes . ; To store all possible strings ; Exponetnital complexity n * ( 2 ^ ( n - 1 ) ) for bit ; Pruning step ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool [ ] primes = new bool [ 1000001 ] ; static int maxn = 1000000 ; static void sieve ( ) { for ( int i = 0 ; i < primes . Length ; i ++ ) { primes [ i ] = true ; } primes [ 0 ] = false ; primes [ 1 ] = false ; for ( int i = 2 ; i * i <= maxn ; i ++ ) { if ( primes [ i ] ) { for ( int j = i * i ; j <= maxn ; j += i ) primes [ j ] = false ; } } } static String toBinary ( int n ) { String r = " " ; while ( n != 0 ) { r = ( n % 2 == 0 ? "0" : "1" ) + r ; n /= 2 ; } return ( r == " " ) ? "0" : r ; } static void PrimeSplit ( String str ) { String temp ; List < String > ans = new List < String > ( ) ; int bt = 1 << ( str . Length - 1 ) ; int n = str . Length ; for ( int i = 0 ; i < bt ; i ++ ) { temp = toBinary ( i ) + "0" ; int j = 0 , x = n - temp . Length , y ; while ( j < x ) { temp = "0" + temp ; j ++ ; } j = 0 ; x = 0 ; y = - 1 ; String sp = " " , tp = " " ; bool flag = false ; while ( j < n ) { sp += str [ j ] ; if ( temp [ j ] == '1' ) { tp += sp + ' , ' ; y = Int32 . Parse ( sp ) ; if ( ! primes [ y ] ) { flag = true ; break ; } sp = " " ; } j ++ ; } tp += sp ; if ( sp != " " ) { y = Int32 . Parse ( sp ) ; if ( ! primes [ y ] ) flag = true ; } if ( ! flag ) ans . Add ( tp ) ; } if ( ans . Count == 0 ) { Console . WriteLine ( - 1 ) ; } foreach ( String i in ans ) { Console . WriteLine ( i ) ; } } public static void Main ( String [ ] args ) { String str = "11373" ; sieve ( ) ; PrimeSplit ( str ) ; } }
Count of subsequences of length 4 in form ( x , x , x + 1 , x + 1 ) | Set 2 | C # program for the above approach ; Function to count the numbers ; Array that stores the digits from left to right ; Array that stores the digits from right to left ; Initially both array store zero ; Fill the table for count1 array ; Update the count of current character ; Fill the table for count2 array ; Update the count of cuuent character ; Variable that stores the count of the numbers ; Traverse Input string and get the count of digits from count1 and count2 array such that difference b / w digit is 1 & store it int c1 & c2 . And store it in variable c1 and c2 ; Update the ans ; Return the readonly count ; Driver code ; Given String ; Function call
using System ; class GFG { static int countStableNum ( String str , int N ) { int [ , ] count1 = new int [ N , 10 ] ; int [ , ] count2 = new int [ N , 10 ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < 10 ; j ++ ) count1 [ i , j ] = count2 [ i , j ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i != 0 ) { for ( int j = 0 ; j < 10 ; j ++ ) { count1 [ i , j ] += count1 [ i - 1 , j ] ; } } count1 [ i , str [ i ] - '0' ] ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( i != N - 1 ) { for ( int j = 0 ; j < 10 ; j ++ ) { count2 [ i , j ] += count2 [ i + 1 , j ] ; } } count2 [ i , str [ i ] - '0' ] ++ ; } int ans = 0 ; for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( str [ i ] == '9' ) continue ; int c1 = count1 [ i - 1 , str [ i ] - '0' ] ; int c2 = count2 [ i + 1 , str [ i ] - '0' + 1 ] ; if ( c2 == 0 ) continue ; ans = ( ans + ( c1 * ( ( c2 * ( c2 - 1 ) / 2 ) ) ) ) ; } return ans ; } public static void Main ( String [ ] args ) { String str = "224353" ; int N = str . Length ; Console . WriteLine ( countStableNum ( str , N ) ) ; } }
Count of subsequences in an array with sum less than or equal to X | C # program to count number of subsequences in an array with sum less than or equal to X ; Utility function to return the count of subsequence in an array with sum less than or equal to X ; Base condition ; Return if the sub - problem is already calculated ; Check if the current element is less than or equal to sum ; Count subsequences excluding the current element ; Count subsequences including the current element ; Exclude current element ; Return the result ; Function to return the count of subsequence in an array with sum less than or equal to X ; Initialize a DP array ; Return the result ; Driver Code
using System ; class GFG { static int countSubsequenceUtil ( int ind , int sum , int [ ] A , int N , int [ , ] dp ) { if ( ind == N ) return 1 ; if ( dp [ ind , sum ] != - 1 ) return dp [ ind , sum ] ; if ( A [ ind ] <= sum ) { dp [ ind , sum ] = countSubsequenceUtil ( ind + 1 , sum , A , N , dp ) + countSubsequenceUtil ( ind + 1 , sum - A [ ind ] , A , N , dp ) ; } else { dp [ ind , sum ] = countSubsequenceUtil ( ind + 1 , sum , A , N , dp ) ; } return dp [ ind , sum ] ; } static int countSubsequence ( int [ ] A , int N , int X ) { int [ , ] dp = new int [ N , X + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < X + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } return countSubsequenceUtil ( 0 , X , A , N , dp ) - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 25 , 13 , 40 } ; int X = 50 ; int N = arr . Length ; Console . Write ( countSubsequence ( arr , N , X ) ) ; } }
Count possible decodings of a given Digit Sequence | Set 2 | C # implementation to count the possible decodings of the given digit sequence ; Function to count the number of ways to decode the given digit sequence ; Array to store the dp states ; Case of empty String ; Condition to check if the first character of String is 0 ; Base case for single length String ; Bottom - up dp for the String ; Previous character ; Current character ; Case to include the current digit as a single digit for decoding the String ; Case to include the current character as two - digit for decoding the String ; Condition to check if the current character is " * " ; Condition to check if the current character is less than or equal to 26 ; Condition to check if the previous digit is equal to " * " ; Driver Code ; Function Call
using System ; class GFG { static int waysToDecode2 ( char [ ] s ) { int n = s . Length ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 1 ; if ( s [ 0 ] == '0' ) return 0 ; dp [ 1 ] = ( ( s [ 0 ] == ' * ' ) ? 9 : 1 ) ; for ( int i = 2 ; i <= n ; i ++ ) { char first = s [ i - 2 ] ; char second = s [ i - 1 ] ; if ( second == ' * ' ) { dp [ i ] += 9 * dp [ i - 1 ] ; } else if ( second > '0' ) { dp [ i ] += dp [ i - 1 ] ; } if ( first == '1' first == '2' ) { if ( second == ' * ' ) { if ( first == '1' ) { dp [ i ] += 9 * dp [ i - 2 ] ; } else if ( first == '2' ) { dp [ i ] += 6 * dp [ i - 2 ] ; } } else if ( ( ( first - '0' ) * 10 + ( second - '0' ) ) <= 26 ) { dp [ i ] += dp [ i - 2 ] ; } } else if ( first == ' * ' ) { if ( second == ' * ' ) { dp [ i ] += 15 * dp [ i - 2 ] ; } else if ( second <= '6' ) { dp [ i ] += 2 * dp [ i - 2 ] ; } else { dp [ i ] += dp [ i - 2 ] ; } } } return dp [ n ] ; } public static void Main ( String [ ] args ) { String str = "12*3" ; Console . Write ( waysToDecode2 ( str . ToCharArray ( ) ) + " STRNEWLINE " ) ; } }
Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H | C # implementation of the above approach ; Function that prints the number of times X gets a value between L and R ; Base condition ; Condition if X can be made equal to j after i additions ; Compute value of X after adding arr [ i ] ; Compute value of X after adding arr [ i ] - 1 ; Update dp as the maximum value ; Compute maximum answer from all possible cases ; Printing maximum good occurrence of X ; Driver Code
using System ; class GFG { static void goodint ( int [ ] arr , int n , int h , int l , int r ) { int [ , ] dp = new int [ n + 1 , h ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < h ; j ++ ) dp [ i , j ] = - 1 ; dp [ 0 , 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < h ; j ++ ) { if ( dp [ i , j ] != - 1 ) { int h1 = ( j + arr [ i ] ) % h ; int h2 = ( j + arr [ i ] - 1 ) % h ; dp [ i + 1 , h1 ] = Math . Max ( dp [ i + 1 , h1 ] , dp [ i , j ] + ( ( h1 >= l && h1 <= r ) ? 1 : 0 ) ) ; dp [ i + 1 , h2 ] = Math . Max ( dp [ i + 1 , h2 ] , dp [ i , j ] + ( ( h2 >= l && h2 <= r ) ? 1 : 0 ) ) ; } } } int ans = 0 ; for ( int i = 0 ; i < h ; i ++ ) { if ( dp [ n , i ] != - 1 ) ans = Math . Max ( ans , dp [ n , i ] ) ; } Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 16 , 17 , 14 , 20 , 20 , 11 , 22 } ; int H = 24 ; int L = 21 ; int R = 23 ; int size = A . Length ; goodint ( A , size , H , L , R ) ; } }
Range queries for alternatively addition and subtraction on given Array | C # program for the above approach ; Structure to represent a query ; This function fills the Prefix Array ; Initialise the prefix array ; Iterate all the element of [ ] arr and update the prefix array ; If n is even then , add the previous value of prefix array with the current value of arr ; if n is odd , then subtract the previous value of prefix Array from current value ; Function to find the result of alternatively adding and subtracting elements in the range [ L < R ] ; Case 1 : when L is zero ; Case 2 : When L is non zero ; If L is odd means range starts from odd position multiply result by - 1 ; Return the readonly result ; Function to find the sum of all alternative add and subtract between ranges [ L , R ] ; Declare prefix array ; Function Call to fill prefix [ ] arr ; Iterate for each query ; Driver Code ; Given array ; Given Queries ; Function Call
using System ; class GFG { class Query { public int L , R ; public Query ( int l , int r ) { L = l ; R = r ; } } ; static void fillPrefixArray ( int [ ] arr , int n , int [ ] prefixArray ) { prefixArray [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { prefixArray [ i ] = prefixArray [ i - 1 ] + arr [ i ] ; } else { prefixArray [ i ] = prefixArray [ i - 1 ] - arr [ i ] ; } } } static int findResultUtil ( int [ ] prefixArray , int L , int R ) { int result ; if ( L == 0 ) { result = prefixArray [ R ] ; } else { result = prefixArray [ R ] - prefixArray [ L - 1 ] ; } if ( L % 2 == 1 ) { result = result * ( - 1 ) ; } return result ; } static void findResult ( int [ ] arr , int n , Query [ ] q , int m ) { int [ ] prefixArray = new int [ n ] ; fillPrefixArray ( arr , n , prefixArray ) ; for ( int i = 0 ; i < m ; i ++ ) { Console . Write ( findResultUtil ( prefixArray , q [ i ] . L , q [ i ] . R ) + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 } ; int n = arr . Length ; Query [ ] q = { new Query ( 2 , 5 ) , new Query ( 6 , 8 ) , new Query ( 1 , 7 ) , new Query ( 4 , 8 ) , new Query ( 0 , 5 ) } ; int m = q . Length ; findResult ( arr , n , q , m ) ; } }
Partition of a set into K subsets with equal sum using BitMask and DP | C # program to check if the given array can be partitioned into K subsets with equal sum ; Function to check whether K required partitions are possible or not ; Return true as the entire array is the answer ; If total number of partitions exceeds size of the array ; If the array sum is not divisible by K ; No such partitions are possible ; Required sum of each subset ; Initialize dp array with - 1 ; Sum of empty subset is zero ; Iterate over all subsets / masks ; If current mask is invalid , continue ; Iterate over all array elements ; Check if the current element can be added to the current subset / mask ; Transition ; Driver Code
using System ; class GFG { static bool isKPartitionPossible ( int [ ] arr , int N , int K ) { if ( K == 1 ) return true ; if ( N < K ) return false ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % K != 0 ) return false ; int target = sum / K ; int [ ] dp = new int [ ( 1 << 15 ) ] ; for ( int i = 0 ; i < ( 1 << N ) ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( int mask = 0 ; mask < ( 1 << N ) ; mask ++ ) { if ( dp [ mask ] == - 1 ) continue ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( ( mask & ( 1 << i ) ) == 0 ) && dp [ mask ] + arr [ i ] <= target ) { dp [ mask ( 1 << i ) ] = ( dp [ mask ] + arr [ i ] ) % target ; } } } if ( dp [ ( 1 << N ) - 1 ] == 0 ) return true ; else return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 4 , 5 , 3 , 3 } ; int N = arr . Length ; int K = 3 ; if ( isKPartitionPossible ( arr , N , K ) ) { Console . Write ( " Partitions ▁ into ▁ equal ▁ " ) ; Console . Write ( " sum ▁ is ▁ possible . STRNEWLINE " ) ; } else { Console . Write ( " Partitions ▁ into ▁ equal ▁ " ) ; Console . Write ( " sum ▁ is ▁ not ▁ possible . STRNEWLINE " ) ; } } }
Minimum cost path in a Matrix by moving only on value difference of X | C # implementation to find the minimum number of operations required to move from ( 1 , 1 ) to ( N , M ) ; Function to find the minimum operations required to move to bottom - right cell of matrix ; Condition to check if the current cell is the bottom - right cell of the matrix ; Condition to check if the current cell is out of matrix ; Condition to check if the current indices is already computed ; Condition to check that the movement with the current value is not possible ; Recursive call to compute the number of operation required to compute the value ; Function to find the minimum number of operations required to reach the bottom - right cell ; Loop to iterate over every possible cell of the matrix ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static long MAX = ( long ) 1e18 ; static long n , m ; static List < List < long > > v = new List < List < long > > ( ) ; static long [ , ] dp = new long [ 151 , 151 ] ; static long min_operation ( long i , long j , long val , long x ) { if ( i == n - 1 && j == m - 1 ) { if ( val > v [ ( int ) i ] [ ( int ) j ] ) { return dp [ ( int ) i , ( int ) j ] = MAX ; } else { return dp [ ( int ) i , ( int ) j ] = v [ ( int ) i ] [ ( int ) j ] - val ; } } if ( i == n j == m ) { return dp [ ( int ) i , ( int ) j ] = MAX ; } if ( dp [ ( int ) i , ( int ) j ] != - 1 ) { return dp [ ( int ) i , ( int ) j ] ; } if ( val > v [ ( int ) i ] [ ( int ) j ] ) { return dp [ ( int ) i , ( int ) j ] = MAX ; } long temp = v [ ( int ) i ] [ ( int ) j ] - val ; temp += Math . Min ( min_operation ( i + 1 , j , val + x , x ) , min_operation ( i , j + 1 , val + x , x ) ) ; return dp [ ( int ) i , ( int ) j ] = temp ; } static long solve ( long x ) { long ans = Int64 . MaxValue ; for ( long i = 0 ; i < n ; i ++ ) { for ( long j = 0 ; j < m ; j ++ ) { long val = v [ ( int ) i ] [ ( int ) j ] - x * ( i + j ) ; for ( long k = 0 ; k < dp . GetLength ( 0 ) ; k ++ ) { for ( long l = 0 ; l < dp . GetLength ( 1 ) ; l ++ ) { dp [ k , l ] = - 1 ; } } val = min_operation ( 0 , 0 , val , x ) ; ans = Math . Min ( ans , val ) ; } } return ans ; } static public void Main ( ) { for ( int i = 0 ; i < 151 ; i ++ ) { v . Add ( new List < long > ( ) ) ; v [ i ] . Add ( 0 ) ; v [ i ] . Add ( 0 ) ; } v [ 0 ] [ 0 ] = 15 ; v [ 0 ] [ 1 ] = 153 ; v [ 1 ] [ 0 ] = 135 ; v [ 1 ] [ 1 ] = 17 ; n = 2 ; m = 2 ; long x = 3 ; Console . WriteLine ( solve ( x ) ) ; } }
Minimum count of numbers required from given array to represent S | C # implementation to find the minimum number of sequence required from array such that their sum is equal to given S ; Function to find the minimum elements required to get the sum of given value S ; Condition if the sequence is found ; Condition when no such sequence found ; Calling for without choosing the current index value ; Calling for after choosing the current index value ; Function for every array ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int printAllSubsetsRec ( int [ ] arr , int n , List < int > v , int sum ) { if ( sum == 0 ) { return v . Count ; } if ( sum < 0 ) return Int32 . MaxValue ; if ( n == 0 ) return Int32 . MaxValue ; int x = printAllSubsetsRec ( arr , n - 1 , v , sum ) ; v . Add ( arr [ n - 1 ] ) ; int y = printAllSubsetsRec ( arr , n , v , sum - arr [ n - 1 ] ) ; v . RemoveAt ( v . Count - 1 ) ; return Math . Min ( x , y ) ; } static int printAllSubsets ( int [ ] arr , int n , int sum ) { List < int > v = new List < int > ( ) ; return printAllSubsetsRec ( arr , n , v , sum ) ; } static void Main ( ) { int [ ] arr = { 2 , 1 , 4 , 3 , 5 , 6 } ; int sum = 6 ; int n = arr . Length ; Console . WriteLine ( printAllSubsets ( arr , n , sum ) ) ; } }
Minimum count of numbers required from given array to represent S | C # implementation to find the minimum number of sequence required from array such that their sum is equal to given S ; Function to find the count of minimum length of the sequence ; Loop to initialize the array as infinite in the row 0 ; Loop to find the solution by pre - computation for the sequence ; Minimum possible for the previous minimum value of the sequence ; Driver Code
using System ; class GFG { static int Count ( int [ ] S , int m , int n ) { int [ , ] table = new int [ m + 1 , n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { table [ 0 , i ] = int . MaxValue - 1 ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i , j ] = table [ i - 1 , j ] ; } else { table [ i , j ] = Math . Min ( table [ i - 1 , j ] , table [ i , j - S [ i - 1 ] ] + 1 ) ; } } } return table [ m , n ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 6 , 5 , 1 } ; int m = 4 ; Console . WriteLine ( Count ( arr , m , 11 ) ) ; } }
Maximize product of same | C # implementation to maximize product of same - indexed elements of same size subsequences ; Utility function to find the maximum ; Utility function to find the maximum scalar product from the equal length sub - sequences taken from the two given array ; Return a very small number if index is invalid ; If the sub - problem is already evaluated , then return it ; Take the maximum of all the recursive cases ; Function to find maximum scalar product from same size sub - sequences taken from the two given array ; Initialize a 2 - D array for memoization ; Driver Code
using System ; class GFG { static readonly int INF = 10000000 ; static int maximum ( int A , int B , int C , int D ) { return Math . Max ( Math . Max ( A , B ) , Math . Max ( C , D ) ) ; } static int maxProductUtil ( int X , int Y , int [ ] A , int [ ] B , int [ , ] dp ) { if ( X < 0 Y < 0 ) return - INF ; if ( dp [ X , Y ] != - 1 ) return dp [ X , Y ] ; dp [ X , Y ] = maximum ( A [ X ] * B [ Y ] + maxProductUtil ( X - 1 , Y - 1 , A , B , dp ) , A [ X ] * B [ Y ] , maxProductUtil ( X - 1 , Y , A , B , dp ) , maxProductUtil ( X , Y - 1 , A , B , dp ) ) ; return dp [ X , Y ] ; } static int maxProduct ( int [ ] A , int N , int [ ] B , int M ) { int [ , ] dp = new int [ N , M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { dp [ i , j ] = - 1 ; } } return maxProductUtil ( N - 1 , M - 1 , A , B , dp ) ; } public static void Main ( String [ ] args ) { int [ ] a = { - 2 , 6 , - 2 , - 5 } ; int [ ] b = { - 3 , 4 , - 2 , 8 } ; int n = a . Length ; int m = b . Length ; Console . Write ( maxProduct ( a , n , b , m ) ) ; } }
Check if one string can be converted to other using given operation | C # implementation of above approach ; Function that prints whether is it possible to make a String equal to T by performing given operations ; Base case , if we put the last character at front of A ; Base case , if we put the last character at back of A ; Condition if current sequence is matchable ; Condition for front move to ( i - 1 ) th character ; Condition for back move to ( i - 1 ) th character ; Condition if it is possible to make String A equal to String T ; Print readonly answer ; Driver Code
using System ; class GFG { static void twoStringsEquality ( String s , String t ) { int n = s . Length ; int [ , ] dp = new int [ n , n + 1 ] ; if ( s [ n - 1 ] == t [ 0 ] ) dp [ n - 1 , 1 ] = 1 ; if ( s [ n - 1 ] == t [ n - 1 ] ) dp [ n - 1 , 0 ] = 1 ; for ( int i = n - 1 ; i > 0 ; i -- ) { for ( int j = 0 ; j <= n - i ; j ++ ) { if ( dp [ i , j ] > 0 ) { if ( s [ i - 1 ] == t [ j ] ) dp [ i - 1 , j + 1 ] = 1 ; if ( s [ i - 1 ] == t [ i + j - 1 ] ) dp [ i - 1 , j ] = 1 ; } } } bool ans = false ; for ( int i = 0 ; i <= n ; i ++ ) { if ( dp [ 0 , i ] == 1 ) { ans = true ; break ; } } if ( ans == true ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else Console . Write ( " No " + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String S = " abab " ; String T = " baab " ; twoStringsEquality ( S , T ) ; } }