codeparrot/xlcost-text-to-code huggingface dataset:
Maximum Product Subarray | Added negative product case | C # program to find maximum subarray product . ; Function to find maximum subarray product . ; As maximum product can be negative , so initialize ans with minimum integer value . ;  ; Variable to store minimum product until current value . ; Variable used during updation of maximum product and minimum product . ; If current element is positive , update maxval . Update minval if it is negative . ; If current element is zero , maximum product cannot end at current element . Update minval with 1 and maxval with 0. maxval is updated to 0 as in case all other elements are negative , then maxval is 0. ; If current element is negative , then new value of maxval is previous minval * arr [ i ] and new value of minval is previous maxval * arr [ i ] . Before updating maxval , store its previous value in prevMax to be used to update minval . ; Update ans if necessary . ; If maxval is zero , then to calculate product for next iteration , it should be set to 1 as maximum product subarray does not include 0. The minimum possible value to be considered in maximum product subarray is already stored in minval , so when maxval is negative it is set to 1. ; Driver Code
using System ; class GFG { static int findMaxProduct ( int [ ] arr , int n ) { int i ; int ans = Int32 . MinValue ; until current . ' ' ' int minval = 1 ; int prevMax ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { maxval = maxval * arr [ i ] ; minval = Math . Min ( 1 , minval * arr [ i ] ) ; } else if ( arr [ i ] == 0 ) { minval = 1 ; maxval = 0 ; } else if ( arr [ i ] < 0 ) { prevMax = maxval ; maxval = minval * arr [ i ] ; minval = prevMax * arr [ i ] ; } ans = Math . Max ( ans , maxval ) ; if ( maxval <= 0 ) { maxval = 1 ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 0 , - 4 , 0 , - 2 } ; int n = arr . Length ; Console . WriteLine ( findMaxProduct ( arr , n ) ) ; } }
Maximum and Minimum Values of an Algebraic Expression | C # program to find the maximum and minimum values of an Algebraic expression of given form ; Finding sum of array elements ; shifting the integers by 50 so that they become positive ; dp [ i ] [ j ] represents true if sum j can be reachable by choosing i numbers ; if dp [ i ] [ j ] is true , that means it is possible to select i numbers from ( n + m ) numbers to sum upto j ; k can be at max n because the left expression has n numbers ; checking if a particular sum can be reachable by choosing n numbers ; getting the actual sum as we shifted the numbers by / 50 to avoid negative indexing in array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static double INF = 1e9 ; static int MAX = 50 ; static void minMaxValues ( int [ ] arr , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { sum += arr [ i ] ; arr [ i ] += 50 ; } bool [ , ] dp = new bool [ MAX + 1 , MAX * MAX + 1 ] ; dp [ 0 , 0 ] = true ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { for ( int k = Math . Min ( n , i + 1 ) ; k >= 1 ; k -- ) { for ( int j = 0 ; j < MAX * MAX + 1 ; j ++ ) { if ( dp [ k - 1 , j ] ) dp [ k , j + arr [ i ] ] = true ; } } } double max_value = - 1 * INF , min_value = INF ; for ( int i = 0 ; i < MAX * MAX + 1 ; i ++ ) { if ( dp [ n , i ] ) { int temp = i - 50 * n ; max_value = Math . Max ( max_value , temp * ( sum - temp ) ) ; min_value = Math . Min ( min_value , temp * ( sum - temp ) ) ; } } Console . WriteLine ( " Maximum ▁ Value : ▁ " + max_value + " STRNEWLINE " + " Minimum ▁ Value : ▁ " + min_value + " STRNEWLINE " ) ; } public static void Main ( ) { int n = 2 , m = 2 ; int [ ] arr = { 1 , 2 , 3 , 4 } ; minMaxValues ( arr , n , m ) ; } }
Check if any valid sequence is divisible by M | C # program to check if any valid sequence is divisible by M ; Base case ; check if sum is divisible by M ; check if the current state is already computed ; 1. Try placing ' + ' ; 2. Try placing ' - ' ; calculate value of res for recursive case ; store the value for res for current states and return for parent call ; Driver code
using System ; class GFG { static int MAX = 1000 ; static Boolean isPossible ( int n , int index , int sum , int M , int [ ] arr , int [ , ] dp ) { if ( index == n ) { if ( ( sum % M ) == 0 ) return true ; return false ; } else if ( sum < 0 sum >= MAX ) return false ; if ( dp [ index , sum ] != - 1 ) { if ( dp [ index , sum ] == 0 ) return false ; return true ; } Boolean placeAdd = isPossible ( n , index + 1 , sum + arr [ index ] , M , arr , dp ) ; Boolean placeMinus = isPossible ( n , index + 1 , sum - arr [ index ] , M , arr , dp ) ; Boolean res = ( placeAdd placeMinus ) ; dp [ index , sum ] = ( res ) ? 1 : 0 ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 6 } ; int n = arr . Length ; int M = 4 ; int [ , ] dp = new int [ n + 1 , MAX ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = - 1 ; Boolean res ; res = isPossible ( n , 0 , 0 , M , arr , dp ) ; Console . WriteLine ( ( res ? " True " : " False " ) ) ; } }
Dynamic Programming on Trees | Set 2 | Function to pre - calculate the array in [ ] which stores the maximum height when travelled via branches ; Function to pre - calculate the array in [ ] which stores the maximum height when travelled via branches ; Initially every node has 0 height ; Traverse in the subtree of u ; If child is same as parent ; dfs called ; Recursively calculate the max height ; Function to pre - calculate the array ouut [ ] which stores the maximum height when traveled via parent ; Stores the longest and second longest branches ; Traverse in the subtress of u ; Compare and store the longest and second longest ; Traverse in the subtree of u ; If longest branch has the node , then consider the second longest branch ; Recursively calculate out [ i ] ; dfs function call ; Function to print all the maximum heights from every node ; Traversal to calculate in [ ] array ; Traversal to calculate out [ ] array ; Print all maximum heights ; Driver Code ; Initialize the tree given in the diagram ; Function to print the maximum height from every node
using System ; using System . Collections . Generic ; public class GFG { static int MAX_NODES = 100 ; static int [ ] In = new int [ MAX_NODES ] ; static int [ ] Out = new int [ MAX_NODES ] ; static void dfs1 ( List < List < int > > v , int u , int parent ) { In [ u ] = 0 ; for ( int j = 0 ; j < v [ u ] . Count ; j ++ ) { int child = v [ u ] [ j ] ; if ( child == parent ) continue ; dfs1 ( v , child , u ) ; In [ u ] = Math . Max ( In [ u ] , 1 + In [ child ] ) ; } } static void dfs2 ( List < List < int > > v , int u , int parent ) { int mx1 = - 1 , mx2 = - 1 ; for ( int j = 0 ; j < v [ u ] . Count ; j ++ ) { int child = v [ u ] [ j ] ; if ( child == parent ) continue ; if ( In [ child ] >= mx1 ) { mx2 = mx1 ; mx1 = In [ child ] ; } else if ( In [ child ] > mx2 ) mx2 = In [ child ] ; } for ( int j = 0 ; j < v [ u ] . Count ; j ++ ) { int child = v [ u ] [ j ] ; if ( child == parent ) continue ; int longest = mx1 ; if ( mx1 == In [ child ] ) longest = mx2 ; Out [ child ] = 1 + Math . Max ( Out [ u ] , 1 + longest ) ; dfs2 ( v , child , u ) ; } } static void addEdge ( List < List < int > > adj , int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static void printHeights ( List < List < int > > v , int n ) { dfs1 ( v , 1 , 0 ) ; dfs2 ( v , 1 , 0 ) ; for ( int i = 1 ; i < n ; i ++ ) Console . WriteLine ( " The ▁ maximum ▁ height ▁ when ▁ node ▁ " + i + " ▁ is ▁ considered ▁ as ▁ root ▁ is ▁ " + Math . Max ( In [ i ] , Out [ i ] ) ) ; } static public void Main ( ) { int V = 12 ; List < List < int > > adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < V ; i ++ ) adj . Add ( new List < int > ( ) ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 1 , 4 ) ; addEdge ( adj , 2 , 5 ) ; addEdge ( adj , 2 , 6 ) ; addEdge ( adj , 3 , 7 ) ; addEdge ( adj , 7 , 10 ) ; addEdge ( adj , 7 , 11 ) ; addEdge ( adj , 4 , 8 ) ; addEdge ( adj , 4 , 9 ) ; printHeights ( adj , V ) ; } }
Golomb sequence | C # Program to find first n terms of Golomb sequence . ; Return the nth element of Golomb sequence ; base case ; Recursive Step ; Print the first n term of Golomb Sequence ; Finding first n terms of Golomb Sequence . ; Driver Code
using System ; class GFG { static int findGolomb ( int n ) { if ( n == 1 ) return 1 ; return 1 + findGolomb ( n - findGolomb ( findGolomb ( n - 1 ) ) ) ; } static void printGolomb ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( findGolomb ( i ) + " ▁ " ) ; } public static void Main ( ) { int n = 9 ; printGolomb ( n ) ; } }
Printing Items in 0 / 1 Knapsack | C # code for Dynamic Programming based solution for 0 - 1 Knapsack problem ; A utility function that returns maximum of two integers ; Prints the items which are put in a knapsack of capacity W ; Build table K [ ] [ ] in bottom up manner ; stores the result of Knapsack ; either the result comes from the top ( K [ i - 1 ] [ w ] ) or from ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] ) as in Knapsack table . If it comes from the latter one / it means the item is included . ; This item is included . ; Since this weight is included its value is deducted ; Driver code
using System ; class GFG { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static void printknapSack ( int W , int [ ] wt , int [ ] val , int n ) { int i , w ; int [ , ] K = new int [ n + 1 , W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 w == 0 ) K [ i , w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i , w ] = Math . Max ( val [ i - 1 ] + K [ i - 1 , w - wt [ i - 1 ] ] , K [ i - 1 , w ] ) ; else K [ i , w ] = K [ i - 1 , w ] ; } } int res = K [ n , W ] ; Console . WriteLine ( res ) ; w = W ; for ( i = n ; i > 0 && res > 0 ; i -- ) { if ( res == K [ i - 1 , w ] ) continue ; else { Console . Write ( wt [ i - 1 ] + " ▁ " ) ; res = res - val [ i - 1 ] ; w = w - wt [ i - 1 ] ; } } } public static void Main ( ) { int [ ] val = { 60 , 100 , 120 } ; int [ ] wt = { 10 , 20 , 30 } ; int W = 50 ; int n = val . Length ; printknapSack ( W , wt , val , n ) ; } }
s | C # program for the above approach ; To sort the array and return the answer ; Sort the array ; Fill all stated with - 1 when only one element ; As dp [ 0 ] = 0 ( base case ) so min no of elements to be removed are n - 1 elements ; Iterate from 1 to n - 1 ; Driver code
using System ; class GFG { static int removals ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = - 1 ; int ans = n - 1 ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = i ; int j = dp [ i - 1 ] ; while ( j != i && arr [ i ] - arr [ j ] > k ) { j ++ ; } dp [ i ] = Math . Min ( dp [ i ] , j ) ; ans = Math . Min ( ans , ( n - ( i - j + 1 ) ) ) ; } return ans ; } static public void Main ( ) { int [ ] a = { 1 , 3 , 4 , 9 , 10 , 11 , 12 , 17 , 20 } ; int n = a . Length ; int k = 4 ; Console . Write ( removals ( a , n , k ) ) ; } }
Maximum number of segments of lengths a , b and c | C # implementation to divide N into maximum number of segments of length a , b and c ; function to find the maximum number of segments ; stores the maximum number of segments each index can have ; initialize with - 1 ; 0 th index will have 0 segments base case ; traverse for all possible segments till n ; conditions if ( i + a <= n ) avoid buffer overflow ; if ( i + b <= n ) avoid buffer overflow ; if ( i + c <= n ) avoid buffer overflow ; Driver code
using System ; class GFG { static int maximumSegments ( int n , int a , int b , int c ) { int [ ] dp = new int [ n + 10 ] ; for ( int i = 0 ; i < n + 10 ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ i ] != - 1 ) { dp [ i + a ] = Math . Max ( dp [ i ] + 1 , dp [ i + a ] ) ; dp [ i + b ] = Math . Max ( dp [ i ] + 1 , dp [ i + b ] ) ; dp [ i + c ] = Math . Max ( dp [ i ] + 1 , dp [ i + c ] ) ; } } return dp [ n ] ; } public static void Main ( ) { int n = 7 , a = 5 , b = 2 , c = 5 ; Console . Write ( maximumSegments ( n , a , b , c ) ) ; } }
Maximize the sum of selected numbers from an array to make it empty | C # implementation of the approach ; Function to maximise the sum of selected numbers by deleting occurences of Ai - 1 and Ai + 1 ; Number of elements in the array ; Largest element in the array ; An array to count the occurence of each element ; ans to store the result ; Using the above mentioned approach ; if occurence is greater than 0 ; add it to ans ; decrease i - 1 th element by 1 ; decrease ith element by 1 ; decrease i ; Driver code
using System ; using System . Linq ; class GFG { static int getMaximumSum ( int [ ] arr ) { int n = arr . Length ; int max = arr . Max ( ) ; int [ ] freq = new int [ max + 1 ] ; for ( int j = 0 ; j < n ; j ++ ) { freq [ arr [ j ] ] ++ ; } int ans = 0 , i = max ; while ( i > 0 ) { if ( freq [ i ] > 0 ) { ans += i ; freq [ i - 1 ] -- ; freq [ i ] -- ; } else { i -- ; } } return ans ; } public static void Main ( string [ ] args ) { int [ ] a = { 1 , 2 , 3 } ; Console . Write ( getMaximumSum ( a ) ) ; } }
Print n terms of Newman | C # Program to print n terms of Newman - Conway Sequence ; Function to find the n - th element ; Declare array to store sequence ; Driver code
using System ; class GFG { public static void sequence ( int n ) { int [ ] f = new int [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; Console . Write ( f [ 1 ] + " ▁ " + f [ 2 ] + " ▁ " ) ; for ( int i = 3 ; i <= n ; i ++ ) { f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; Console . Write ( f [ i ] + " ▁ " ) ; } } public static void Main ( ) { int n = 13 ; sequence ( n ) ; } }
LCS formed by consecutive segments of at least length K | C # program to find the Length of Longest subsequence formed by consecutive segments of at least length K ; Returns the length of the longest common subsequence with a minimum of length of K consecutive segments ; length of strings ; declare the lcs and cnt array ; iterate from i = 1 to n and j = 1 to j = m ; stores the maximum of lcs [ i - 1 ] [ j ] and lcs [ i ] [ j - 1 ] ; when both the characters are equal of s1 and s2 ; when length of common segment is more than k , then update lcs answer by adding that segment to the answer ; formulate for all length of segments to get the longest subsequence with consecutive Common Segment of length of min k length ; update lcs value by adding segment length ; driver code to check the above function
using System ; class GFG { static int longestSubsequenceCommonSegment ( int k , string s1 , string s2 ) { int n = s1 . Length ; int m = s2 . Length ; int [ , ] lcs = new int [ n + 1 , m + 1 ] ; int [ , ] cnt = new int [ n + 1 , m + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { lcs [ i , j ] = Math . Max ( lcs [ i - 1 , j ] , lcs [ i , j - 1 ] ) ; if ( s1 [ i - 1 ] == s2 [ j - 1 ] ) cnt [ i , j ] = cnt [ i - 1 , j - 1 ] + 1 ; if ( cnt [ i , j ] >= k ) { for ( int a = k ; a <= cnt [ i , j ] ; a ++ ) lcs [ i , j ] = Math . Max ( lcs [ i , j ] , lcs [ i - a , j - a ] + a ) ; } } } return lcs [ n , m ] ; } public static void Main ( ) { int k = 4 ; string s1 = " aggasdfa " ; string s2 = " aggajasdfa " ; Console . WriteLine ( longestSubsequenceCommonSegment ( k , s1 , s2 ) ) ; } }
Check for possible path in 2D matrix | C # program to find if there is path from top left to right bottom ; To find the path from top left to bottom right ; Directions ; Queue ; Insert the top right corner . ; Until queue is empty ; Mark as visited ; Destination is reached . ; Check all four directions ; Using the direction array ; Not blocked and valid ; Driver Code ; Given array ; Path from arr [ 0 ] [ 0 ] to arr [ row ] [ col ]
using System ; using System . Collections ; class GFG { static int row = 5 ; static int col = 5 ; static bool isPath ( int [ , ] arr ) { int [ , ] dir = { { 0 , 1 } , { 0 , - 1 } , { 1 , 0 } , { - 1 , 0 } } ; Queue q = new Queue ( ) ; q . Enqueue ( new Tuple < int , int > ( 0 , 0 ) ) ; while ( q . Count > 0 ) { Tuple < int , int > p = ( Tuple < int , int > ) ( q . Peek ( ) ) ; q . Dequeue ( ) ; arr [ p . Item1 , p . Item2 ] = - 1 ; if ( p == new Tuple < int , int > ( row - 1 , col - 1 ) ) return true ; for ( int i = 0 ; i < 4 ; i ++ ) { int a = p . Item1 + dir [ i , 0 ] ; int b = p . Item2 + dir [ i , 1 ] ; if ( a >= 0 && b >= 0 && a < row && b < col && arr [ a , b ] != - 1 ) { q . Enqueue ( new Tuple < int , int > ( a , b ) ) ; } } } return false ; } static void Main ( ) { int [ , ] arr = { { 0 , 0 , 0 , - 1 , 0 } , { - 1 , 0 , 0 , - 1 , - 1 } , { 0 , 0 , 0 , - 1 , 0 } , { - 1 , 0 , - 1 , 0 , - 1 } , { 0 , 0 , - 1 , 0 , 0 } } ; if ( isPath ( arr ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Next Smaller Element | A Stack based C # program to find next smaller element for all array elements in same order as input . using System ; ; prints element and NSE pair for all elements of arr [ ] of size n ; push the first element to stack ; iterate for rest of the elements ; if stack is not empty , then pop an element from stack . If the popped element is greater than next , then a ) print the pair b ) keep popping while elements are greater and stack is not empty ; push next to stack so that we can find next smaller for it ; After iterating over the loop , the remaining elements in stack do not have the next smaller element , so print - 1 for them ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static void printNSE ( int [ ] arr , int n ) { Stack < int > s = new Stack < int > ( ) ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; s . Push ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . Count == 0 ) { s . Push ( arr [ i ] ) ; continue ; } while ( s . Count != 0 && s . Peek ( ) > arr [ i ] ) { mp . Add ( s . Peek ( ) , arr [ i ] ) ; s . Pop ( ) ; } s . Push ( arr [ i ] ) ; } while ( s . Count != 0 ) { mp . Add ( s . Peek ( ) , - 1 ) ; s . Pop ( ) ; } for ( int i = 0 ; i < n ; i ++ ) Console . WriteLine ( arr [ i ] + " ▁ - - - > ▁ " + mp [ arr [ i ] ] ) ; } public static void Main ( ) { int [ ] arr = { 11 , 13 , 21 , 3 } ; int n = arr . Length ; printNSE ( arr , n ) ; } }
Entringer Number | C # Code For Entringer Number ; Return Entringer Number E ( n , k ) ; Base cases ; Finding dp [ i ] [ j ] ; Driver program to test above function
using System ; class GFG { static int zigzag ( int n , int k ) { int [ , ] dp = new int [ n + 1 , k + 1 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) dp [ i , 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= Math . Min ( i , k ) ; j ++ ) dp [ i , j ] = dp [ i , j - 1 ] + dp [ i - 1 , i - j ] ; } return dp [ n , k ] ; } public static void Main ( ) { int n = 4 , k = 3 ; Console . WriteLine ( zigzag ( n , k ) ) ; } }
Lobb Number | C # Code For Lobb Number ; Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Return the Lm , n Lobb Number . ; Driver program to test above function
using System ; class GFG { static int binomialCoeff ( int n , int k ) { int [ , ] C = new int [ n + 1 , k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } return C [ n , k ] ; } static int lobb ( int n , int m ) { return ( ( 2 * m + 1 ) * binomialCoeff ( 2 * n , m + n ) ) / ( m + n + 1 ) ; } public static void Main ( ) { int n = 5 , m = 3 ; Console . WriteLine ( lobb ( n , m ) ) ; } }
Count of arrays having consecutive element with different values | C # program to find count of arrays . ; static int MAXN = 109 ; ; Initialising dp [ 0 ] and dp [ 1 ] . ; Computing f ( i ) for each 2 <= i <= n . ; Driver code
using System ; class GFG { public static int countarray ( int n , int k , int x ) { int [ ] dp = new int [ 109 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ; return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ; } public static void Main ( ) { int n = 4 , k = 3 , x = 2 ; Console . WriteLine ( countarray ( n , k , x ) ) ; } }
Number of palindromic subsequences of length k where k <= 3 | C # program to count number of subsequences of given length . ; Precompute the prefix and suffix array . ; Precompute the prefix 2D array ; Precompute the Suffix 2D array . ; Find the number of palindromic subsequence of length k ; If k is 1. ; If k is 2 ; Adding all the products of prefix array ; For k greater than 2. Adding all the products of value of prefix and suffix array . ; Driver code
using System ; class GFG { static int MAX = 100 ; static int MAX_CHAR = 26 ; static void precompute ( string s , int n , int [ , ] l , int [ , ] r ) { l [ s [ 0 ] - ' a ' , 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) l [ j , i ] += l [ j , i - 1 ] ; l [ s [ i ] - ' a ' , i ] ++ ; } r [ s [ n - 1 ] - ' a ' , n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) r [ j , i ] += r [ j , i + 1 ] ; r [ s [ i ] - ' a ' , i ] ++ ; } } static int countPalindromes ( int k , int n , int [ , ] l , int [ , ] r ) { int ans = 0 ; if ( k == 1 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += l [ i , n - 1 ] ; return ans ; } if ( k == 2 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += ( ( l [ i , n - 1 ] * ( l [ i , n - 1 ] - 1 ) ) / 2 ) ; return ans ; } for ( int i = 1 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < MAX_CHAR ; j ++ ) ans += l [ j , i - 1 ] * r [ j , i + 1 ] ; return ans ; } public static void Main ( ) { string s = " aabab " ; int k = 2 ; int n = s . Length ; int [ , ] l = new int [ MAX_CHAR , MAX ] ; int [ , ] r = new int [ MAX_CHAR , MAX ] ; precompute ( s , n , l , r ) ; Console . Write ( countPalindromes ( k , n , l , r ) ) ; } }
Minimum cost to make Longest Common Subsequence of length k | C # program to find subarray with sum closest to 0 ; Return Minimum cost to make LCS of length k ; If k is 0. ; If length become less than 0 , return big number . ; If state already calculated . ; Finding the cost ; Finding minimum cost and saving the state value ; Driver code
using System ; class GFG { static int N = 30 ; static int solve ( char [ ] X , char [ ] Y , int l , int r , int k , int [ , , ] dp ) { if ( k == 0 ) { return 0 ; } if ( l < 0 r < 0 ) { return ( int ) 1e9 ; } if ( dp [ l , r , k ] != - 1 ) { return dp [ l , r , k ] ; } int cost = ( X [ l ] - ' a ' ) ^ ( Y [ r ] - ' a ' ) ; return dp [ l , r , k ] = Math . Min ( Math . Min ( cost + solve ( X , Y , l - 1 , r - 1 , k - 1 , dp ) , solve ( X , Y , l - 1 , r , k , dp ) ) , solve ( X , Y , l , r - 1 , k , dp ) ) ; } public static void Main ( String [ ] args ) { char [ ] X = " abble " . ToCharArray ( ) ; char [ ] Y = " pie " . ToCharArray ( ) ; int n = X . Length ; int m = Y . Length ; int k = 2 ; int [ , , ] dp = new int [ N , N , N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int l = 0 ; l < N ; l ++ ) { dp [ i , j , l ] = - 1 ; } } } int ans = solve ( X , Y , n - 1 , m - 1 , k , dp ) ; Console . WriteLine ( ans == 1e9 ? - 1 : ans ) ; } }