codeparrot/xlcost-text-to-code huggingface dataset:
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
import java . util . * ; class GFG { static class node { int data ; node left , right ; } ; static node newNode ( int data ) { node node = new node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static boolean iterativeSearch ( node root , int x ) { if ( root == null ) return false ; Stack < node > nodeStack = new Stack < node > ( ) ; nodeStack . push ( root ) ; while ( nodeStack . empty ( ) == false ) { 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 NewRoot = null ; 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 ) ) System . out . println ( " Found " ) ; else System . out . println ( " Not ▁ Found " ) ; if ( iterativeSearch ( root , 12 ) ) System . out . println ( " Found " ) ; else System . out . println ( " Not ▁ Found " ) ; } }
Check whether two strings can be made equal by increasing prefixes | Java 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
class GFG { static boolean 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 . charAt ( 0 ) - s1 . charAt ( 0 ) ; for ( int i = 1 ; i < len ; i ++ ) { if ( s1 . charAt ( i ) > s2 . charAt ( i ) ) { return false ; } else { d [ i ] = s2 . charAt ( i ) - s1 . charAt ( i ) ; } } for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( d [ i ] < d [ i + 1 ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String s1 = " abcd " , s2 = " bcdd " ; if ( find ( s1 , s2 ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Count all palindrome which is square of a palindrome | Java implementation of the above approach ; check if a number is a palindrome ; 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 program ; function call to get required answer
import java . lang . * ; class GFG { public static boolean ispalindrome ( int x ) { int ans = 0 ; int temp = x ; while ( temp > 0 ) { ans = 10 * ans + temp % 10 ; temp = temp / 10 ; } return ans == x ; } public static int SuperPalindromes ( int L , int R ) { int LIMIT = 100000 ; int ans = 0 ; for ( int i = 0 ; i < LIMIT ; i ++ ) { String s = Integer . toString ( i ) ; StringBuilder rs = new StringBuilder ( ) ; rs . append ( s . substring ( 0 , Math . max ( 1 , s . length ( ) - 1 ) ) ) ; String srs = rs . reverse ( ) . toString ( ) ; String p = s + srs ; int p_sq = ( int ) ( Math . pow ( Integer . parseInt ( 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 = Integer . toString ( i ) ; StringBuilder rs = new StringBuilder ( ) ; rs . append ( s ) ; rs = rs . reverse ( ) ; String p = s + rs ; int p_sq = ( int ) ( Math . pow ( Integer . parseInt ( p ) , 2 ) ) ; if ( p_sq > R ) { break ; } if ( p_sq >= L && ispalindrome ( p_sq ) ) { ans = ans + 1 ; } } return ans ; } public static void main ( String [ ] args ) { String L = "4" ; String R = "1000" ; System . out . println ( SuperPalindromes ( Integer . parseInt ( L ) , Integer . parseInt ( R ) ) ) ; } }
N | Java 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 ; Driver Code
class GFG { static char NthCharacter ( int n ) { String s = " " ; int c = 1 ; for ( int i = 1 ; ; i ++ ) { if ( c < 10 ) s += Integer . toString ( c ) ; else { String s1 = " " ; int dup = c ; while ( dup > 0 ) { s1 += Integer . toString ( dup % 10 ) ; dup /= 10 ; } StringBuilder temp = new StringBuilder ( ) ; temp . append ( s1 ) ; temp = temp . reverse ( ) ; s += temp ; } c ++ ; if ( s . length ( ) >= n ) { return s . charAt ( n - 1 ) ; } } } public static void main ( String [ ] args ) { int n = 11 ; System . out . println ( NthCharacter ( n ) ) ; } }
Sub | Java 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
import java . util . * ; class GFG { static void solve ( String s , int k ) { int count = 0 , length = 0 , pos = 0 ; HashMap < Character , Integer > m = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( m . containsKey ( s . charAt ( i ) ) ) m . put ( s . charAt ( i ) , m . get ( s . charAt ( i ) ) + 1 ) ; else m . put ( s . charAt ( i ) , 1 ) ; length ++ ; if ( length > k ) { m . put ( s . charAt ( pos ) , m . get ( s . charAt ( pos ) ) - 1 ) ; pos ++ ; length -- ; } if ( length == k && m . get ( s . charAt ( i ) ) == length ) count ++ ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String s = " aaaabbbccdddd " ; int k = 4 ; solve ( s , k ) ; } }
Minimum operation require to make first and last character same | Java 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
import java . io . * ; import java . util . * ; class GFG { static int MAX = Integer . MAX_VALUE ; static HashMap < String , Integer > m = new HashMap < > ( ) ; static int Min = Integer . MAX_VALUE ; static int minOperation ( String s , int i , int j , int count ) { if ( ( i >= s . length ( ) && j < 0 ) || ( i == j ) ) { return MAX ; } if ( s . charAt ( i ) == s . charAt ( j ) || ( count >= Min ) ) { return count ; } String str = String . valueOf ( i ) + " | " + String . valueOf ( j ) ; if ( ! m . containsKey ( str ) ) { if ( i >= s . length ( ) ) { m . put ( str , minOperation ( s , i , j - 1 , count + 1 ) ) ; } else if ( j < 0 ) { m . put ( str , minOperation ( s , i + 1 , j , count + 1 ) ) ; } else { m . put ( str , Math . min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ) ; } } if ( m . get ( str ) < Min ) { Min = m . get ( str ) ; } return m . get ( str ) ; } public static void main ( String [ ] args ) { String s = " bacdefghipalop " ; int ans = minOperation ( s , 0 , s . length ( ) - 1 , 0 ) ; if ( ans == MAX ) { System . out . println ( - 1 ) ; } else { System . out . println ( ans ) ; } } }
Check if any permutation of a large number is divisible by 8 | Java 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 ; Driver Code
import java . util . * ; class GFG { public static boolean solve ( String n , int l ) { if ( l < 3 ) { if ( Integer . parseInt ( n ) % 8 == 0 ) return true ; n = new String ( ( new StringBuilder ( ) ) . append ( n ) . reverse ( ) ) ; if ( Integer . parseInt ( n ) % 8 == 0 ) return true ; return false ; } int [ ] hash = new int [ 10 ] ; for ( int i = 0 ; i < l ; i ++ ) hash [ n . charAt ( 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 ; } public static void main ( String [ ] args ) { String number = "31462708" ; int l = number . length ( ) ; if ( solve ( number , l ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Lexicographically smallest string formed by appending a character from the first K characters of a given string | Java 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
class GFG { static String newString ( String s , int k ) { String X = " " ; while ( s . length ( ) > 0 ) { char temp = s . charAt ( 0 ) ; for ( int i = 1 ; i < k && i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) < temp ) { temp = s . charAt ( i ) ; } } X = X + temp ; for ( int i = 0 ; i < k ; i ++ ) { if ( s . charAt ( 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 ; System . out . println ( newString ( s , k ) ) ; } }
Given a binary tree , how do you remove all the 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
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printInorder ( Node node ) { if ( node != null ) { printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } } 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 ) ; System . out . println ( " the ▁ inorder ▁ traversal ▁ of ▁ tree ▁ is ▁ " ) ; tree . printInorder ( tree . root ) ; NewRoot = tree . RemoveHalfNodes ( tree . root ) ; System . out . print ( " Inorder traversal of the modified tree "); tree . printInorder ( NewRoot ) ; } }
Palindrome by swapping only one character | Java 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
class GFG { public static boolean 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 ; 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 ; } return false ; } public static void main ( String [ ] args ) { System . out . println ( isPalindromePossible ( " bbg " ) ) ; System . out . println ( isPalindromePossible ( " bdababd " ) ) ; System . out . println ( isPalindromePossible ( " gcagac " ) ) ; } }
Convert String into Binary Sequence | Java 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
import java . util . * ; class Node { static void strToBinary ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int val = Integer . valueOf ( s . charAt ( i ) ) ; String bin = " " ; while ( val > 0 ) { if ( val % 2 == 1 ) { bin += '1' ; } else bin += '0' ; val /= 2 ; } bin = reverse ( bin ) ; System . out . print ( 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 . valueOf ( a ) ; } public static void main ( String [ ] args ) { String s = " geeks " ; strToBinary ( s ) ; } }
Sparse Search | Java 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
class solution { 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 ) System . out . println ( x + " ▁ found ▁ at ▁ index ▁ " + index ) ; else System . out . println ( x + " ▁ not ▁ found " ) ; } }
Print distinct sorted permutations with duplicates allowed in input | Java program to print all permutations of a string in sorted order . ; Calculating factorial of a number ; Method to print the array ; Method to find total number of permutations ; Building HashMap to store frequencies of all characters . ; Traversing hashmap and finding duplicate elements . ; Start traversing from the end and find position ' i - 1' of the first character which is greater than its successor . ; Finding smallest character after ' i - 1' and greater than temp [ i - 1 ] ; Swapping the above found characters . ; Sort all digits from position next to ' i - 1' to end of the string . ; Print the String ; Sorting String ; Print first permutation ; Finding the total permutations ; Driver Code
import java . io . * ; import java . util . * ; class Solution { static int factorial ( int n ) { int f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) f = f * i ; return f ; } static void print ( char [ ] temp ) { for ( int i = 0 ; i < temp . length ; i ++ ) System . out . print ( temp [ i ] ) ; System . out . println ( ) ; } static int calculateTotal ( char [ ] temp , int n ) { int f = factorial ( n ) ; HashMap < Character , Integer > hm = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < temp . length ; i ++ ) { if ( hm . containsKey ( temp [ i ] ) ) hm . put ( temp [ i ] , hm . get ( temp [ i ] ) + 1 ) ; else hm . put ( temp [ i ] , 1 ) ; } for ( Map . Entry e : hm . entrySet ( ) ) { Integer x = ( Integer ) e . getValue ( ) ; if ( x > 1 ) { int temp5 = factorial ( x ) ; f = f / temp5 ; } } return f ; } static void nextPermutation ( char [ ] temp ) { int i ; for ( i = temp . length - 1 ; i > 0 ; i -- ) if ( temp [ i ] > temp [ i - 1 ] ) break ; int min = i ; int j , x = temp [ i - 1 ] ; for ( j = i + 1 ; j < temp . length ; j ++ ) if ( ( temp [ j ] < temp [ min ] ) && ( temp [ j ] > x ) ) min = j ; char temp_to_swap ; temp_to_swap = temp [ i - 1 ] ; temp [ i - 1 ] = temp [ min ] ; temp [ min ] = temp_to_swap ; Arrays . sort ( temp , i , temp . length ) ; print ( temp ) ; } static void printAllPermutations ( String s ) { char temp [ ] = s . toCharArray ( ) ; Arrays . sort ( temp ) ; print ( temp ) ; int total = calculateTotal ( temp , temp . length ) ; for ( int i = 1 ; i < total ; i ++ ) nextPermutation ( temp ) ; } public static void main ( String [ ] args ) { String s = " AAB " ; printAllPermutations ( s ) ; } }
Swap Nodes in Binary tree of every k 'th level | Java program swap nodes ; A Binary Tree Node ; function to create a new tree node ; A utility function swap left - node & right node of tree of every k 'th level  ; base case ; if current level + 1 is present in swap vector then we swap left & right node ; Recur for left and right subtrees ; This function mainly calls recursive function swapEveryKLevelUtil ( ) ; call swapEveryKLevelUtil function with initial level as 1. ; Utility method for inorder tree traversal ; Driver Code ; 1 / \ 2 3 / / \ 4 7 8
class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void swapEveryKLevelUtil ( Node root , int level , int k ) { if ( root == null || ( root . left == null && root . right == null ) ) return ; if ( ( level + 1 ) % k == 0 ) { Node temp = root . left ; root . left = root . right ; root . right = temp ; } swapEveryKLevelUtil ( root . left , level + 1 , k ) ; swapEveryKLevelUtil ( root . right , level + 1 , k ) ; } static void swapEveryKLevel ( Node root , int k ) { swapEveryKLevelUtil ( root , 1 , k ) ; } static void inorder ( Node root ) { if ( root == null ) return ; inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . right = newNode ( 8 ) ; root . right . left = newNode ( 7 ) ; int k = 2 ; System . out . println ( " Before ▁ swap ▁ node ▁ : " ) ; inorder ( root ) ; swapEveryKLevel ( root , k ) ; System . out . println ( " After swap Node : " );  inorder ( root ) ; } }
Convert a sentence into its equivalent mobile numeric keypad sequence | Java implementation to convert a sentence into its equivalent mobile numeric keypad sequence ; Function which computes the sequence ; length of input string ; Checking for space ; Calculating index for each character ; Output sequence ; Driver Function ; storing the sequence in array
import java . util . * ; class GFG { static String printSequence ( String arr [ ] , String input ) { String output = " " ; int n = input . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( input . charAt ( i ) == ' ▁ ' ) output = output + "0" ; else { int position = input . charAt ( i ) - ' A ' ; output = output + arr [ position ] ; } } return output ; } public static void main ( String [ ] args ) { String str [ ] = { "2" , "22" , "222" , "3" , "33" , "333" , "4" , "44" , "444" , "5" , "55" , "555" , "6" , "66" , "666" , "7" , "77" , "777" , "7777" , "8" , "88" , "888" , "9" , "99" , "999" , "9999" } ; String input = " GEEKSFORGEEKS " ; System . out . println ( printSequence ( str , input ) ) ; } }