codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum steps required to reach the end of a matrix | Set 2 | C # implementation of the approach ; 2d array to store states of dp ; Array to determine whether a state has been solved before ; Function to return the minimum steps required ; Base cases ; If a state has been solved before it won 't be evaluated again ; Recurrence relation ; Driver code
using System ; class GFG { static int n = 3 ; static int [ , ] dp = new int [ n , n ] ; static int [ , ] v = new int [ n , n ] ; static int minSteps ( int i , int j , int [ , ] arr ) { if ( i == n - 1 && j == n - 1 ) { return 0 ; } if ( i > n - 1 j > n - 1 ) { return 9999999 ; } if ( v [ i , j ] == 1 ) { return dp [ i , j ] ; } v [ i , j ] = 1 ; dp [ i , j ] = 9999999 ; for ( int k = Math . Max ( 0 , arr [ i , j ] + j - n + 1 ) ; k <= Math . Min ( n - i - 1 , arr [ i , j ] ) ; k ++ ) { dp [ i , j ] = Math . Min ( dp [ i , j ] , minSteps ( i + k , j + arr [ i , j ] - k , arr ) ) ; } dp [ i , j ] ++ ; return dp [ i , j ] ; } static public void Main ( ) { int [ , ] arr = { { 4 , 1 , 2 } , { 1 , 1 , 1 } , { 2 , 1 , 1 } } ; int ans = minSteps ( 0 , 0 , arr ) ; if ( ans >= 9999999 ) { Console . WriteLine ( - 1 ) ; } else { Console . WriteLine ( ans ) ; } } }
Find minimum steps required to reach the end of a matrix | Set | C # program to implement above approach ; 2d array to store states of dp ; array to determine whether a state has been solved before ; Function to find the minimum number of steps to reach the end of matrix ; base cases ; if a state has been solved before it won 't be evaluated again. ; recurrence relation ; Driver Code
using System ; class GFG { static int n = 3 ; static int [ , ] dp = new int [ n , n ] ; static int [ , ] v = new int [ n , n ] ; static int minSteps ( int i , int j , int [ , ] arr ) { if ( i == n - 1 && j == n - 1 ) { return 0 ; } if ( i > n - 1 j > n - 1 ) { return 9999999 ; } if ( v [ i , j ] == 1 ) { return dp [ i , j ] ; } v [ i , j ] = 1 ; dp [ i , j ] = 1 + Math . Min ( minSteps ( i + arr [ i , j ] , j , arr ) , minSteps ( i , j + arr [ i , j ] , arr ) ) ; return dp [ i , j ] ; } static public void Main ( ) { int [ , ] arr = { { 2 , 1 , 2 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int ans = minSteps ( 0 , 0 , arr ) ; if ( ans >= 9999999 ) { Console . WriteLine ( - 1 ) ; } else { Console . WriteLine ( ans ) ; } } }
Treasure and Cities | A memoization based program to find maximum treasure that can be collected . ; k is current index and col is previous color . ; if ( k == n ) base case ; check if color of this city is equal to prev visited city ; return max of both options ; Driver code
using System ; class GFG { static int MAX = 1001 ; static int [ , ] dp = new int [ MAX , MAX ] ; static int MaxProfit ( int [ ] treasure , int [ ] color , int n , int k , int col , int A , int B ) { return dp [ k , col ] = 0 ; if ( dp [ k , col ] != - 1 ) return dp [ k , col ] ; int sum = 0 ; if ( col == color [ k ] ) sum += Math . Max ( A * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) ) ; else sum += Math . Max ( B * treasure [ k ] + MaxProfit ( treasure , color , n , k + 1 , color [ k ] , A , B ) , MaxProfit ( treasure , color , n , k + 1 , col , A , B ) ) ; return dp [ k , col ] = sum ; } public static void Main ( String [ ] args ) { int A = - 5 , B = 7 ; int [ ] treasure = { 4 , 8 , 2 , 9 } ; int [ ] color = { 2 , 2 , 6 , 2 } ; int n = color . Length ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = - 1 ; Console . Write ( MaxProfit ( treasure , color , n , 0 , 0 , A , B ) ) ; } }
Tetranacci Numbers | A DP based C # program to print the nth tetranacci number ; Function to print the N - th tetranacci number ; base cases ; Driver code
class GFG { static void printTetra ( int n ) { int [ ] dp = new int [ n + 5 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = dp [ 2 ] = 1 ; dp [ 3 ] = 2 ; for ( int i = 4 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] + dp [ i - 4 ] ; System . Console . WriteLine ( dp [ n ] ) ; } static void Main ( ) { int n = 10 ; printTetra ( n ) ; } }
Maximum sum in circular array such that no two elements are adjacent | C # program to find maximum sum in a circular array such that no elements are adjacent in the sum . ; Function to calculate the sum from 0 th position to ( n - 2 ) th position ; copy the element of original array to dp [ ] ; find the maximum element in the array ; start from 2 nd to n - 1 th pos ; traverse for all pairs bottom - up approach ; dp - condition ; find maximum sum ; return the maximum ; Function to find the maximum sum from 1 st position to n - 1 - th position ; Traverse from third to n - th pos ; bootom - up approach ; dp condition ; find max sum ; return max ; Driver Code
using System ; class GFG { static int maxSum1 ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; int maxi = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { dp [ i ] = arr [ i ] ; if ( maxi < arr [ i ] ) maxi = arr [ i ] ; } for ( int i = 2 ; i < n - 1 ; i ++ ) { for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( dp [ i ] < dp [ j ] + arr [ i ] ) { dp [ i ] = dp [ j ] + arr [ i ] ; if ( maxi < dp [ i ] ) maxi = dp [ i ] ; } } } return maxi ; } static int maxSum2 ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; int maxi = 0 ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] ; if ( maxi < arr [ i ] ) maxi = arr [ i ] ; } for ( int i = 3 ; i < n ; i ++ ) { for ( int j = 1 ; j < i - 1 ; j ++ ) { if ( dp [ i ] < arr [ i ] + dp [ j ] ) { dp [ i ] = arr [ i ] + dp [ j ] ; if ( maxi < dp [ i ] ) maxi = dp [ i ] ; } } } return maxi ; } static int findMaxSum ( int [ ] arr , int n ) { int t = Math . Max ( maxSum1 ( arr , n ) , maxSum2 ( arr , n ) ) ; return t ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 } ; int n = arr . Length ; Console . WriteLine ( findMaxSum ( arr , n ) ) ; } }
Permutation Coefficient | C # code for Dynamic Programming based solution that uses table P [ ] [ ] to calculate the Permutation Coefficient ; Returns value of Permutation Coefficient P ( n , k ) ; Calculate value of Permutation Coefficient in bottom up manner ; Base Cases ; Calculate value using previosly stored values ; This step is important as P ( i , j ) = 0 for j > i ; Driver Code
using System ; class GFG { static int permutationCoeff ( int n , int k ) { int [ , ] P = new int [ n + 2 , k + 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 ) P [ i , j ] = 1 ; else P [ i , j ] = P [ i - 1 , j ] + ( j * P [ i - 1 , j - 1 ] ) ; P [ i , j + 1 ] = 0 ; } } return P [ n , k ] ; } public static void Main ( ) { int n = 10 , k = 2 ; Console . WriteLine ( " Value ▁ of ▁ P ( ▁ " + n + " , " + k + " ) " + " ▁ is ▁ " + permutationCoeff ( n , k ) ) ; } }
Permutation Coefficient | A O ( n ) solution that uses table fact [ ] to calculate the Permutation Coefficient ; Returns value of Permutation Coefficient P ( n , k ) ; base case ; Calculate value factorials up to n ; P ( n , k ) = n ! / ( n - k ) ! ; Driver Code
using System ; public class GFG { static int permutationCoeff ( int n , int k ) { int [ ] fact = new int [ n + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; return fact [ n ] / fact [ n - k ] ; } static public void Main ( ) { int n = 10 , k = 2 ; Console . WriteLine ( " Value ▁ of " + " ▁ P ( ▁ " + n + " , ▁ " + k + " ) ▁ is ▁ " + permutationCoeff ( n , k ) ) ; } }
Dynamic Programming | A recursive solution for subset sum problem ; Returns true if there is a subset of set [ ] with sum equal to given sum ; Base Cases ; If last element is greater than sum , then ignore it ; else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element ; Driver code
using System ; class GFG { static bool isSubsetSum ( int [ ] set , int n , int sum ) { if ( sum == 0 ) return true ; if ( n == 0 ) return false ; if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ; return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ; } public static void Main ( ) { int [ ] set = { 3 , 34 , 4 , 12 , 5 , 2 } ; int sum = 9 ; int n = set . Length ; if ( isSubsetSum ( set , n , sum ) == true ) Console . WriteLine ( " Found ▁ a ▁ subset ▁ with ▁ given ▁ sum " ) ; else Console . WriteLine ( " No ▁ subset ▁ with ▁ given ▁ sum " ) ; } }
Count of cyclic permutations having XOR with other binary string as 0 | C # program to find bitwise XOR between binary string A and all the cyclic permutations of binary string B ; Implementation of Z - algorithm for linear time pattern searching ; Function to get the count of the cyclic permutations of b that given 0 when XORed with a ; concatenate b with b ; new b now contains all the cyclic permutations of old b as it 's sub-strings ; concatenate pattern with text ; Fill z array used in Z algorithm ; pattern occurs at index i since z value of i equals pattern length ; Driver Code
using System ; class GFG { public static void compute_z ( string s , int [ ] z ) { int l = 0 , r = 0 ; int n = s . Length ; for ( int i = 1 ; i <= n - 1 ; i ++ ) { if ( i > r ) { l = i ; r = i ; while ( r < n && s [ r - l ] == s [ r ] ) { r ++ ; } z [ i ] = r - l ; r -- ; } else { int k = i - l ; if ( z [ k ] < r - i + 1 ) { z [ i ] = z [ k ] ; } else { l = i ; while ( r < n && s [ r - l ] == s [ r ] ) { r ++ ; } z [ i ] = r - l ; r -- ; } } } } public static int countPermutation ( string a , string b ) { b = b + b ; b = b . Substring ( 0 , b . Length - 1 ) ; int ans = 0 ; string s = a + " $ " + b ; int n = s . Length ; int [ ] z = new int [ n ] ; compute_z ( s , z ) ; for ( int i = 1 ; i <= n - 1 ; i ++ ) { if ( z [ i ] == a . Length ) { ans ++ ; } } return ans ; } public static void Main ( string [ ] args ) { string a = "101" ; string b = "101" ; Console . WriteLine ( countPermutation ( a , b ) ) ; } }
Lexicographically smallest K | C # program for the above approach ; Function to find lexicographically smallest subsequence of size K ; Length of String ; Stores the minimum subsequence ; Traverse the String S ; If the stack is empty ; Iterate till the current character is less than the the character at the top of stack ; Check if there are enough characters remaining to obtain length K ; If stack size is < K ; Push the current character into it ; Stores the resultant String ; Iterate until stack is empty ; Reverse the String ; Print the String ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static void smallestSubsequence ( char [ ] S , int K ) { int N = S . Length ; Stack < char > answer = new Stack < char > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( answer . Count == 0 ) { answer . Push ( S [ i ] ) ; } else { while ( ( answer . Count != 0 ) && ( S [ i ] < answer . Peek ( ) ) && ( answer . Count - 1 + N - i >= K ) ) { answer . Pop ( ) ; } if ( answer . Count == 0 answer . Count < K ) { answer . Push ( S [ i ] ) ; } } } String ret = " " ; while ( answer . Count != 0 ) { ret += ( answer . Peek ( ) ) ; answer . Pop ( ) ; } ret = reverse ( ret ) ; Console . Write ( ret ) ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String S = " aabdaabc " ; int K = 3 ; smallestSubsequence ( S . ToCharArray ( ) , K ) ; } }
Check if string is right to left diagonal or not | C # program to check if the given string is right to left diagonal or not ; Function to check if the given string is right to left diagonal or not ; Iterate over string ; If character is not same as the first character then return false ; Driver Code ; Given String str ; Function call
using System ; class GFG { public static bool is_rtol ( String s ) { int tmp = ( int ) ( Math . Sqrt ( s . Length ) ) - 1 ; char first = s [ tmp ] ; for ( int pos = tmp ; pos < s . Length - 1 ; pos += tmp ) { if ( s [ pos ] != first ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { String str = " abcxabxcaxbcxabc " ; if ( is_rtol ( str ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Split a given string into substrings of length K with equal sum of ASCII values | C # program to check if a given string can be split into substrings of size K having an equal sum of ASCII values . ; Function for checking string ; Check if the string can be split into substrings of K length only ; Compute the sum of first substring of length K ; Compute the sum of remaining substrings ; Check if sum is equal to that of the first substring ; Since all sums are not equal , return false ; All sums are equal , Return true ; All substrings cannot be of size K ; Driver code
using System ; class GFG { static bool check ( string str , int K ) { if ( str . Length % K == 0 ) { int sum = 0 , i ; for ( i = 0 ; i < K ; i ++ ) { sum += str [ i ] ; } for ( int j = i ; j < str . Length ; j += K ) { int s_comp = 0 ; for ( int p = j ; p < j + K ; p ++ ) s_comp += str [ p ] ; if ( s_comp != sum ) return false ; } return true ; } return false ; } public static void Main ( string [ ] args ) { int K = 3 ; string str = " abdcbbdba " ; if ( check ( str , K ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Maximize count of 0 s in left and 1 s in right substring by splitting given Binary string | C # program for the above approach ; Function to maximize the sum of the count of zeros and ones in the left and right substring ; To store the total ones ; Count the total numbers of ones in string str ; To store the count of zeros and ones while traversing string ; Iterate the given string and update the maximum sum ; Update the maximum Sum ; Driver Code ; Given binary string ; Function call
using System ; class GFG { static int maxSum ( string str ) { int maximumSum = 0 ; int totalOnes = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '1' ) { totalOnes ++ ; } } int zero = 0 , ones = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '0' ) { zero ++ ; } else { ones ++ ; } maximumSum = Math . Max ( maximumSum , zero + ( totalOnes - ones ) ) ; } return maximumSum ; } public static void Main ( string [ ] args ) { string str = "011101" ; Console . Write ( maxSum ( str ) ) ; } }
Longest substring such that no three consecutive characters are same | C # implementation of the approach ; Function to return the length of the longest substring such that no three consecutive characters are same ; If the length of the given string is less than 3 ; Initialize temporary and final ans to 2 as this is the minimum length of substring when length of the given string is greater than 2 ; Traverse the string from the third character to the last ; If no three consecutive characters are same then increment temporary count ; Else update the final ans and reset the temporary count ; Driver code
using System ; class GFG { static int maxLenSubStr ( String s ) { if ( s . Length < 3 ) return s . Length ; int temp = 2 ; int ans = 2 ; for ( int i = 2 ; i < s . Length ; i ++ ) { if ( s [ i ] != s [ i - 1 ] s [ i ] != s [ i - 2 ] ) temp ++ ; else { ans = Math . Max ( temp , ans ) ; temp = 2 ; } } ans = Math . Max ( temp , ans ) ; return ans ; } static public void Main ( ) { String s = " baaabbabbb " ; Console . Write ( maxLenSubStr ( s ) ) ; } }
Number of ways to remove a sub | C # program to count number of ways of removing a substring from a string such that all remaining characters are equal ; Function to return the number of ways of removing a sub - string from s such that all the remaining characters are same ; To store the count of prefix and suffix ; Loop to count prefix ; Loop to count suffix ; First and last characters of the string are same ; Otherwise ; Driver Code
using System ; class GFG { static int no_of_ways ( string s ) { int n = s . Length ; int count_left = 0 , count_right = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s [ i ] == s [ 0 ] ) { ++ count_left ; } else break ; } for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == s [ n - 1 ] ) { ++ count_right ; } else break ; } if ( s [ 0 ] == s [ n - 1 ] ) return ( ( count_left + 1 ) * ( count_right + 1 ) ) ; else return ( count_left + count_right + 1 ) ; } public static void Main ( ) { string s = " geeksforgeeks " ; Console . WriteLine ( no_of_ways ( s ) ) ; } }
Count number of indices such that s [ i ] = s [ i + 1 ] : Range queries | C # program to find substring with ; Function to create prefix array ; Function to return the result of the query ; Driver Code ; Query 1 ; Query 2
using System ; class GFG { static void preCompute ( int n , string s , int [ ] pref ) { pref [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { pref [ i ] = pref [ i - 1 ] ; if ( s [ i - 1 ] == s [ i ] ) pref [ i ] ++ ; } } static int query ( int [ ] pref , int l , int r ) { return pref [ r ] - pref [ l ] ; } public static void Main ( ) { string s = " ggggggg " ; int n = s . Length ; int [ ] pref = new int [ n ] ; preCompute ( n , s , pref ) ; int l = 1 ; int r = 2 ; Console . WriteLine ( query ( pref , l , r ) ) ; l = 1 ; r = 5 ; Console . WriteLine ( query ( pref , l , r ) ) ; } }
Find the direction from given string | C # implementation of above approach ; Function to find the final direction ; if count is positive that implies resultant is clockwise direction ; if count is negative that implies resultant is anti - clockwise direction ; Driver code
using System ; class GFG { static String findDirection ( String s ) { int count = 0 ; String d = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ 0 ] == ' STRNEWLINE ' ) return null ; if ( s [ i ] == ' L ' ) count -- ; else { if ( s [ i ] == ' R ' ) count ++ ; } } if ( count > 0 ) { if ( count % 4 == 0 ) d = " N " ; else if ( count % 4 == 1 ) d = " E " ; else if ( count % 4 == 2 ) d = " S " ; else if ( count % 4 == 3 ) d = " W " ; } if ( count < 0 ) { if ( count % 4 == 0 ) d = " N " ; else if ( count % 4 == - 1 ) d = " W " ; else if ( count % 4 == - 2 ) d = " S " ; else if ( count % 4 == - 3 ) d = " E " ; } return d ; } public static void Main ( ) { String s = " LLRLRRL " ; Console . WriteLine ( findDirection ( s ) ) ; s = " LL " ; Console . WriteLine ( findDirection ( s ) ) ; } }
Check if lowercase and uppercase characters are in same order | C # program to check if lowercase and uppercase characters are in same order ; Function to check if both the case follow the same order ; Traverse the string ; Store both lowercase and uppercase in two different strings ; transform lowerStr1 to upper ; Driver code
using System ; class GFG { static bool isCheck ( string str ) { int len = str . Length ; string lowerStr = " " , upperStr = " " ; char [ ] str1 = str . ToCharArray ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ( int ) ( str1 [ i ] ) >= 65 && ( int ) str1 [ i ] <= 91 ) upperStr = upperStr + str1 [ i ] ; else lowerStr = lowerStr + str1 [ i ] ; } String transformStr = lowerStr . ToUpper ( ) ; return ( transformStr . Equals ( upperStr ) ) ; } public static void Main ( String [ ] args ) { String str = " geeGkEEsKS " ; if ( isCheck ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Replace every character of string by character whose ASCII value is K times more than it | C # program to move every character K times ahead in a given string ; Function to move string character ; changed string ; iterate for every characters ; ASCII value ; store the duplicate ; if k - th ahead character exceed ' z ' ; print the new string ; Driver Code ; function call
using System ; public class GFG { static void encode ( String s , int k ) { String newS = " " ; for ( int i = 0 ; i < s . Length ; ++ i ) { int val = s [ i ] ; int dup = k ; if ( val + k > 122 ) { k -= ( 122 - val ) ; k = k % 26 ; newS += ( char ) ( 96 + k ) ; } else { newS += ( char ) ( 96 + k ) ; } k = dup ; } Console . Write ( newS ) ; } public static void Main ( ) { String str = " abc " ; int k = 28 ; encode ( str , k ) ; } }
Modify string by removing vowels in between two consonants | C # program to remove all Vowels in between two consonants from the string ; Function to check if the character x is a vowel or not ; Returns the updated string formed after removing all the Sandwiched Vowels from the given string ; string to store the Updated String after removing the Sandwiched Vowels ; traverse the string from left to right ; if the current character is the first or the last character of the string then , this needs to be appended to the updatedString , since the corner alphabet irrespective of it being a vowel or a consonant , is never ' Sandwiched ' ; Check if the current character of the string is a vowel and both the previous and the next characters are consonants , if so then this is a sandwiched vowel , thus is ignored and not appended to the updated string ; if this character is not a sandwiched Vowel append it to the updated String ; Driver Code ; Remove all the Sandwitched Vowels
using System ; class GFG { static bool isVowel ( char x ) { if ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) return true ; else return false ; } static String updateSandwichedVowels ( String a ) { int n = a . Length ; String updatedString = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 i == n - 1 ) { updatedString += a [ i ] ; continue ; } if ( ( isVowel ( a [ i ] ) ) == true && isVowel ( a [ i - 1 ] ) == false && isVowel ( a [ i + 1 ] ) == false ) { continue ; } updatedString += a [ i ] ; } return updatedString ; } public static void Main ( ) { String str = " geeksforgeeks " ; String updatedString = updateSandwichedVowels ( str ) ; Console . WriteLine ( updatedString ) ; }