codeparrot/xlcost-text-to-code huggingface dataset:
Smallest number possible by swapping adjacent even odd pairs | Java program to find the smallest number possible by swapping adjacent digits of different parity ; Function to return the smallest number possible ; Arrays to store odd and even digits in the order their appearance in the given String ; Insert the odd and even digits ; Pointer to odd digit ; Pointer to even digit ; In case number of even and odd digits are not equal If odd digits are remaining ; If even digits are remaining ; Removal of leading 0 's ; Driver code
import java . util . * ; class GFG { static String findAns ( String s ) { int digit ; Vector < Integer > odd = new Vector < Integer > ( ) ; Vector < Integer > even = new Vector < Integer > ( ) ; for ( char c : s . toCharArray ( ) ) { digit = c - '0' ; if ( digit % 2 == 1 ) odd . add ( digit ) ; else even . add ( digit ) ; } int i = 0 ; int j = 0 ; String ans = " " ; while ( i < odd . size ( ) && j < even . size ( ) ) { if ( odd . get ( i ) < even . get ( j ) ) ans += ( char ) ( odd . get ( i ++ ) + '0' ) ; else ans += ( char ) ( even . get ( j ++ ) + '0' ) ; } while ( i < odd . size ( ) ) ans += ( char ) ( odd . get ( i ++ ) + '0' ) ; while ( j < even . size ( ) ) ans += ( char ) ( even . get ( j ++ ) + '0' ) ; while ( ans . charAt ( 0 ) == '0' ) { ans = ans . substring ( 1 ) ; } return ans ; } public static void main ( String [ ] args ) { String s = "894687536" ; System . out . print ( findAns ( s ) ) ; } }
Program to Convert BCD number into Decimal number | Java code to convert BCD to its decimal number ( base 10 ) . Including Header Files ; Function to convert BCD to Decimal ; Iterating through the bits backwards ; Forming the equivalent digit ( 0 to 9 ) from the group of 4. ; Reinitialize all variables and compute the number . ; Update the answer ; Reverse the number formed . ; Driver code ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static int bcdToDecimal ( String s ) { int len = s . length ( ) ; int check = 0 , check0 = 0 ; int num = 0 , sum = 0 ; int mul = 1 , rev = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { sum += ( s . charAt ( i ) - '0' ) * mul ; mul *= 2 ; check ++ ; if ( check == 4 i == 0 ) { if ( sum == 0 && check0 == 0 ) { num = 1 ; check0 = 1 ; } else { num = num * 10 + sum ; } check = 0 ; sum = 0 ; mul = 1 ; } } while ( num > 0 ) { rev = rev * 10 + ( num % 10 ) ; num /= 10 ; } if ( check0 == 1 ) return rev - 1 ; return rev ; } public static void main ( String [ ] args ) { String s = "100000101000" ; System . out . println ( bcdToDecimal ( s ) ) ; } }
Find Count of Single Valued Subtrees | Class containing left and right child of current node and key value ; This function increments count by number of single valued subtrees under root . It returns true if subtree under root is Singly , else false . ; Return false to indicate NULL ; Recursively count in left and right subtrees also ; If any of the subtrees is not singly , then this cannot be singly . ; If left subtree is singly and non - empty , but data doesn 't match ; Same for right subtree ; If none of the above conditions is true , then tree rooted under root is single valued , increment count and return true . ; This function mainly calls countSingleRec ( ) after initializing count as 0 ; Recursive function to count ; Driver program to test above functions ; Let us construct the below tree 5 / \ 4 5 / \ \ 4 4 5
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class Count { int count = 0 ; } class BinaryTree { Node root ; Count ct = new Count ( ) ; boolean countSingleRec ( Node node , Count c ) { if ( node == null ) return true ; boolean left = countSingleRec ( node . left , c ) ; boolean right = countSingleRec ( node . right , c ) ; if ( left == false right == false ) return false ; if ( node . left != null && node . data != node . left . data ) return false ; if ( node . right != null && node . data != node . right . data ) return false ; c . count ++ ; return true ; } int countSingle ( ) { return countSingle ( root ) ; } int countSingle ( Node node ) { countSingleRec ( node , ct ) ; return ct . count ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 4 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . right . right = new Node ( 5 ) ; System . out . println ( " The ▁ count ▁ of ▁ single ▁ valued ▁ sub ▁ trees ▁ is ▁ : ▁ " + tree . countSingle ( ) ) ; } }
Bitwise XOR of a Binary array | Java implementation of the approach ; Function to return the reverse string ; Function to return the bitwise XOR of all the binary strings ; Get max size and reverse each string Since we have to perform XOR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform XOR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Driver code
import java . io . * ; import java . util . * ; class GFG { static String reverse ( String str ) { String rev = " " ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) rev = rev + str . charAt ( i ) ; return rev ; } static String strBitwiseXOR ( String [ ] arr , int n ) { String result = " " ; int max_len = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { max_len = Math . max ( max_len , ( int ) arr [ i ] . length ( ) ) ; arr [ i ] = reverse ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = " " ; for ( int j = 0 ; j < max_len - arr [ i ] . length ( ) ; j ++ ) s += '0' ; arr [ i ] = arr [ i ] + s ; } for ( int i = 0 ; i < max_len ; i ++ ) { int pres_bit = 0 ; for ( int j = 0 ; j < n ; j ++ ) pres_bit = pres_bit ^ ( arr [ j ] . charAt ( i ) - '0' ) ; result += ( char ) ( pres_bit + '0' ) ; } result = reverse ( result ) ; return result ; } public static void main ( String [ ] args ) { String [ ] arr = { "1000" , "10001" , "0011" } ; int n = arr . length ; System . out . print ( strBitwiseXOR ( arr , n ) ) ; } }
Count of sticks required to represent the given string | Java implementation of the above approach ; stick [ ] stores the count of matchsticks required to represent the alphabets ; number [ ] stores the count of matchsticks required to represent the numerals ; Function that return the count of sticks required to represent the given string ; For every char of the given string ; Add the count of sticks required to represent the current character ; Driver code ; Function call to find the count of matchsticks
class GFG { static int sticks [ ] = { 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 } ; static int number [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int countSticks ( String str ) { int cnt = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch >= ' A ' && ch <= ' Z ' ) { cnt += sticks [ ch - ' A ' ] ; } else { cnt += number [ ch - '0' ] ; } } return cnt ; } public static void main ( String [ ] args ) { String str = " GEEKSFORGEEKS " ; System . out . println ( countSticks ( str ) ) ; } }
Find the last remaining Character in the Binary String according to the given conditions | Java implementation of the above approach ; Converting string to array ; Delete counters for each to count the deletes ; Counters to keep track of characters left from each type ; Queue to simulate the process ; Initializing the queue ; Looping till at least 1 digit is left from both the type ; If there is a floating delete for current character we will delete it and move forward otherwise we will increase delete counter for opposite digit ; If 0 are left then answer is 0 else answer is 1 ; Driver Code ; Input String ; Length of String ; Printing answer
import java . util . * ; public class GfG { private static String remainingDigit ( String S , int N ) { char c [ ] = S . toCharArray ( ) ; int del [ ] = { 0 , 0 } ; int count [ ] = { 0 , 0 } ; Queue < Integer > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int x = c [ i ] == '1' ? 1 : 0 ; count [ x ] ++ ; q . add ( x ) ; } while ( count [ 0 ] > 0 && count [ 1 ] > 0 ) { int t = q . poll ( ) ; if ( del [ t ] > 0 ) { del [ t ] -- ; count [ t ] -- ; } else { del [ t ^ 1 ] ++ ; q . add ( t ) ; } } if ( count [ 0 ] > 0 ) return "0" ; return "1" ; } public static void main ( String args [ ] ) { String S = "1010100100000" ; int N = S . length ( ) ; System . out . print ( remainingDigit ( S , N ) ) ; } }
Closest leaf to a given node in Binary Tree | A binary tree node ;  ; This function finds closest leaf to root . This distance is stored at * minDist . ; base case ; If this is a leaf node , then check if it is closer than the closest so far ; Recur for left and right subtrees ; This function finds if there is closer leaf to x through parent node . ; Base cases ; Search x in left subtree of root ; If left subtree has x ; Find closest leaf in right subtree ; Search x in right subtree of root ; If right subtree has x ; Find closest leaf in left subtree ; Returns minimum distance of a leaf from given node x ; Initialize result ( minimum distance from a leaf ) ; Find closest leaf down to x ; See if there is a closer leaf through parent ; Driver program ; Let us create Binary Tree shown in above example
class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class Distance { int minDis = Integer . MAX_VALUE ; } class BinaryTree { Node root ; void findLeafDown ( Node root , int lev , Distance minDist ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { if ( lev < ( minDist . minDis ) ) minDist . minDis = lev ; return ; } findLeafDown ( root . left , lev + 1 , minDist ) ; findLeafDown ( root . right , lev + 1 , minDist ) ; } int findThroughParent ( Node root , Node x , Distance minDist ) { if ( root == null ) return - 1 ; if ( root == x ) return 0 ; int l = findThroughParent ( root . left , x , minDist ) ; if ( l != - 1 ) { findLeafDown ( root . right , l + 2 , minDist ) ; return l + 1 ; } int r = findThroughParent ( root . right , x , minDist ) ; if ( r != - 1 ) { findLeafDown ( root . left , r + 2 , minDist ) ; return r + 1 ; } return - 1 ; } int minimumDistance ( Node root , Node x ) { Distance d = new Distance ( ) ; findLeafDown ( x , 0 , d ) ; findThroughParent ( root , x , d ) ; return d . minDis ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 13 ) ; tree . root . right . left = new Node ( 14 ) ; tree . root . right . right = new Node ( 15 ) ; tree . root . right . left . left = new Node ( 21 ) ; tree . root . right . left . right = new Node ( 22 ) ; tree . root . right . right . left = new Node ( 23 ) ; tree . root . right . right . right = new Node ( 24 ) ; tree . root . right . left . left . left = new Node ( 1 ) ; tree . root . right . left . left . right = new Node ( 2 ) ; tree . root . right . left . right . left = new Node ( 3 ) ; tree . root . right . left . right . right = new Node ( 4 ) ; tree . root . right . right . left . left = new Node ( 5 ) ; tree . root . right . right . left . right = new Node ( 6 ) ; tree . root . right . right . right . left = new Node ( 7 ) ; tree . root . right . right . right . right = new Node ( 8 ) ; Node x = tree . root . right ; System . out . println ( " The ▁ closest ▁ leaf ▁ to ▁ node ▁ with ▁ value ▁ " + x . key + " ▁ is ▁ at ▁ a ▁ distance ▁ of ▁ " + tree . minimumDistance ( tree . root , x ) ) ; } }
Find the time which is palindromic and comes after the given time | Java implementation of the approach ; Function to return the required time ; To store the resultant time ; Hours are stored in h as integer ; Minutes are stored in m as integer ; Reverse of h ; Reverse of h as a string ; If MM < reverse of ( HH ) ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Increment hours ; Reverse of the hour after incrementing 1 ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Driver Code
import java . util . * ; class GFG { static String getTime ( String s , int n ) { String res = " " ; int h = Integer . parseInt ( s . substring ( 0 , 0 + 2 ) ) ; int m = Integer . parseInt ( s . substring ( 3 , 3 + 2 ) ) ; int rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; String rev_hs = Integer . toString ( rev_h ) ; if ( h == 23 && m >= 32 ) { res = " - 1" ; } else if ( m < rev_h ) { String temp = " " ; if ( h < 10 ) temp = "0" ; temp = temp + Integer . toString ( h ) ; if ( rev_h < 10 ) res = res + temp + " : 0" + rev_hs ; else res = res + temp + " : " + rev_hs ; } else { h ++ ; rev_h = ( h % 10 ) * 10 + ( ( h % 100 ) - ( h % 10 ) ) / 10 ; rev_hs = Integer . toString ( rev_h ) ; String temp = " " ; if ( h < 10 ) temp = "0" ; temp = temp + Integer . toString ( h ) ; if ( rev_h < 10 ) res = res + temp + " : 0" + rev_hs ; else res = res + temp + " : " + rev_hs ; } return res ; } public static void main ( String [ ] args ) { String s = "21:12" ; int n = s . length ( ) ; System . out . println ( getTime ( s , n ) ) ; } }
Count of sub | Java implementation of the approach ; Function to return the count of valid sub - strings ; Variable ans to store all the possible substrings Initialize its value as total number of substrings that can be formed from the given string ; Stores recent index of the characters ; If character is a update a 's index  and the variable ans ; If character is b update b 's index  and the variable ans ; If character is c update c 's index  and the variable ans ; Driver code
class GFG { static int CountSubstring ( char str [ ] , int n ) { int ans = ( n * ( n + 1 ) ) / 2 ; int a_index = 0 ; int b_index = 0 ; int c_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' a ' ) { a_index = i + 1 ; ans -= Math . min ( b_index , c_index ) ; } else if ( str [ i ] == ' b ' ) { b_index = i + 1 ; ans -= Math . min ( a_index , c_index ) ; } else { c_index = i + 1 ; ans -= Math . min ( a_index , b_index ) ; } } return ans ; } public static void main ( String [ ] args ) { char str [ ] = " babac " . toCharArray ( ) ; int n = str . length ; System . out . println ( CountSubstring ( str , n ) ) ; } }
Find the numbers of strings that can be formed after processing Q queries | Java program to implement above approach ; To store the size of string and number of queries ; To store parent and rank of ith place ; To store maximum interval ; Function for initialization ; Function to find parent ; Function to make union ; Power function to calculate a raised to m1 under modulo 10000007 ; Function to take maxmium interval ; Function to find different possible strings ; make union of all chracters which are meant to be same ; find number of different sets formed ; return the required answer ; Driver Code ; queries
import java . util . * ; class GFG { static final int N = 2005 ; static final int mod = 1000000007 ; static int n , q ; static int [ ] par = new int [ N ] , Rank = new int [ N ] ; static HashMap < Integer , Integer > m = new HashMap < > ( ) ; static void initialize ( ) { for ( int i = 0 ; i <= n ; i ++ ) { par [ i ] = i ; Rank [ i ] = 0 ; } } static int find ( int x ) { if ( par [ x ] != x ) par [ x ] = find ( par [ x ] ) ; return par [ x ] ; } static void Union ( int x , int y ) { int xpar = find ( x ) ; int ypar = find ( y ) ; if ( Rank [ xpar ] < Rank [ ypar ] ) par [ xpar ] = ypar ; else if ( Rank [ xpar ] > Rank [ ypar ] ) par [ ypar ] = xpar ; else { par [ ypar ] = xpar ; Rank [ xpar ] ++ ; } } static long power ( long a , long m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1L * a * a ) % mod ; else if ( m1 % 2 == 1 ) return ( 1L * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static void query ( int l , int r ) { if ( m . containsKey ( l + r ) ) m . put ( l + r , Math . max ( m . get ( l + r ) , r ) ) ; else m . put ( l + r , r ) ; } static long possiblestrings ( ) { initialize ( ) ; for ( Integer i : m . keySet ( ) ) { int x = i - m . get ( i ) ; int y = m . get ( i ) ; while ( x < y ) { Union ( x , y ) ; x ++ ; y -- ; } } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( par [ i ] == i ) ans ++ ; return power ( 26 , ans ) % mod ; } public static void main ( String [ ] args ) { n = 4 ; query ( 1 , 3 ) ; query ( 2 , 4 ) ; System . out . println ( possiblestrings ( ) ) ; } }
Iterative Search for a key ' x ' in Binary Tree | Iterative level order traversal based method to search in Binary Tree ; A binary tree node has data , left child and right child ; Constructor that allocates a new node with the given data and null left and right pointers . ; An iterative process to search an element x in a given binary tree ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; Queue based level order traversal ; See if current node is same as x ; Remove current node and enqueue its children ; Driver code
import java . util . * ; class GFG { static class node { int data ; node left ; node right ; node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } ; static boolean iterativeSearch ( node root , int x ) { if ( root == null ) return false ; Queue < node > q = new LinkedList ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { node node = q . peek ( ) ; if ( node . data == x ) return true ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } return false ; } public static void main ( String ags [ ] ) { node NewRoot = null ; node root = new node ( 2 ) ; root . left = new node ( 7 ) ; root . right = new node ( 5 ) ; root . left . right = new node ( 6 ) ; root . left . right . left = new node ( 1 ) ; root . left . right . right = new node ( 11 ) ; root . right . right = new node ( 9 ) ; root . right . right . left = new node ( 4 ) ; System . out . print ( ( iterativeSearch ( root , 6 ) ? "Found " : ▁ " Not Found "));  System . out . print ( ( iterativeSearch ( root , 12 ) ? "Found " : ▁ " Not Found "));  } }
Populate Inorder Successor for all nodes | Java 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
class Node { int data ; Node left , right , next ; Node ( int item ) { data = item ; left = right = 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 | Java 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
class gfg { static boolean isValid ( String str , int len ) { for ( int i = 1 ; i < len ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( i - 1 ) ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = "0110" ; int len = str . length ( ) ; if ( isValid ( str , len ) ) System . out . println ( " Valid " ) ; else System . out . println ( " Invalid " ) ; } }
Minimum K such that every substring of length atleast K contains a character c | Java 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
class GFG { static int check ( String s , int K ) { for ( int ch = 0 ; ch < 26 ; ch ++ ) { char c = ( char ) ( ' a ' + ch ) ; int last = - 1 ; boolean found = true ; for ( int i = 0 ; i < K ; i ++ ) if ( s . charAt ( i ) == c ) last = i ; if ( last == - 1 ) continue ; for ( int i = K ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( 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 args [ ] ) { String s = " abcde " ; System . out . println ( binarySearch ( s ) ) ; s = " aaaa " ; System . out . println ( binarySearch ( s ) ) ; } }
Count the number of vowels occurring in all the substrings of given string | Java 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
import java . io . * ; import java . util . * ; 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 . charAt ( i ) ; if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) sum += arr [ i ] ; } return sum ; } public static void main ( String args [ ] ) { String s = " daceh " ; System . out . println ( vowel_calc ( s ) ) ; } }