codeparrot/xlcost-text-to-code huggingface dataset:
Populate Inorder Successor for all nodes | C # program to populate inorder traversal of all nodes ; A wrapper over populateNextRecur ; The first visited node will be the rightmost node next of the rightmost node will be NULL ; Set next of all descendants of p by traversing them in reverse Inorder ; First set the next pointer in right subtree ; Set the next as previously visited node in reverse Inorder ; Change the prev for subsequent node ; Finally , set the next pointer in right subtree
using System ; class Node { public int data ; public Node left , right , next ; public Node ( int item ) { data = item ; left = right = next = null ; } } Node root ; static Node next = null ; void populateNext ( Node node ) { populateNextRecur ( node , next ) ; } void populateNextRecur ( Node p , Node next_ref ) { if ( p != null ) { populateNextRecur ( p . right , next_ref ) ; p . next = next_ref ; next_ref = p ; populateNextRecur ( p . left , next_ref ) ; } }
Check if a binary string contains consecutive same or not | C # implementation of the approach ; Function that returns true is str is valid ; Assuming the string is binary If any two consecutive characters are equal then the string is invalid ; If the string is alternating ; Driver code
using System ; class gfg { static bool isValid ( string str , int len ) { for ( int i = 1 ; i < len ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) return false ; } return true ; } public static void Main ( ) { string str = "0110" ; int len = str . Length ; if ( isValid ( str , len ) ) Console . Write ( " Valid " ) ; else Console . Write ( " Invalid " ) ; } }
Minimum K such that every substring of length atleast K contains a character c | C # Program to find minimum K such that every substring of length atleast K contains some character c ; This function checks if there exists some character which appears in all K length substrings ; Iterate over all possible characters ; stores the last occurrence ; set answer as true ; ; No occurrence found of current character in first substring of length K ; Check for every last substring of length K where last occurr - ence exists in substring ; If last occ is not present in substring ; current character is K amazing ; This function performs binary search over the answer to minimise it ; Check if answer is found try to minimise it ; Driver Code to test above functions
using System ; class GFG { static int check ( String s , int K ) { for ( int ch = 0 ; ch < 26 ; ch ++ ) { char c = ( char ) ( ' a ' + ch ) ; int last = - 1 ; bool found = true ; for ( int i = 0 ; i < K ; i ++ ) if ( s [ i ] == c ) last = i ; if ( last == - 1 ) continue ; for ( int i = K ; i < s . Length ; i ++ ) { if ( s [ i ] == c ) last = i ; if ( last <= ( i - K ) ) { found = false ; break ; } } if ( found ) return 1 ; } return 0 ; } static int binarySearch ( String s ) { int low = 1 , high = s . Length ; int ans = 0 ; while ( low <= high ) { int mid = ( high + low ) >> 1 ; if ( check ( s , mid ) == 1 ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void Main ( ) { String s = " abcde " ; Console . WriteLine ( binarySearch ( s ) ) ; s = " aaaa " ; Console . WriteLine ( binarySearch ( s ) ) ; } }
Count the number of vowels occurring in all the substrings of given string | C # implementation of the above approach ; Returns the total sum of occurrences of all vowels ; No . of occurrences of 0 th character in all the substrings ; No . of occurrences of ith character in all the substrings ; Check if ith character is a vowel ; Return the total sum of occurrences of vowels ; Driver Code
using System ; public class Gfg { static int vowel_calc ( string s ) { int n = s . Length ; int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) arr [ i ] = n ; else arr [ i ] = ( n - i ) + arr [ i - 1 ] - i ; } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s [ i ] ; if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) sum += arr [ i ] ; } return sum ; } public static void Main ( ) { string s = " daceh " ; Console . Write ( vowel_calc ( s ) ) ; } }
Iterative Search for a key ' x ' in Binary Tree | An iterative method to search an item in Binary Tree ; A binary tree node has data , left child and right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; iterative process to search an element x in a given binary tree ; Base Case ; Create an empty stack and . Push root to it ; Do iterative preorder traversal to search x ; See the top item from stack and check if it is same as x ; Push right and left children of the . Popped node to stack ; Driver Code
using System ; using System . Collections . Generic ; class GFG { class node { public int data ; public node left , right ; } ; static node newNode ( int data ) { node node = new node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static bool iterativeSearch ( node root , int x ) { if ( root == null ) return false ; Stack < node > nodeStack = new Stack < node > ( ) ; nodeStack . Push ( root ) ; while ( nodeStack . Count != 0 ) { node node = nodeStack . Peek ( ) ; if ( node . data == x ) return true ; nodeStack . Pop ( ) ; if ( node . right != null ) nodeStack . Push ( node . right ) ; if ( node . left != null ) nodeStack . Push ( node . left ) ; } return false ; } public static void Main ( String [ ] args ) { node root = newNode ( 2 ) ; root . left = newNode ( 7 ) ; root . right = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 11 ) ; root . right . right = newNode ( 9 ) ; root . right . right . left = newNode ( 4 ) ; if ( iterativeSearch ( root , 6 ) ) Console . WriteLine ( " Found " ) ; else Console . WriteLine ( " Not ▁ Found " ) ; if ( iterativeSearch ( root , 12 ) ) Console . WriteLine ( " Found " ) ; else Console . WriteLine ( " Not ▁ Found " ) ; } }
Check whether two strings can be made equal by increasing prefixes | C # implementation of above approach ; check whether the first string can be converted to the second string by increasing the ASCII value of prefix string of first string ; length of two strings ; If lengths are not equal ; store the difference of ASCII values ; difference of first element ; traverse through the string ; the ASCII value of the second string should be greater than or equal to first string , if it is violated return false . ; store the difference of ASCII values ; the difference of ASCII values should be in descending order ; if the difference array is not in descending order ; if all the ASCII values of characters of first string is less than or equal to the second string and the difference array is in descending order , return true ; Driver code ; create two strings ; check whether the first string can be converted to the second string
using System ; class GFG { public static bool find ( string s1 , string s2 ) { int len = s1 . Length , len_1 = s2 . Length ; if ( len != len_1 ) return false ; int [ ] d = new int [ len ] ; d [ 0 ] = s2 [ 0 ] - s1 [ 0 ] ; for ( int i = 1 ; i < len ; i ++ ) { if ( s1 [ i ] > s2 [ i ] ) return false ; else { d [ i ] = s2 [ i ] - s1 [ i ] ; } } for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( d [ i ] < d [ i + 1 ] ) return false ; } return true ; } public static void Main ( ) { string s1 = " abcd " , s2 = " bcdd " ; if ( find ( s1 , s2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count all palindrome which is square of a palindrome | C # implementation of the above approach ; check if a number is a palindrome ; utility function used for reversing a string ; Function to return required count of palindromes ; Upper limit ; count odd length palindromes ; if s = '1234' ; then , t = '1234321' ; count even length palindromes ; if s = '1234' ; then , t = '12344321' ; Return count of super - palindromes ; Driver Code ; function call to get required answer
using System ; class GFG { static bool ispalindrome ( int x ) { int ans = 0 ; int temp = x ; while ( temp > 0 ) { ans = 10 * ans + temp % 10 ; temp = temp / 10 ; } return ans == x ; } static string Reverse ( string s ) { char [ ] charArray = s . ToCharArray ( ) ; Array . Reverse ( charArray ) ; return new string ( charArray ) ; } static int SuperPalindromes ( int L , int R ) { int LIMIT = 100000 ; int ans = 0 ; for ( int i = 0 ; i < LIMIT ; i ++ ) { string s = i . ToString ( ) ; string rs = s . Substring ( 0 , Math . Max ( 1 , s . Length - 1 ) ) ; rs = Reverse ( rs ) ; string p = s + rs ; int p_sq = ( int ) ( Math . Pow ( Int32 . Parse ( p ) , 2 ) ) ; if ( p_sq > R ) { break ; } if ( p_sq >= L && ispalindrome ( p_sq ) ) { ans = ans + 1 ; } } for ( int i = 0 ; i < LIMIT ; i ++ ) { string s = i . ToString ( ) ; string rs = Reverse ( s ) ; string p = s + rs ; int p_sq = ( int ) ( Math . Pow ( Int32 . Parse ( p ) , 2 ) ) ; if ( p_sq > R ) { break ; } if ( p_sq >= L && ispalindrome ( p_sq ) ) { ans = ans + 1 ; } } return ans ; } public static void Main ( ) { string L = "4" ; String R = "1000" ; Console . WriteLine ( SuperPalindromes ( Int32 . Parse ( L ) , Int32 . Parse ( R ) ) ) ; } }
N | C # program to find the N - th character in the string "1234567891011 . . " ; Function that returns the N - th character ; initially null string ; starting integer ; add integers in string ; one digit numbers added ; more than 1 digit number , generate equivalent number in a string s1 and concatenate s1 into s . ; add the number in string ; reverse the string ; attach the number ; if the length exceeds N ; Utility reverse function ; Driver Code
using System ; class GFG { static char NthCharacter ( int n ) { String s = " " ; int c = 1 ; for ( int i = 1 ; ; i ++ ) { if ( c < 10 ) s += c . ToString ( ) ; else { String s1 = " " ; int dup = c ; while ( dup > 0 ) { s1 += ( dup % 10 ) . ToString ( ) ; dup /= 10 ; } String temp = reverse ( s1 ) ; s += temp ; } c ++ ; if ( s . Length >= n ) { return s [ n - 1 ] ; } } } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; 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 ) { int n = 11 ; Console . WriteLine ( NthCharacter ( n ) ) ; } }
Sub | C # implementation of above approach ; Function that counts all the sub - strings of length ' k ' which have all identical characters ; count of sub - strings , length , initial position of sliding window ; map to store the frequency of the characters of sub - string ; increase the frequency of the character and length of the sub - string ; if the length of the sub - string is greater than K ; remove the character from the beginning of sub - string ; if the length of the sub string is equal to k and frequency of one of its characters is equal to the length of the sub - string i . e . all the characters are same increase the count ; display the number of valid sub - strings ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void solve ( string s , int k ) { int count = 0 , length = 0 , pos = 0 ; Dictionary < char , int > m = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( m . ContainsKey ( s [ i ] ) ) m [ s [ i ] ] ++ ; else m [ s [ i ] ] = 1 ; length ++ ; if ( length > k ) { m [ s [ pos ] ] -- ; pos ++ ; length -- ; } if ( length == k && m [ s [ i ] ] == length ) count ++ ; } Console . WriteLine ( count ) ; } public static void Main ( ) { string s = " aaaabbbccdddd " ; int k = 4 ; solve ( s , k ) ; } }
Minimum operation require to make first and last character same | C # program to find minimum operation require to make first and last character same ; Function to find minimum operation require to make first and last character same ; Base conditions ; If answer found ; If string is already visited ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Store the minimum value ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int MAX = Int32 . MaxValue ; static Dictionary < string , int > m = new Dictionary < string , int > ( ) ; static int Min = Int32 . MaxValue ; static int minOperation ( string s , int i , int j , int count ) { if ( ( i >= s . Length && j < 0 ) || ( i == j ) ) { return MAX ; } if ( s [ i ] == s [ j ] || ( count >= Min ) ) { return count ; } string str = i . ToString ( ) + " | " + j . ToString ( ) ; if ( ! m . ContainsKey ( str ) ) { if ( i >= s . Length ) { m [ str ] = minOperation ( s , i , j - 1 , count + 1 ) ; } else if ( j < 0 ) { m [ str ] = minOperation ( s , i + 1 , j , count + 1 ) ; } else { m [ str ] = Math . Min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ; } } if ( m [ str ] < Min ) { Min = m [ str ] ; } return m [ str ] ; } static void Main ( ) { string s = " bacdefghipalop " ; int ans = minOperation ( s , 0 , s . Length - 1 , 0 ) ; if ( ans == MAX ) { Console . WriteLine ( - 1 ) ; } else { Console . WriteLine ( ans ) ; } } }
Check if any permutation of a large number is divisible by 8 | C # program to check if any permutation of a large number is divisible by 8 or not ; Function to check if any permutation of a large number is divisible by 8 ; Less than three digit number can be checked directly . ; check for the reverse of a number ; Stores the Frequency of characters in the n . ; Iterates for all three digit numbers divisible by 8 ; stores the frequency of all single digit in three - digit number ; check if the original number has the digit ; when all are checked its not possible ; Utility reverse function ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static bool solve ( String n , int l ) { if ( l < 3 ) { if ( int . Parse ( n ) % 8 == 0 ) return true ; n = reverse ( n ) ; if ( int . Parse ( n ) % 8 == 0 ) return true ; return false ; } int [ ] hash = new int [ 10 ] ; for ( int i = 0 ; i < l ; i ++ ) hash [ n [ i ] - '0' ] ++ ; for ( int i = 104 ; i < 1000 ; i += 8 ) { int dup = i ; int [ ] freq = new int [ 10 ] ; freq [ dup % 10 ] ++ ; dup = dup / 10 ; freq [ dup % 10 ] ++ ; dup = dup / 10 ; freq [ dup % 10 ] ++ ; dup = i ; if ( freq [ dup % 10 ] > hash [ dup % 10 ] ) continue ; dup = dup / 10 ; if ( freq [ dup % 10 ] > hash [ dup % 10 ] ) continue ; dup = dup / 10 ; if ( freq [ dup % 10 ] > hash [ dup % 10 ] ) continue ; return true ; } return false ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; 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 number = "31462708" ; int l = number . Length ; if ( solve ( number , l ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Lexicographically smallest string formed by appending a character from the first K characters of a given string | C # program to find the new string after performing deletions and append operation in the string s ; Function to find the new string thus formed by removing characters ; new string ; Remove characters until the string is empty ; Traverse to find the smallest character in the first k characters ; append the smallest character ; removing the lexicographically smallest character from the string ; Driver code
using System ; class GFG { static String newString ( String s , int k ) { String X = " " ; while ( s . Length > 0 ) { char temp = s [ 0 ] ; for ( int i = 1 ; i < k && i < s . Length ; i ++ ) { if ( s [ i ] < temp ) { temp = s [ i ] ; } } X = X + temp ; for ( int i = 0 ; i < k ; i ++ ) { if ( s [ i ] == temp ) { s = s . Substring ( 0 , i ) + s . Substring ( i + 1 ) ; break ; } } } return X ; } public static void Main ( String [ ] args ) { String s = " gaurang " ; int k = 3 ; Console . Write ( newString ( s , k ) ) ; } }
Given a binary tree , how do you remove all the half nodes ? | C # program to remove half nodes ; Binary tree node ; For inorder traversal ; Removes all nodes with only one child and returns new root ( note that root may change ) ; if current nodes is a half node with left child NULL left , then it 's right child is  returned and replaces it in the given tree  ; if current nodes is a half node with right child NULL right , then it 's right child is  returned and replaces it in the given tree  ; Driver program
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual void printInorder ( Node node ) { if ( node != null ) { printInorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } } public virtual Node RemoveHalfNodes ( Node node ) { if ( node == null ) { return null ; } node . left = RemoveHalfNodes ( node . left ) ; node . right = RemoveHalfNodes ( node . right ) ; if ( node . left == null && node . right == null ) { return node ; } if ( node . left == null ) { Node new_root = node . right ; return new_root ; } if ( node . right == null ) { Node new_root = node . left ; return new_root ; } return node ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; Node NewRoot = null ; tree . root = new Node ( 2 ) ; tree . root . left = new Node ( 7 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . left . right . left = new Node ( 1 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . right = new Node ( 9 ) ; tree . root . right . right . left = new Node ( 4 ) ; Console . WriteLine ( " the ▁ inorder ▁ traversal ▁ of ▁ tree ▁ is ▁ " ) ; tree . printInorder ( tree . root ) ; NewRoot = tree . RemoveHalfNodes ( tree . root ) ; Console . Write ( " STRNEWLINE Inorder ▁ traversal ▁ of ▁ the ▁ modified ▁ tree ▁ STRNEWLINE " ) ; tree . printInorder ( NewRoot ) ; } }
Palindrome by swapping only one character | C # program palindrome by swapping only one character ; convert the string to character array ; counts the number of differences which prevents the string from being palindrome . ; keeps a record of the characters that prevents the string from being palindrome . ; loops from the start of a string till the midpoint of the string ; difference is encountered preventing the string from being palindrome ; 3 rd differences encountered and its no longer possible to make is palindrome by one swap ; record the different character ; store the different characters ; its already palindrome ; only one difference is found ; if the middleChar matches either of the difference producing characters , return true ; two differences are found ; if the characters contained in the two sets are same , return true ; Driver code
using System ; class GFG { public static bool isPalindromePossible ( String input ) { int len = input . Length , i ; char [ ] charStr = input . ToCharArray ( ) ; int diffCount = 0 ; char [ , ] diff = new char [ 2 , 2 ] ; for ( i = 0 ; i < len / 2 ; i ++ ) { if ( charStr [ i ] != charStr [ len - i - 1 ] ) { if ( diffCount == 2 ) return false ; diff [ diffCount , 0 ] = charStr [ i ] ; diff [ diffCount ++ , 1 ] = charStr [ len - i - 1 ] ; } } switch ( diffCount ) { case 0 : return true ; case 1 : char midChar = charStr [ i ] ; if ( len % 2 != 0 && ( diff [ 0 , 0 ] == midChar diff [ 0 , 1 ] == midChar ) ) return true ; break ; case 2 : if ( ( diff [ 0 , 0 ] == diff [ 1 , 0 ] && diff [ 0 , 1 ] == diff [ 1 , 1 ] ) || ( diff [ 0 , 0 ] == diff [ 1 , 1 ] && diff [ 0 , 1 ] == diff [ 1 , 0 ] ) ) return true ; break ; } return false ; } public static void Main ( String [ ] args ) { Console . WriteLine ( isPalindromePossible ( " bbg " ) ) ; Console . WriteLine ( isPalindromePossible ( " bdababd " ) ) ; Console . WriteLine ( isPalindromePossible ( " gcagac " ) ) ; } }
Convert String into Binary Sequence | C # program to convert string into binary string ; utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; Utility Reverse function ; Driver code
using System ; public class Node { static void strToBinary ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { int val = s [ i ] ; String bin = " " ; while ( val > 0 ) { if ( val % 2 == 1 ) { bin += '1' ; } else bin += '0' ; val /= 2 ; } bin = reverse ( bin ) ; Console . Write ( bin + " ▁ " ) ; } } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; 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 = " geeks " ; strToBinary ( s ) ; } }
Sparse Search | C # program to implement binary search in a sparse array . ; Binary Search in an array with blanks ; Modified Part ; Search for both side for a non empty string ; No non - empty string on both sides ; Normal Binary Search ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int binarySearch ( string [ ] arr , int low , int high , string x ) { if ( low > high ) return - 1 ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] == " " ) { int left = mid - 1 ; int right = mid + 1 ; while ( true ) { if ( left < low && right > high ) return - 1 ; if ( left >= low && arr [ left ] != " " ) { mid = left ; break ; } else if ( right <= high && arr [ right ] != " " ) { mid = right ; break ; } left -- ; right ++ ; } } if ( arr [ mid ] == x ) return mid ; else if ( x . CompareTo ( arr [ mid ] ) < 0 ) return binarySearch ( arr , low , mid - 1 , x ) ; else return binarySearch ( arr , mid + 1 , high , x ) ; } static int sparseSearch ( string [ ] arr , string x , int n ) { return binarySearch ( arr , 0 , n - 1 , x ) ; } public static void Main ( string [ ] args ) { string [ ] arr = { " for " , " geeks " , " " , " " , " " , " " , " ide " , " practice " , " " , " " , " " , " quiz " } ; string x = " geeks " ; int n = x . Length ; int index = sparseSearch ( arr , x , n ) ; if ( index != - 1 ) Console . Write ( x + " ▁ found ▁ at ▁ index ▁ " + index ) ; else Console . Write ( x + " ▁ not ▁ found " ) ; } }