codeparrot/xlcost-text-to-code huggingface dataset:
Print numbers in the range 1 to n having bits in alternate pattern | Java implementation to print numbers in the range 1 to n having bits in alternate pattern ; function to print numbers in the range 1 to n having bits in alternate pattern ; first number having bits in alternate pattern ; display ; loop until n < curr_num ; generate next number having alternate bit pattern ; if true then break ; display ; generate next number having alternate bit pattern ; if true then break ; display ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static void printNumHavingAltBitPatrn ( int n ) { int curr_num = 1 , i = 1 ; System . out . print ( curr_num + " ▁ " ) ; while ( i != 0 ) { i ++ ; curr_num <<= 1 ; if ( n < curr_num ) break ; System . out . print ( curr_num + " ▁ " ) ; curr_num = ( ( curr_num ) << 1 ) ^ 1 ; if ( n < curr_num ) break ; System . out . print ( curr_num + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 50 ; printNumHavingAltBitPatrn ( n ) ; } }
Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | JAVA Code for Smallest perfect power of 2 greater than n ; Function to find smallest perfect power of 2 greater than n ; To store perfect power of 2 ; bitwise left shift by 1 ; bitwise right shift by 1 ; Required perfect power of 2 ; Driver program
import java . util . * ; class GFG { static int perfectPowerOf2 ( int n ) { int per_pow = 1 ; while ( n > 0 ) { per_pow = per_pow << 1 ; n = n >> 1 ; } return per_pow ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( " Perfect ▁ power ▁ of ▁ 2 ▁ greater ▁ than ▁ " + n + " : ▁ " + perfectPowerOf2 ( n ) ) ; } }
Find Unique pair in an array with pairs of numbers | Java program to find a unique pair in an array of pairs . ; XOR each element and get XOR of two unique elements ( ans ) ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; Driver code
class GFG { static void findUniquePair ( int [ ] arr , int n ) { int XOR = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) XOR = XOR ^ arr [ i ] ; int set_bit_no = XOR & ~ ( XOR - 1 ) ; int x = 0 , y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & set_bit_no ) > 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } System . out . println ( " The ▁ unique ▁ pair ▁ is ▁ ( " + x + " , " + y + " ) " ) ; } public static void main ( String [ ] args ) { int [ ] a = { 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 } ; int n = a . length ; findUniquePair ( a , n ) ; } }
Count unset bits of a number | An optimized Java program to count unset bits in an integer . ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Count set bits in toggled number ; Driver code
class GFG { static int countUnsetBits ( int n ) { int x = n ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return Integer . bitCount ( x ^ n ) ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . println ( countUnsetBits ( n ) ) ; } }
Count total bits in a number | Function to get no of bits in binary representation of positive integer ; Driver program
class GFG { static int countBits ( int n ) { int count = 0 ; while ( n != 0 ) { count ++ ; n >>= 1 ; } return count ; } public static void main ( String [ ] arg ) { int i = 65 ; System . out . print ( countBits ( i ) ) ; } }
Toggle all bits after most significant bit | Java program to toggle set bits starting from MSB ; Function to toggle bits starting from MSB ; temporary variable to use XOR with one of a n ; Run loop until the only set bit in temp crosses MST of n . ; Toggle bit of n corresponding to current set bit in temp . ; Move set bit to next higher position . ; Driver code
class GFG { static int toggle ( int n ) { int temp = 1 ; while ( temp <= n ) { n = n ^ temp ; temp = temp << 1 ; } return n ; } public static void main ( String arg [ ] ) { int n = 10 ; n = toggle ( n ) ; System . out . print ( n ) ; } }
Find the n | Java program to find n - th number whose binary representation is palindrome . ; Finds if the kth bit is set in the binary representation ; Returns the position of leftmost set bit in the binary representation ; Finds whether the integer in binary representation is palindrome or not ; One by one compare bits ; Compare left and right bits and converge ; Start from 1 , traverse through all the integers ; If we reach n , break the loop ; Driver code ; Function Call
import java . io . * ; class GFG { static int INT_MAX = 2147483647 ; static int isKthBitSet ( int x , int k ) { return ( ( x & ( 1 << ( k - 1 ) ) ) > 0 ) ? 1 : 0 ; } static int leftmostSetBit ( int x ) { int count = 0 ; while ( x > 0 ) { count ++ ; x = x >> 1 ; } return count ; } static int isBinPalindrome ( int x ) { int l = leftmostSetBit ( x ) ; int r = 1 ; while ( l > r ) { if ( isKthBitSet ( x , l ) != isKthBitSet ( x , r ) ) return 0 ; l -- ; r ++ ; } return 1 ; } static int findNthPalindrome ( int n ) { int pal_count = 0 ; int i = 0 ; for ( i = 1 ; i <= INT_MAX ; i ++ ) { if ( isBinPalindrome ( i ) > 0 ) { pal_count ++ ; } if ( pal_count == n ) break ; } return i ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( findNthPalindrome ( n ) ) ; } }
Check if two numbers are equal without using comparison operators | Java code to check if 2 numbers are same ; Finds if a and b are same ; Driver code
class GFG { static void areSame ( int a , int b ) { if ( ( a - b ) == 0 ) System . out . println ( " Same " ) ; else System . out . println ( " Not ▁ Same " ) ; } public static void main ( String args [ ] ) { areSame ( 10 , 20 ) ; } }
Toggle bits in the given range | Java implementation to toggle bits in the given range ; Function to toggle bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; toggle bits in the range l to r in ' n ' and return the number Besides this , we can calculate num as : num = ( 1 << r ) - l . ; driver program
import java . io . * ; class GFG { static int toggleBitsFromLToR ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( n ^ num ) ; } public static void main ( String [ ] args ) { int n = 50 ; int l = 2 , r = 5 ; System . out . println ( toggleBitsFromLToR ( n , l , r ) ) ; } }
Position of rightmost different bit | Java implementation to find the position of rightmost different bit ; Function to find the position of rightmost set bit in ' n ' return 0 if there is no set bit . ; to handle edge case when n = 0. ; Function to find the position of rightmost different bit in the binary representations of ' m ' and ' n ' ; position of rightmost different bit ; Driver code
class GFG { static int getRightMostSetBit ( int n ) { if ( n == 0 ) return 0 ; return ( int ) ( ( Math . log10 ( n & - n ) ) / Math . log10 ( 2 ) ) + 1 ; } static int posOfRightMostDiffBit ( int m , int n ) { return getRightMostSetBit ( m ^ n ) ; } public static void main ( String arg [ ] ) { int m = 52 , n = 4 ; System . out . print ( " Position ▁ = ▁ " + posOfRightMostDiffBit ( m , n ) ) ; } }
Closest ( or Next ) smaller and greater numbers with same number of set bits | Java Implementation of getNext with Same number of bits 1 's is below ; Function to find next smallest number bigger than n ; Compute c0 and c1 ; If there is no bigger number with the same no . of 1 's ; Driver Code ; input 1 ; input 2
import java . io . * ; class GFG { static int getNext ( int n ) { int c = n ; int c0 = 0 ; int c1 = 0 ; while ( ( ( c & 1 ) == 0 ) && ( c != 0 ) ) { c0 ++ ; c >>= 1 ; } while ( ( c & 1 ) == 1 ) { c1 ++ ; c >>= 1 ; } if ( c0 + c1 == 31 c0 + c1 == 0 ) return - 1 ; return n + ( 1 << c0 ) + ( 1 << ( c1 - 1 ) ) - 1 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( getNext ( n ) ) ; n = 8 ; System . out . println ( getNext ( n ) ) ; } }
Count minimum bits to flip such that XOR of A and B equal to C | Java code to count the Minimum bits in A and B ; If both A [ i ] and B [ i ] are equal ; If Both A and B are unequal ; driver code ; N represent total count of Bits
class GFG { static int totalFlips ( String A , String B , String C , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( A . charAt ( i ) == B . charAt ( i ) && C . charAt ( i ) == '1' ) ++ count ; else if ( A . charAt ( i ) != B . charAt ( i ) && C . charAt ( i ) == '0' ) ++ count ; } return count ; } public static void main ( String [ ] args ) { int N = 5 ; String a = "10100" ; String b = "00010" ; String c = "10011" ; System . out . print ( totalFlips ( a , b , c , N ) ) ; } }
Swap three variables without using temporary variable | Java program to swap three variables without using temporary variable . ; Assign c ' s ▁ value ▁ to ▁ a , ▁ a ' s value to b and b 's value to c. ; Store XOR of all in a ; After this , b has value of a ; After this , c has value of b ; After this , a has value of c ; Driver Code ; Calling Function
class GFG { static int a , b , c ; static void swapThree ( ) { a = a ^ b ^ c ; b = a ^ b ^ c ; c = a ^ b ^ c ; a = a ^ b ^ c ; } public static void main ( String [ ] args ) { a = 10 ; b = 20 ; c = 30 ; System . out . println ( " Before ▁ swapping ▁ a ▁ = ▁ " + a + " , ▁ b ▁ = ▁ " + b + " , c ▁ = ▁ " + c ) ; swapThree ( ) ; System . out . println ( " After ▁ swapping ▁ a ▁ = ▁ " + a + " , ▁ b ▁ = ▁ " + b + " , ▁ c ▁ = ▁ " + c ) ; } }
Find Two Missing Numbers | Set 2 ( XOR based solution ) | Java Program to find 2 Missing Numbers ; Function to find two missing numbers in range [ 1 , n ] . This function assumes that size of array is n - 2 and all array elements are distinct ; Get the XOR of all elements in arr [ ] and { 1 , 2 . . n } ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . int x = 0 , y = 0 ; Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; XOR of first set in arr [ ] and { 1 , 2 , ... n } ; XOR of second set in arr [ ] and { 1 , 2 , ... n } ; Driver program to test above function ; Range of numbers is 2 plus size of array
import java . util . * ; class GFG { static void findTwoMissingNumbers ( int arr [ ] , int n ) { int XOR = arr [ 0 ] ; for ( int i = 1 ; i < n - 2 ; i ++ ) XOR ^= arr [ i ] ; for ( int i = 1 ; i <= n ; i ++ ) XOR ^= i ; int set_bit_no = XOR & ~ ( XOR - 1 ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( ( arr [ i ] & set_bit_no ) > 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( ( i & set_bit_no ) > 0 ) x = x ^ i ; else y = y ^ i ; } System . out . println ( " Two ▁ Missing ▁ Numbers ▁ are ▁ " ) ; System . out . println ( x + " ▁ " + y ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 6 } ; int n = 2 + arr . length ; findTwoMissingNumbers ( arr , n ) ; } }
Find profession in a special family | Java program to find profession of a person at given level and position . ; Function to get no of set bits in binary representation of passed binary no . ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Count set bits in ' pos - 1' ; If set bit count is odd , then doctor , else engineer ; Driver code
class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static char findProffesion ( int level , int pos ) { int c = countSetBits ( pos - 1 ) ; return ( c % 2 != 0 ) ? ' d ' : ' e ' ; } public static void main ( String [ ] args ) { int level = 3 , pos = 4 ; String prof = ( findProffesion ( level , pos ) == ' e ' ) ? " Engineer " : " Doctor " ; System . out . print ( prof ) ; } }
Find XOR of two number without using XOR operator | Java program to find XOR without using ^ ; Returns XOR of x and y ; Driver Code
import java . io . * ; class GFG { static int myXOR ( int x , int y ) { return ( x & ( ~ y ) ) | ( ( ~ x ) & y ) ; } public static void main ( String [ ] args ) { int x = 3 , y = 5 ; System . out . println ( " XOR ▁ is ▁ " + ( myXOR ( x , y ) ) ) ; } }
Find maximum xor of k elements in an array | Java implementation of the approach ; Function to return the maximum xor for a subset of size k from the given array ; Initialize result ; Traverse all subsets of the array ; __builtin_popcount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update maximum xor so far ; Driver code
import java . util . * ; class GFG { static int Max_Xor ( int arr [ ] , int n , int k ) { int maxXor = Integer . MIN_VALUE ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( Integer . bitCount ( i ) == k ) { int cur_xor = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) == 0 ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = Math . max ( maxXor , cur_xor ) ; } } return maxXor ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = arr . length ; int k = 3 ; System . out . println ( Max_Xor ( arr , n , k ) ) ; } }
Convert a given temperature to another system based on given boiling and freezing points | Java program for above approach ; Function to return temperature in the second thermometer ; Calculate the temperature ; Driver Code
import java . io . * ; class GFG { static double temp_convert ( int F1 , int B1 , int F2 , int B2 , int T ) { float t2 ; t2 = F2 + ( float ) ( B2 - F2 ) / ( B1 - F1 ) * ( T - F1 ) ; return t2 ; } public static void main ( String [ ] args ) { int F1 = 0 , B1 = 100 ; int F2 = 32 , B2 = 212 ; int T = 37 ; float t2 ; System . out . printf ( " % .2f " , temp_convert ( F1 , B1 , F2 , B2 , T ) ) ; } }
Maximum possible elements which are divisible by 2 | Java program to find maximum possible elements which divisible by 2 ; Function to find maximum possible elements which divisible by 2 ; To store count of even numbers ; All even numbers and half of odd numbers ; Driver code ; Function call
class GFG { static int Divisible ( int arr [ ] , int n ) { int count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) count_even ++ ; return count_even + ( n - count_even ) / 2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( Divisible ( arr , n ) ) ; } }
Select a Random Node from a tree with equal probability | Java program to Select a Random Node from a tree ; This is used to fill children counts . ; Inserts Children count for each node ; returns number of children for root ; Helper Function to return a random node ; Returns Random node ; Driver Code ; Creating Above Tree
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static class Node { int data ; int children ; Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; temp . children = 0 ; return temp ; } static int getElements ( Node root ) { if ( root == null ) return 0 ; return getElements ( root . left ) + getElements ( root . right ) + 1 ; } static Node insertChildrenCount ( Node root ) { if ( root == null ) return null ; root . children = getElements ( root ) - 1 ; root . left = insertChildrenCount ( root . left ) ; root . right = insertChildrenCount ( root . right ) ; return root ; } static int children ( Node root ) { if ( root == null ) return 0 ; return root . children + 1 ; } static int randomNodeUtil ( Node root , int count ) { if ( root == null ) return 0 ; if ( count == children ( root . left ) ) return root . data ; if ( count < children ( root . left ) ) return randomNodeUtil ( root . left , count ) ; return randomNodeUtil ( root . right , count - children ( root . left ) - 1 ) ; } static int randomNode ( Node root ) { int count = ( int ) Math . random ( ) * ( root . children + 1 ) ; return randomNodeUtil ( root , count ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . right = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; root . right . left = newNode ( 60 ) ; root . right . right = newNode ( 70 ) ; insertChildrenCount ( root ) ; System . out . println ( " A ▁ Random ▁ Node ▁ From ▁ Tree ▁ : ▁ " + randomNode ( root ) ) ; } }
Implement rand3 ( ) using rand2 ( ) | Java Program to print 0 , 1 or 2 with equal probability ; Random Function to that returns 0 or 1 with equal probability ; rand ( ) function will generate odd or even number with equal probability . If rand ( ) generates odd number , the function will return 1 else it will return 0. ; Random Function to that returns 0 , 1 or 2 with equal probability 1 with 75 % ; returns 0 , 1 , 2 or 3 with 25 % probability ; Driver code
import java . util . Random ; class GFG { static int rand2 ( ) { Random rand = new Random ( ) ; return ( rand . nextInt ( ) & 1 ) ; } static int rand3 ( ) { int r = 2 * rand2 ( ) + rand2 ( ) ; if ( r < 3 ) return r ; return rand3 ( ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) System . out . print ( rand3 ( ) ) ; } }
Delete leaf nodes with value as x | Java code to delete all leaves with given value . ; A binary tree node ; A utility function to allocate a new node ; deleteleaves ( ) ; inorder ( ) ; Driver program
class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return ( newNode ) ; } static Node deleteLeaves ( Node root , int x ) { if ( root == null ) return null ; root . left = deleteLeaves ( root . left , x ) ; root . right = deleteLeaves ( root . right , x ) ; if ( root . data == x && root . left == null && root . right == null ) { return null ; } return root ; } 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 ( 10 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 10 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 1 ) ; root . right . right = newNode ( 3 ) ; root . right . right . left = newNode ( 3 ) ; root . right . right . right = newNode ( 3 ) ; deleteLeaves ( root , 3 ) ; System . out . print ( " Inorder ▁ traversal ▁ after ▁ deletion ▁ : ▁ " ) ; inorder ( root ) ; } }
Non | Non - recursive program to delete the entire binary tree ; A binary tree node ; Non - recursive function to delete an entire binary tree . ; Base Case ; Create an empty queue for level order traversal ; Do level order traversal starting from root ; Deletes a tree and sets the root as NULL ; Driver program to test above functions ; create a binary tree ; delete entire binary tree
import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; void _deleteTree ( ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { Node node = q . peek ( ) ; q . poll ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } } void deleteTree ( ) { _deleteTree ( ) ; root = null ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 15 ) ; tree . root . left = new Node ( 10 ) ; tree . root . right = new Node ( 20 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . right . left = new Node ( 16 ) ; tree . root . right . right = new Node ( 25 ) ; tree . deleteTree ( ) ; } }
Iterative program to Calculate Size of a tree | Java programn to calculate Size of a tree ; Node Structure ; return size of tree ; if tree is empty it will return 0 ; Using level order Traversal . ; when the queue is empty : the poll ( ) method returns null . ; Increment count ; Enqueue left child ; Increment count ; Enqueue right child ; Driver code ; creating a binary tree and entering the nodes
import java . util . LinkedList ; import java . util . Queue ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; public int size ( ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < Node > ( ) ; q . offer ( root ) ; int count = 1 ; while ( ! q . isEmpty ( ) ) { Node tmp = q . poll ( ) ; if ( tmp != null ) { if ( tmp . left != null ) { count ++ ; q . offer ( tmp . left ) ; } if ( tmp . right != null ) { count ++ ; q . offer ( tmp . right ) ; } } } return count ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " The ▁ size ▁ of ▁ binary ▁ tree " + " ▁ is ▁ : ▁ " + tree . size ( ) ) ; } }
Write a Program to Find the Maximum Depth or Height of a Tree | A binary tree node ; Compute the " maxDepth " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the depth of each subtree ; use the larger one ; Driver program to test above functions
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int maxDepth ( Node node ) { if ( node == null ) return 0 ; else { int lDepth = maxDepth ( node . left ) ; int rDepth = maxDepth ( node . right ) ; if ( lDepth > rDepth ) return ( lDepth + 1 ) ; else return ( rDepth + 1 ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " Height ▁ of ▁ tree ▁ is ▁ : ▁ " + tree . maxDepth ( tree . root ) ) ; } }