codeparrot/xlcost-text-to-code huggingface dataset:
Maximum Tip Calculator | C # program for the above approach ; Recursive function to calculate sum of maximum tip order taken by X and Y ; When all orders have been taken ; When X cannot take more orders ; When Y cannot take more orders ; When both can take order calculate maximum out of two ; Driver Code
using System ; class GFG { static int n ; static int solve ( int i , int X , int Y , int [ ] a , int [ ] b , int n ) { if ( i == n ) return 0 ; if ( X <= 0 ) return b [ i ] + solve ( i + 1 , X , Y - 1 , a , b , n ) ; if ( Y <= 0 ) return a [ i ] + solve ( i + 1 , X - 1 , Y , a , b , n ) ; else return Math . Max ( a [ i ] + solve ( i + 1 , X - 1 , Y , a , b , n ) , b [ i ] + solve ( i + 1 , X , Y - 1 , a , b , n ) ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 , 4 , 5 } ; int [ ] b = { 5 , 4 , 3 , 2 , 1 } ; int x = 3 , y = 3 ; Console . Write ( solve ( 0 , x , y , a , b , n ) ) ; } }
Number of even substrings in a string of digits | C # program to count number of substring which are even integer in a string of digits . ; Return the even number substrings . ; If current digit is even , add count of substrings ending with it . The count is ( i + 1 ) ; Driver code
using System ; public class GFG { static int evenNumSubstring ( string str ) { int len = str . Length ; int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int temp = str [ i ] - '0' ; if ( temp % 2 == 0 ) count += ( i + 1 ) ; } return count ; } public static void Main ( ) { string str = "1234" ; Console . Write ( evenNumSubstring ( str ) ) ; } }
Maximum Consecutive Increasing Path Length in Binary Tree | C # Program to find Maximum Consecutive Path Length in a Binary Tree ; To represent a node of a Binary Tree ; Create a new Node and return its address ; Returns the maximum consecutive Path Length ; Get the value of Current Node The value of the current node will be prev Node for its left and right children ; If current node has to be a part of the consecutive path then it should be 1 greater than the value of the previous node ; a ) Find the length of the Left Path b ) Find the length of the Right Path Return the maximum of Left path and Right path ; Find length of the maximum path under subtree rooted with this node ( The path may or may not include this node ) ; Take the maximum previous path and path under subtree rooted with this node . ; A wrapper over maxPathLenUtil ( ) . ; Return 0 if root is NULL ; Else compute Maximum Consecutive Increasing Path Length using maxPathLenUtil . ; Driver code
using System ; class GfG { class Node { public Node left , right ; public int val ; } static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . val = val ; temp . left = null ; temp . right = null ; return temp ; } static int maxPathLenUtil ( Node root , int prev_val , int prev_len ) { if ( root == null ) return prev_len ; int cur_val = root . val ; if ( cur_val == prev_val + 1 ) { return Math . Max ( maxPathLenUtil ( root . left , cur_val , prev_len + 1 ) , maxPathLenUtil ( root . right , cur_val , prev_len + 1 ) ) ; } int newPathLen = Math . Max ( maxPathLenUtil ( root . left , cur_val , 1 ) , maxPathLenUtil ( root . right , cur_val , 1 ) ) ; return Math . Max ( prev_len , newPathLen ) ; } static int maxConsecutivePathLength ( Node root ) { if ( root == null ) return 0 ; return maxPathLenUtil ( root , root . val - 1 , 0 ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 11 ) ; root . right = newNode ( 9 ) ; root . left . left = newNode ( 13 ) ; root . left . right = newNode ( 12 ) ; root . right . left = newNode ( 13 ) ; root . right . right = newNode ( 8 ) ; Console . WriteLine ( " Maximum ▁ Consecutive " + " ▁ Increasing ▁ Path ▁ Length ▁ is ▁ " + maxConsecutivePathLength ( root ) ) ; } }
Find largest word in dictionary by deleting some characters of given string | C # program to find largest word in Dictionary by deleting some characters of given String ; Returns true if str1 [ ] is a subsequence of str2 [ ] . m is length of str1 and n is length of str2 ; Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 , if matched then move ahead in str1 ; If all characters of str1 were found in str2 ; Returns the longest String in dictionary which is a subsequence of str . ; Traverse through all words of dictionary ; If current word is subsequence of str and is largest such word so far . ; Return longest String ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isSubSequence ( String str1 , String str2 ) { int m = str1 . Length , n = str2 . Length ; for ( int i = 0 ; i < n && j < m ; i ++ ) { if ( str1 [ j ] == str2 [ i ] ) { j ++ ; } } return ( j == m ) ; } static String findLongestString ( List < String > dict , String str ) { String result = " " ; int length = 0 ; foreach ( String word in dict ) { if ( length < word . Length && isSubSequence ( word , str ) ) { result = word ; length = word . Length ; } } return result ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ale " , " apple " , " monkey " , " plea " } ; List < String > dict = new List < String > ( arr ) ; String str = " abpcplea " ; Console . WriteLine ( findLongestString ( dict , str ) ) ; } }
Count of distinct substrings of a string using Suffix Trie | C # program to find the count of distinct substring of a string using trie data structure ; A Suffix Trie ( A Trie of all suffixes ) Node ; public SuffixTrieNode ( ) Constructor ; Initialize all child pointers as NULL ; A recursive function to insert a suffix of the s in subtree rooted with this node ; If string has more characters ; Find the first character and convert it into 0 - 25 range . ; If there is no edge for this character , add a new edge ; Recur for next suffix ; A Trie of all suffixes ; Constructor ( Builds a trie of suffies of the given text ) ; Consider all suffixes of given string and insert them into the Suffix Trie using recursive function insertSuffix ( ) in SuffixTrieNode class ; A recursive function to count nodes in trie ; If all characters of pattern have been processed , ; if children is not NULL then find count of all nodes in this subtrie ; return count of nodes of subtrie and plus 1 because of node 's own count ; method to count total nodes in suffix trie ; Returns count of distinct substrings of str ; Construct a Trie of all suffixes ; Return count of nodes in Trie of Suffixes ; Driver program to test above function
using System ; public class Suffix { public class SuffixTrieNode { static readonly int MAX_CHAR = 26 ; public SuffixTrieNode [ ] children = new SuffixTrieNode [ MAX_CHAR ] ; { for ( int i = 0 ; < ; ++ ) children [ i ] = null ; } public void insertSuffix ( String s ) { if ( s . Length > 0 ) { char cIndex = ( char ) ( s [ 0 ] - ' a ' ) ; if ( children [ cIndex ] == null ) children [ cIndex ] = new SuffixTrieNode ( ) ; children [ cIndex ] . insertSuffix ( s . Substring ( 1 ) ) ; } } } public class Suffix_trie { static readonly int MAX_CHAR = 26 ; public SuffixTrieNode root ; public Suffix_trie ( String s ) { root = new SuffixTrieNode ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) root . insertSuffix ( s . Substring ( i ) ) ; } public int _countNodesInTrie ( SuffixTrieNode node ) { if ( node == null ) return 0 ; int count = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( node . children [ i ] != null ) count += _countNodesInTrie ( node . children [ i ] ) ; } return ( 1 + count ) ; } public int countNodesInTrie ( ) { return _countNodesInTrie ( root ) ; } } static int ( ) { = new Suffix_trie ( str ) ; return sTrie . countNodesInTrie ( ) ; } public static void Main ( String [ ] args ) { String str = " ababa " ; Console . WriteLine ( " Count ▁ of ▁ distinct ▁ substrings ▁ is ▁ " + countDistinctSubstring ( str ) ) ; } }
XOR Cipher | C # program to implement XOR - Encryption ; The same function is used to encrypt and decrypt ; Define XOR key Any character value will work ; Define String to store encrypted / decrypted String ; calculate length of input string ; perform XOR operation of key with every character in string ; Driver code ; Encrypt the string ; Decrypt the string
using System ; public class XOREncryption { static String encryptDecrypt ( String inputString ) { char xorKey = ' P ' ; String outputString = " " ; int len = inputString . Length ; for ( int i = 0 ; i < len ; i ++ ) { outputString = outputString + char . ToString ( ( char ) ( inputString [ i ] ^ xorKey ) ) ; } Console . WriteLine ( outputString ) ; return outputString ; } public static void Main ( String [ ] args ) { String sampleString = " GeeksforGeeks " ; Console . WriteLine ( " Encrypted ▁ String " ) ; String encryptedString = encryptDecrypt ( sampleString ) ; Console . WriteLine ( " Decrypted ▁ String " ) ; encryptDecrypt ( encryptedString ) ; } }
Compare two Version numbers | C # program to compare two version number ; Method to compare two versions . Returns 1 if v2 is smaller , - 1 if v1 is smaller , 0 if equal ; vnum stores each numeric part of version ; loop until both string are processed ; storing numeric part of version 1 in vnum1 ; storing numeric part of version 2 in vnum2 ; if equal , reset variables and go for next numeric part ; Driver code
using System ; class GFG { static int versionCompare ( string v1 , string v2 ) { int vnum1 = 0 , vnum2 = 0 ; for ( int i = 0 , j = 0 ; ( i < v1 . Length < v2 . Length ) ; ) { while ( i < v1 . Length && v1 [ i ] != ' . ' ) { vnum1 = vnum1 * 10 + ( v1 [ i ] - '0' ) ; i ++ ; } while ( j < v2 . Length && v2 [ j ] != ' . ' ) { vnum2 = vnum2 * 10 + ( v2 [ j ] - '0' ) ; j ++ ; } if ( vnum1 > vnum2 ) return 1 ; if ( vnum2 > vnum1 ) return - 1 ; vnum1 = vnum2 = 0 ; i ++ ; j ++ ; } return 0 ; } static void Main ( ) { string version1 = "1.0.3" ; string version2 = "1.0.7" ; if ( versionCompare ( version1 , version2 ) < 0 ) Console . WriteLine ( version1 + " ▁ is ▁ smaller " ) ; else if ( versionCompare ( version1 , version2 ) > 0 ) Console . WriteLine ( version2 + " ▁ is ▁ smaller " ) ; else Console . WriteLine ( " Both ▁ version ▁ are ▁ equal " ) ; } }
Repeated subsequence of length 2 or more | C # program to check if any repeated subsequence exists in the string ; A function to check if a string str is palindrome ; l and h are leftmost and rightmost corners of str Keep comparing characters while they are same ; The main function that checks if repeated subsequence exists in the string ; Find length of input string ; Create an array to store all characters and their frequencies in str [ ] ; Traverse the input string and store frequencies of all characters in freq [ ] array . ; If the character count is more than 2 we found a repetition ; In - place remove non - repeating characters from the string ; check if the resultant string is palindrome ; special case - if length is odd return true if the middle character is same as previous one ; It is checked so that StringIndexOutOfBounds can be avoided ; return false if string is a palindrome ; return true if string is not a palindrome ; Driver Code
using System ; class GFG { static int MAX_CHAR = 256 ; public static Boolean isPalindrome ( String str , int l , int h ) { while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ; return true ; } public static Boolean check ( String str ) { int n = str . Length ; int [ ] freq = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str [ i ] ] ++ ; if ( freq [ str [ i ] ] > 2 ) return true ; } int k = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( freq [ str [ i ] ] > 1 ) str . Replace ( str [ k ++ ] , str [ i ] ) ; str . Replace ( str [ k ] , ' \0' ) ; if ( isPalindrome ( str , 0 , k - 1 ) ) { if ( ( k & 1 ) == 1 ) { if ( k / 2 >= 1 ) return ( str [ k / 2 ] == str [ k / 2 - 1 ] ) ; } return false ; } return true ; } public static void Main ( String [ ] args ) { String str = " ABCABD " ; if ( check ( str ) ) Console . WriteLine ( " Repeated ▁ Subsequence ▁ Exists " ) ; else Console . WriteLine ( " Repeated ▁ Subsequence " + " ▁ Doesn ' t ▁ Exists " ) ; } }
How to find Lexicographically previous permutation ? | C # program to print all permutations with duplicates allowed using prev_permutation ( ) ; Function to compute the previous permutation ; Find index of the last element of the string ; Find largest index i such that str [ i - 1 ] > str [ i ] ; if string is sorted in ascending order we 're  at the last permutation ; Note - str [ i . . n ] is sorted in ascending order Find rightmost element 's index  that is less than str[i - 1] ; Swap character at i - 1 with j ; Reverse the substring [ i . . n ] ; Utililty function to swap ; Utility function to reverse ; Driver code
using System ; class GFG { static Boolean prevPermutation ( char [ ] str ) { int n = str . Length - 1 ; int i = n ; while ( i > 0 && str [ i - 1 ] <= str [ i ] ) { i -- ; } if ( i <= 0 ) { return false ; } int j = i - 1 ; while ( j + 1 <= n && str [ j + 1 ] <= str [ i - 1 ] ) { j ++ ; } swap ( str , i - 1 , j ) ; String sb = String . Join ( " " , str ) ; sb = reverse ( sb ) ; str = sb . ToString ( ) . ToCharArray ( ) ; return true ; } static String swap ( char [ ] ch , int i , int j ) { char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . Join ( " " , ch ) ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( String [ ] args ) { char [ ] str = "4321" . ToCharArray ( ) ; if ( prevPermutation ( str ) ) { Console . WriteLine ( " Previous ▁ permutation ▁ is ▁ " + String . Join ( " " , str ) ) ; } else { Console . WriteLine ( " Previous ▁ permutation " + " doesn ' t ▁ exist " ) ; } } }
Longest Path with Same Values in a Binary Tree | C # program to find the length of longest path with same values in a binary tree . ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to print the longest path of same values ; Recursive calls to check for subtrees ; Variables to store maximum lengths in two directions ; If curr node and it 's left child  has same value ; If curr node and it 's right child  has same value ; Function to find length of longest same value path ; Helper function that allocates a new node with the given data and null left and right pointers . ; Driver code ; Let us cona Binary Tree 4 / \ 4 4 / \ \ 4 9 5
using System ; class GFG { static int ans ; public class Node { public int val ; public Node left , right ; } ; static int length ( Node node ) { if ( node == null ) return 0 ; int left = length ( node . left ) ; int right = length ( node . right ) ; int Leftmax = 0 , Rightmax = 0 ; if ( node . left != null && node . left . val == node . val ) Leftmax += left + 1 ; if ( node . right != null && node . right . val == node . val ) Rightmax += right + 1 ; ans = Math . Max ( ans , Leftmax + Rightmax ) ; return Math . Max ( Leftmax , Rightmax ) ; } static int longestSameValuePath ( Node root ) { ans = 0 ; length ( root ) ; return ans ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . val = data ; temp . left = temp . right = null ; return temp ; } public static void Main ( String [ ] args ) { Node root = null ; root = newNode ( 4 ) ; root . left = newNode ( 4 ) ; root . right = newNode ( 4 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 9 ) ; root . right . right = newNode ( 5 ) ; Console . Write ( longestSameValuePath ( root ) ) ; } }
Check if edit distance between two strings is one | C # program to check if given two strings are at distance one . ; Returns true if edit distance between s1 and s2 is one , else false ; Find lengths of given strings ; If difference between lengths is more than 1 , then strings can 't  be at one distance ; Count of edits ; If current characters don 't match ; If length of one string is more , then only possible edit is to remove a character ; If lengths of both strings is same ; Increment count of edits ; If current characters match ; If last character is extra in any string ; Driver code
using System ; class GFG { static bool isEditDistanceOne ( String s1 , String s2 ) { int m = s1 . Length , n = s2 . Length ; if ( Math . Abs ( m - n ) > 1 ) return false ; int count = 0 ; int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( s1 [ i ] != s2 [ j ] ) { if ( count == 1 ) return false ; if ( m > n ) i ++ ; else if ( m < n ) j ++ ; else { i ++ ; j ++ ; } count ++ ; } else { i ++ ; j ++ ; } } if ( i < m < n ) count ++ ; return count == 1 ; } public static void Main ( ) { String s1 = " gfg " ; String s2 = " gf " ; if ( isEditDistanceOne ( s1 , s2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count of numbers from range [ L , R ] whose sum of digits is Y | Set 2 | C # program for the above approach ; Function to find the sum of digits of numbers in the range [ 0 , X ] ; Check if count of digits in a number greater than count of digits in X ; Check if sum of digits of a number is equal to Y ; Check if current subproblem has already been computed ; Stores count of numbers whose sum of digits is Y ; Check if the number exceeds Y or not ; Iterate over all possible values of i - th digits ; Update res ; Return res ; Utility function to count the numbers in the range [ L , R ] whose sum of digits is Y ; Base Case ; Stores numbers in the form of its equivalent String ; Stores overlapping subproblems ; Initialize [ , ] dp [ ] ; Stores count of numbers in the range [ 0 , R ] ; Update str ; Initialize [ , ] dp [ ] ; Stores count of numbers in the range [ 0 , L - 1 ] ; Driver Code
using System ; class GFG { static readonly int M = 1000 ; static int cntNum ( String X , int i , int sum , int tight , int [ , , ] dp ) { if ( i >= X . Length sum < 0 ) { if ( sum == 0 ) { return 1 ; } return 0 ; } if ( dp [ sum , i , tight ] != - 1 ) { return dp [ sum , i , tight ] ; } int res = 0 ; int end = tight != 0 ? X [ i ] - '0' : 9 ; for ( int j = 0 ; j <= end ; j ++ ) { res += cntNum ( X , i + 1 , sum - j , ( tight > 0 & ( j == end ) ) == true ? 1 : 0 , dp ) ; } return dp [ sum ] [ i ] [ tight ] = res ; } static int UtilCntNumRange ( int L , int R , int Y ) { if ( R == 0 && Y == 0 ) { return 1 ; } String str = String . Join ( " " , R ) ; int [ , , ] dp = new int [ M , M , 2 ] ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) dp [ i , j , k ] = - 1 ; } } int cntR = cntNum ( str , 0 , Y , 1 , dp ) ; str = String . Join ( " " , L - 1 ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) dp [ i , j , k ] = - 1 ; } } int cntL = cntNum ( str , 0 , Y , 1 , dp ) ; return ( cntR - cntL ) ; } public static void Main ( String [ ] args ) { int L = 20 , R = 10000 , Y = 14 ; Console . Write ( UtilCntNumRange ( L , R , Y ) ) ; } }
Remove spaces from a given string | C # program to remove all spaces from a string ; Function to remove all spaces from a given string ; Driver Code
using System ; class GFG { static String removeSpace ( String str ) { str = str . Replace ( " ▁ " , " " ) ; return str ; } public static void Main ( ) { String str = " g ▁ eeks ▁ for ▁ ge ▁ eeks ▁ " ; Console . WriteLine ( removeSpace ( str ) ) ; } }
Given a binary string , count number of substrings that start and end with 1. | A simple C # program to count number of substrings starting and ending with 1 ; Initialize result ; Pick a starting point ; Search for all possible ending point ; Driver Code
using System ; class GFG { public virtual int countSubStr ( char [ ] str , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( str [ j ] == '1' ) { res ++ ; } } } } return res ; } public static void Main ( string [ ] args ) { GFG count = new GFG ( ) ; string s = "00100101" ; char [ ] str = s . ToCharArray ( ) ; int n = str . Length ; Console . WriteLine ( count . countSubStr ( str , n ) ) ; } }
An in | C # implementation of the above approach ; A utility function to reverse String str [ low . . high ] ; Cycle leader algorithm to move all even positioned elements at the end . ; odd index ; even index ; Keep the back - up of element at new position ; The main function to transform a String . This function mainly uses cycleLeader ( ) to transform ; Step 1 : Find the largest prefix subarray of the form 3 ^ k + 1 ; Step 2 : Apply cycle leader algorithm for the largest subarrau ; Step 4.1 : Reverse the second half of first subarray ; Step 4.2 : Reverse the first half of second sub - String . ; Step 4.3 Reverse the second half of first sub - String and first half of second sub - String together ; Increase the length of first subarray ; Driver code
using System ; class GFG { static char [ ] str ; static void reverse ( int low , int high ) { while ( low < high ) { char t = str [ low ] ; str [ low ] = str [ high ] ; str [ high ] = t ; ++ low ; -- high ; } } static void cycleLeader ( int shift , int len ) { int j ; char item ; for ( int i = 1 ; i < len ; i *= 3 ) { j = i ; item = str [ j + shift ] ; do { if ( j % 2 == 1 ) j = len / 2 + j / 2 ; else j /= 2 ; char t = str [ j + shift ] ; str [ j + shift ] = item ; item = t ; } while ( j != i ) ; } } static void moveNumberToSecondHalf ( ) { int k , lenFirst ; int lenRemaining = str . Length ; int shift = 0 ; while ( lenRemaining > 0 ) { k = 0 ; while ( Math . Pow ( 3 , k ) + 1 <= lenRemaining ) k ++ ; lenFirst = ( int ) Math . Pow ( 3 , k - 1 ) + 1 ; lenRemaining -= lenFirst ; cycleLeader ( shift , lenFirst ) ; reverse ( shift / 2 , shift - 1 ) ; reverse ( shift , shift + lenFirst / 2 - 1 ) ; reverse ( shift / 2 , shift + lenFirst / 2 - 1 ) ; shift += lenFirst ; } } public static void Main ( String [ ] args ) { String st = " a1b2c3d4e5f6g7" ; str = st . ToCharArray ( ) ; moveNumberToSecondHalf ( ) ; Console . Write ( str ) ; } }