codeparrot/xlcost-text-to-code huggingface dataset:
Hosoya 's Triangle | C # Code for Hosoya Triangle ; Print the Hosoya triangle of height n . ; base case . ; For each row . ; for each column ; ; recursive steps . ; printing the solution ; Driver program
using System ; class GFG { static int N = 5 ; static void printHosoya ( int n ) { int [ , ] dp = new int [ N , N ] ; dp [ 0 , 0 ] = dp [ 1 , 0 ] = 1 ; dp [ 1 , 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i > j ) dp [ i , j ] = dp [ i - 1 , j ] + dp [ i - 2 , j ] ; else dp [ i , j ] = dp [ i - 1 , j - 1 ] + dp [ i - 2 , j - 2 ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) Console . Write ( dp [ i , j ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } } public static void Main ( ) { int n = 5 ; printHosoya ( n ) ; } }
Different ways to sum n using numbers greater than or equal to m | C # program to find number of ways to which numbers that are greater than given number can be added to get sum . ; Return number of ways to which numbers that are greater than given number can be added to get sum . ; Filling the table . k is for numbers greater than or equal that are allowed . ; i is for sum ; initializing dp [ i ] [ k ] to number ways to get sum using numbers greater than or equal k + 1 ; if i > k ; Driver Program
using System ; class GFG { static int numberofways ( int n , int m ) { int [ , ] dp = new int [ n + 2 , n + 2 ] ; dp [ 0 , n + 1 ] = 1 ; for ( int k = n ; k >= m ; k -- ) { for ( int i = 0 ; i <= n ; i ++ ) { dp [ i , k ] = dp [ i , k + 1 ] ; if ( i - k >= 0 ) dp [ i , k ] = ( dp [ i , k ] + dp [ i - k , k ] ) ; } } return dp [ n , m ] ; } public static void Main ( ) { int n = 3 , m = 1 ; Console . WriteLine ( numberofways ( n , m ) ) ; } }
Entringer Number | C # Code For Entringer Number ; Return Entringer Number E ( n , k ) ; Base Case ; Base Case ; Recursive step ; Driver program to test above function
using System ; class GFG { static int zigzag ( int n , int k ) { if ( n == 0 && k == 0 ) return 1 ; if ( k == 0 ) return 0 ; return zigzag ( n , k - 1 ) + zigzag ( n - 1 , n - k ) ; } public static void Main ( ) { int n = 4 , k = 3 ; Console . WriteLine ( zigzag ( n , k ) ) ; } }
Eulerian Number | C # program to find Eulerian number A ( n , m ) ; Return euleriannumber A ( n , m ) ; For each row from 1 to n ; For each column from 0 to m ; If i is greater than j ; If j is 0 , then make that state as 1. ; basic recurrence relation . ; driver code
using System ; class Eulerian { public static int eulerian ( int n , int m ) { int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i > j ) { if ( j == 0 ) dp [ i , j ] = 1 ; else dp [ i , j ] = ( ( i - j ) * dp [ i - 1 , j - 1 ] ) + ( ( j + 1 ) * dp [ i - 1 , j ] ) ; } } } return dp [ n , m ] ; } public static void Main ( ) { int n = 3 , m = 1 ; Console . WriteLine ( eulerian ( n , m ) ) ; } }
Delannoy Number | C # Program for finding nth Delannoy Number . ; Return the nth Delannoy Number . ; Base case ; Recursive step . ; driver function
using System ; public class GfG { public static int dealnnoy ( int n , int m ) { if ( m == 0 n == 0 ) return 1 ; return dealnnoy ( m - 1 , n ) + dealnnoy ( m - 1 , n - 1 ) + dealnnoy ( m , n - 1 ) ; } public static void Main ( ) { int n = 3 , m = 4 ; Console . WriteLine ( dealnnoy ( n , m ) ) ; } }
Delannoy Number | C # Program of finding nth Delannoy Number . ; Return the nth Delannoy Number . ; Base cases ; Driven Program
using System ; class GFG { static int dealnnoy ( int n , int m ) { int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i , 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) dp [ 0 , i ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) dp [ i , j ] = dp [ i - 1 , j ] + dp [ i - 1 , j - 1 ] + dp [ i , j - 1 ] ; return dp [ m , n ] ; } public static void Main ( ) { int n = 3 , m = 4 ; Console . WriteLine ( dealnnoy ( n , m ) ) ; } }
Longest alternating ( positive and negative ) subarray starting at every index | C # program to find longest alternating subarray starting from every index . ; Fill count [ ] from end . ; Print result ; Driver program
using System ; class Longest { public static void longestAlternating ( int [ ] arr , int n ) { int [ ] count = new int [ n ] ; count [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ; else count [ i ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( count [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] a = { - 5 , - 1 , - 1 , 2 , - 2 , - 3 } ; int n = 6 ; longestAlternating ( a , n ) ; } }
Maximum value with the choice of either dividing or considering as it is | C # program for maximize result when we have choice to divide or consider as it is . ; function for calculating max possible result ; Compute remaining values in bottom up manner . ; Driver code
using System ; class GFG { static int maxDP ( int n ) { int [ ] res = new int [ n + 1 ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res [ i ] = Math . Max ( i , ( res [ i / 2 ] + res [ i / 3 ] + res [ i / 4 ] + res [ i / 5 ] ) ) ; return res [ n ] ; } public static void Main ( ) { int n = 60 ; Console . WriteLine ( " MaxSum ▁ = ▁ " + maxDP ( n ) ) ; } }
Maximum difference of zeros and ones in binary string | C # Program to find the length of substring with maximum difference of zeroes and ones in binary string . ; Return true if there all 1 s ; Checking each index is 0 or not . ; Find the length of substring with maximum difference of zeroes and ones in binary string ; If string is over . ; If the state is already calculated . ; Returns length of substring which is having maximum difference of number of 0 s and number of 1 s ; If all 1 s return - 1. ; Else find the length . ; Driven Program
using System ; class GFG { static int MAX = 100 ; public static bool allones ( string s , int n ) { int co = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) co += ( s [ i ] == '1' ? 1 : 0 ) ; return ( co == n ) ; } public static int findlength ( int [ ] arr , string s , int n , int ind , int st , int [ , ] dp ) { if ( ind >= n ) return 0 ; if ( dp [ ind , st ] != - 1 ) return dp [ ind , st ] ; if ( st == 0 ) return dp [ ind , st ] = Math . Max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , findlength ( arr , s , n , ind + 1 , 0 , dp ) ) ; else return dp [ ind , st ] = Math . Max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , 0 ) ; } public static int maxLen ( string s , int n ) { if ( allones ( s , n ) ) return - 1 ; int [ ] arr = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = ( s [ i ] == '0' ? 1 : - 1 ) ; int [ , ] dp = new int [ MAX , 3 ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < 3 ; j ++ ) dp [ i , j ] = - 1 ; return findlength ( arr , s , n , 0 , 0 , dp ) ; } static void Main ( ) { string s = "11000010001" ; int n = 11 ; Console . Write ( maxLen ( s , n ) ) ; } }
Minimum jumps to reach last building in a matrix | Recursive C # program to find minimum jumps to reach last building from first . ; Returns minimum jump path from ( 0 , 0 ) to ( m , n ) in height [ R ] [ C ] ; base case ; Find minimum jumps if we go through diagonal ; Find minimum jumps if we go through down ; Find minimum jumps if we go through right ; return minimum jumps ; Driver code
using System ; class GFG { static bool isSafe ( int x , int y ) { return ( x < 4 && y < 3 ) ; } static int minJump ( int [ , ] height , int x , int y ) { if ( x == 4 - 1 && y == 3 - 1 ) return 0 ; int diag = int . MaxValue ; if ( isSafe ( x + 1 , y + 1 ) ) diag = minJump ( height , x + 1 , y + 1 ) + Math . Abs ( height [ x , y ] - height [ x + 1 , y + 1 ] ) ; int down = int . MaxValue ; if ( isSafe ( x + 1 , y ) ) down = minJump ( height , x + 1 , y ) + Math . Abs ( height [ x , y ] - height [ x + 1 , y ] ) ; int right = int . MaxValue ; if ( isSafe ( x , y + 1 ) ) right = minJump ( height , x , y + 1 ) + Math . Abs ( height [ x , y ] - height [ x , y + 1 ] ) ; return Math . Min ( down , Math . Min ( right , diag ) ) ; } public static void Main ( ) { int [ , ] height = { { 5 , 4 , 2 } , { 9 , 2 , 1 } , { 2 , 5 , 9 } , { 1 , 3 , 11 } } ; Console . Write ( minJump ( height , 0 , 0 ) ) ; } }
Maximum sum subsequence with at | C # program to find maximum sum subsequence such that elements are at least k distance away . ; MS [ i ] is going to store maximum sum subsequence in subarray from arr [ i ] to arr [ n - 1 ] ; We fill MS from right to left . ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr , int N , int k ) { int [ ] MS = new int [ N ] ; MS [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( i + k + 1 >= N ) MS [ i ] = Math . Max ( arr [ i ] , MS [ i + 1 ] ) ; else MS [ i ] = Math . Max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ; } return MS [ 0 ] ; } public static void Main ( ) { int N = 10 , k = 2 ; int [ ] arr = { 50 , 70 , 40 , 50 , 90 , 70 , 60 , 40 , 70 , 50 } ; Console . WriteLine ( maxSum ( arr , N , k ) ) ; } }
Length of Longest Balanced Subsequence | C # program to find length of the longest balanced subsequence . ; Variable to count all the open brace that does not have the corresponding closing brace . ; To count all the close brace that does not have the corresponding open brace . ; Iterating over the String ; Number of open braces that hasn 't been closed yet. ; Number of close braces that cannot be mapped to any open brace . ; Mapping the ith close brace to one of the open brace . ; Driver Code
using System ; class GFG { static int maxLength ( String s , int n ) { int invalidOpenBraces = 0 ; int invalidCloseBraces = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ( ' ) { invalidOpenBraces ++ ; } else { if ( invalidOpenBraces == 0 ) { invalidCloseBraces ++ ; } else { invalidOpenBraces -- ; } } } return ( n - ( invalidOpenBraces + invalidCloseBraces ) ) ; } public static void Main ( ) { string s = " ( ) ( ( ( ( ( ( ) " ; int n = s . Length ; Console . WriteLine ( maxLength ( s , n ) ) ; } }
Longest alternating sub | C # program to calculate longest alternating sub - array for each index elements ; Function to calculate alternating sub - array for each index of array elements ; Initialize the base state of len [ ] ; Calculating value for each element ; If both elements are different then add 1 to next len [ i + 1 ] ; else initialize to 1 ; Print lengths of binary subarrays . ; Driver code
using System ; class GFG { static void alternateSubarray ( bool [ ] arr , int n ) { int [ ] len = new int [ n ] ; len [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( arr [ i ] ^ arr [ i + 1 ] == true ) len [ i ] = len [ i + 1 ] + 1 ; else len [ i ] = 1 ; } for ( int i = 0 ; i < n ; ++ i ) Console . Write ( len [ i ] + " ▁ " ) ; } public static void Main ( ) { bool [ ] arr = { true , false , true , false , false , true } ; int n = arr . Length ; alternateSubarray ( arr , n ) ; } }
Longest alternating sub | C # program to calculate longest alternating sub - array for each index elements ; Function to calculate alternating sub - array for each index of array elements ; Initialize count variable for storing length of sub - array ; Initialize ' prev ' variable which indicates the previous element while traversing for index ' i ' ; If both elements are same print elements because alternate element is not found for current index ; print count and decrement it . ; Increment count for next element ; Re - initialize previous variable ; If elements are still available after traversing whole array , print it ; Driver program
using System ; public class Test { static void alternateSubarray ( bool [ ] arr , int n ) { int count = 1 ; bool prev = arr [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) { if ( ( arr [ i ] ^ prev ) == false ) { while ( count > 0 ) { Console . Write ( count -- + " ▁ " ) ; } } ++ count ; prev = arr [ i ] ; } while ( count != 0 ) { Console . Write ( count -- + " ▁ " ) ; } } public static void Main ( ) { bool [ ] arr = { true , false , true , false , false , true } ; int n = arr . Length ; alternateSubarray ( arr , n ) ; } }
Longest Common Subsequence with at most k changes allowed | C # program to find LCS of two arrays with k changes allowed in first array . ; Return LCS with at most k changes allowed . ; If at most changes is less than 0. ; If any of two array is over . ; Making a reference variable to dp [ n , m , k ] ; If value is already calculated , return that value . ; calculating LCS with no changes made . ; calculating LCS when array element are same . ; calculating LCS with changes made . ; Driver Code
using System ; class GFG { static int MAX = 10 ; static int lcs ( int [ , , ] dp , int [ ] arr1 , int n , int [ ] arr2 , int m , int k ) { if ( k < 0 ) return - 10000000 ; if ( n < 0 m < 0 ) return 0 ; int ans = dp [ n , m , k ] ; if ( ans != - 1 ) return ans ; try { ans = Math . Max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , m - 1 , k ) ) ; if ( arr1 [ n - 1 ] == arr2 [ m - 1 ] ) ans = Math . Max ( ans , 1 + lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k ) ) ; ans = Math . Max ( ans , 1 + lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k - 1 ) ) ; } catch ( Exception e ) { } return ans ; } public static void Main ( String [ ] args ) { int k = 1 ; int [ ] arr1 = { 1 , 2 , 3 , 4 , 5 } ; int [ ] arr2 = { 5 , 3 , 1 , 4 , 2 } ; int n = arr1 . Length ; int m = arr2 . Length ; int [ , , ] dp = new int [ MAX , MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) for ( int l = 0 ; l < MAX ; l ++ ) dp [ i , j , l ] = - 1 ; Console . WriteLine ( lcs ( dp , arr1 , n , arr2 , m , k ) ) ; } }
Count ways to build street under given constraints | C # program to count ways to build street under given constraints ; function to count ways of building a street of n rows ; base case ; ways of building houses in both the spots of ith row ; ways of building an office in one of the two spots of ith row ; total ways for n rows ; driver program for checking above function
using System ; public class GFG { static long countWays ( int n ) { long [ , ] dp = new long [ 2 , n + 1 ] ; dp [ 0 , 1 ] = 1 ; dp [ 1 , 1 ] = 2 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ 0 , i ] = dp [ 0 , i - 1 ] + dp [ 1 , i - 1 ] ; dp [ 1 , i ] = dp [ 0 , i - 1 ] * 2 + dp [ 1 , i - 1 ] ; } return dp [ 0 , n ] + dp [ 1 , n ] ; } public static void Main ( ) { int n = 5 ; Console . Write ( " Total ▁ no ▁ of ▁ ways ▁ with ▁ n ▁ = ▁ " + n + " ▁ are : ▁ " + countWays ( n ) ) ; } }
Count ways to build street under given constraints | C # program of above approach ; Program to count ways ; Iterate from 2 to n ; Driver code ; Count Ways
using System ; class GFG { static int countways ( int n ) { int [ ] A = new int [ n + 1 ] ; A [ 0 ] = 1 ; A [ 1 ] = 3 ; A [ 2 ] = 7 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = 2 * A [ i - 1 ] + A [ i - 2 ] ; } return A [ n ] ; } public static void Main ( ) { int n = 5 ; Console . Write ( countways ( n ) ) ; } }
Maximum length subsequence with difference between adjacent elements as either 0 or 1 | C # Code for Maximum length subsequence with difference between adjacent elements as either 0 or 1 ; function to find maximum length subsequence with difference between adjacent elements as either 0 or 1 ; Initialize mls [ ] values for all indexes ; Compute optimized maximum length subsequence values in bottom up manner ; Store maximum of all ' mls ' values in ' max ' ; required maximum length subsequence ; Driver program to test above function
using System ; class GFG { public static int maxLenSub ( int [ ] arr , int n ) { int [ ] mls = new int [ n ] ; int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) mls [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( Math . Abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ] = mls [ j ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( max < mls [ i ] ) max = mls [ i ] ; return max ; } public static void Main ( ) { int [ ] arr = { 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 } ; int n = arr . Length ; Console . Write ( " Maximum ▁ length ▁ subsequence ▁ = ▁ " + maxLenSub ( arr , n ) ) ; } }
Coin game winner where every player has three choices | C # program to find winner of game if player can pick 1 , x , y coins ; To find winner of game ; To store results ; Initial values ; Computing other values . ; If A losses any of i - 1 or i - x or i - y game then he will definitely win game i ; Else A loses game . ; If dp [ n ] is true then A will game otherwise he losses ; Driver program to test findWinner ( ) ;
using System ; public class GFG { static bool findWinner ( int x , int y , int n ) { bool [ ] dp = new bool [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = false ; dp [ 0 ] = false ; dp [ 1 ] = true ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i - 1 >= 0 && dp [ i - 1 ] == false ) dp [ i ] = true ; else if ( i - x >= 0 && dp [ i - x ] == false ) dp [ i ] = true ; else if ( i - y >= 0 && dp [ i - y ] == false ) dp [ i ] = true ; else dp [ i ] = false ; } return dp [ n ] ; } public static void Main ( ) { int x = 3 , y = 4 , n = 5 ; if ( findWinner ( x , y , n ) == true ) Console . WriteLine ( ' A ' ) ; else Console . WriteLine ( ' B ' ) ; } }
Maximum games played by winner | C # program to find maximum number of games played by winner ; method returns maximum games a winner needs to play in N - player tournament ; for 0 games , 1 player is needed for 1 game , 2 players are required ; loop until i - th Fibonacci number is less than or equal to N ; result is ( i - 2 ) because i will be incremented one extra in while loop and we want the last value which is smaller than N , so one more decrement ; Driver code
using System ; class GFG { static int maxGameByWinner ( int N ) { int [ ] dp = new int [ N ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 2 ; int i = 2 ; do { dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ; } while ( dp [ i ++ ] <= N ) ; return ( i - 2 ) ; } public static void Main ( ) { int N = 10 ; Console . Write ( maxGameByWinner ( N ) ) ; } }
Convert to Strictly increasing integer array with minimum changes | C # program to find min elements to change so array is strictly increasing ; To find min elements to remove from array to make it strictly increasing ; Mark all elements of LIS as 1 ; Find LIS of array ; Return min changes for array to strictly increasing ; Driver Code
using System ; class GFG { static int minRemove ( int [ ] arr , int n ) { int [ ] LIS = new int [ n ] ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) LIS [ i ] = Math . Max ( LIS [ i ] , LIS [ j ] + 1 ) ; } len = Math . Max ( len , LIS [ i ] ) ; } return n - len ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 6 , 5 , 4 } ; int n = arr . Length ; Console . WriteLine ( minRemove ( arr , n ) ) ; } }
Ways of transforming one string to other by removing 0 or more characters | C # program to count the distinct transformation of one string to other . ; If b = " " i . e . , an empty string . There is only one way to transform ( remove all characters ) ; Fil dp [ ] [ ] in bottom up manner Traverse all character of b [ ] ; Traverse all characters of a [ ] for b [ i ] ; Filling the first row of the dp matrix . ; Filling other rows . ; Driver code
using System ; class GFG { static int countTransformation ( string a , string b ) { int n = a . Length , m = b . Length ; if ( m == 0 ) return 1 ; int [ , ] dp = new int [ m , n ] ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( i == 0 ) { if ( j == 0 ) dp [ i , j ] = ( a [ j ] == b [ i ] ) ? 1 : 0 ; else if ( a [ j ] == b [ i ] ) dp [ i , j ] = dp [ i , j - 1 ] + 1 ; else dp [ i , j ] = dp [ i , j - 1 ] ; } else { if ( a [ j ] == b [ i ] ) dp [ i , j ] = dp [ i , j - 1 ] + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = dp [ i , j - 1 ] ; } } } return dp [ m - 1 , n - 1 ] ; } static void Main ( ) { string a = " abcccdf " , b = " abccdf " ; Console . Write ( countTransformation ( a , b ) ) ; } }
n | C # code to find nth number with digits 0 , 1 , 2 , 3 , 4 , 5 ; function to convert num to base 6 ; Driver code ; initialize an array to 0 ; function calling to convert number n to base 6 ; if size is zero then return zero
using System ; class GFG { static int max = 100000 ; static int baseconversion ( int [ ] arr , int num , int bas ) { int i = 0 , rem ; if ( num == 0 ) { return 0 ; } while ( num > 0 ) { rem = num % bas ; arr [ i ++ ] = rem ; num /= bas ; } return i ; } public static void Main ( ) { int [ ] arr = new int [ max ] ; int n = 10 ; int size = baseconversion ( arr , n - 1 , 6 ) ; if ( size == 0 ) Console . Write ( size ) ; for ( int i = size - 1 ; i >= 0 ; i -- ) { Console . Write ( arr [ i ] ) ; } } }
Find length of longest subsequence of one string which is substring of another string | C # program to find maximum length of subsequence of a string X such it is substring in another string Y . ; Return the maximum size of substring of X which is substring in Y . ; Initialize the dp [ ] [ ] to 0. ; Calculating value for each element . ; If alphabet of string X and Y are equal make dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; Else copy the previous value in the row i . e dp [ i - 1 ] [ j - 1 ] ; Finding the maximum length . ; Driver Method
using System ; public class GFG { static int MAX = 1000 ; static int maxSubsequenceSubstring ( string x , string y , int n , int m ) { int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i <= m ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( x [ j - 1 ] == y [ i - 1 ] ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = dp [ i , j - 1 ] ; } } int ans = 0 ; for ( int i = 1 ; i <= m ; i ++ ) ans = Math . Max ( ans , dp [ i , n ] ) ; return ans ; } public static void Main ( ) { string x = " ABCD " ; string y = " BACDBDCD " ; int n = x . Length , m = y . Length ; Console . WriteLine ( maxSubsequenceSubstring ( x , y , n , m ) ) ; } }
Pyramid form ( increasing then decreasing ) consecutive array using reduce operations | C # program to find minimum cost for pyramid from given array ; Returns minimum cost to form a pyramid ; Store the maximum possible pyramid height ; Maximum height at start is 1 ; For each position calculate maximum height ; Maximum height at end is 1 ; For each position calculate maximum height ; Find minimum possible among calculated values ; Find maximum height of pyramid ; Calculate cost of this pyramid ; Calculate cost of left half ; Calculate cost of right half ; Driver code
using System ; public class GFG { static int minPyramidCost ( int [ ] arr , int N ) { int [ ] left = new int [ N ] ; int [ ] right = new int [ N ] ; left [ 0 ] = Math . Min ( arr [ 0 ] , 1 ) ; for ( int i = 1 ; i < N ; ++ i ) left [ i ] = Math . Min ( arr [ i ] , Math . Min ( left [ i - 1 ] + 1 , i + 1 ) ) ; right [ N - 1 ] = Math . Min ( arr [ N - 1 ] , 1 ) ; for ( int i = N - 2 ; i >= 0 ; -- i ) right [ i ] = Math . Min ( arr [ i ] , Math . Min ( right [ i + 1 ] + 1 , N - i ) ) ; int [ ] tot = new int [ N ] ; for ( int i = 0 ; i < N ; ++ i ) tot [ i ] = Math . Min ( right [ i ] , left [ i ] ) ; int max_ind = 0 ; for ( int i = 0 ; i < N ; ++ i ) if ( tot [ i ] > tot [ max_ind ] ) max_ind = i ; int cost = 0 ; int height = tot [ max_ind ] ; for ( int x = max_ind ; x >= 0 ; -- x ) { cost += arr [ x ] - height ; if ( height > 0 ) -- height ; } height = tot [ max_ind ] - 1 ; for ( int x = max_ind + 1 ; x < N ; ++ x ) { cost += arr [ x ] - height ; if ( height > 0 ) -- height ; } return cost ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 2 , 1 } ; int N = arr . Length ; Console . WriteLine ( minPyramidCost ( arr , N ) ) ; } }