codeparrot/xlcost-text-to-code huggingface dataset:
Check sum of Covered and Uncovered nodes of Binary Tree | A binary tree node has key , pointer to left child and a pointer to right child ; Utility function to calculate sum of all node of tree ; Recursive function to calculate sum of left boundary elements ; If left node , then just return its key value ; If left is available then go left otherwise go right ; Recursive function to calculate sum of right boundary elements ; If left node , then just return its key value ; If right is available then go right otherwise go left ; Returns sum of uncovered elements ; Initializing with 0 in case we don 't have  left or right boundary  ; returning sum of root node , left boundary and right boundary ; Returns true if sum of covered and uncovered elements is same . ; Sum of uncovered elements ; Sum of all elements ; Check if sum of covered and uncovered is same ; Helper function to print inorder traversal of binary tree ; Driver program to test above functions ; Making above given diagram 's binary tree
class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class BinaryTree { Node root ; int sum ( Node t ) { if ( t == null ) return 0 ; return t . key + sum ( t . left ) + sum ( t . right ) ; } int uncoveredSumLeft ( Node t ) { if ( t . left == null && t . right == null ) return t . key ; if ( t . left != null ) return t . key + uncoveredSumLeft ( t . left ) ; else return t . key + uncoveredSumLeft ( t . right ) ; } int uncoveredSumRight ( Node t ) { if ( t . left == null && t . right == null ) return t . key ; if ( t . right != null ) return t . key + uncoveredSumRight ( t . right ) ; else return t . key + uncoveredSumRight ( t . left ) ; } int uncoverSum ( Node t ) { int lb = 0 , rb = 0 ; if ( t . left != null ) lb = uncoveredSumLeft ( t . left ) ; if ( t . right != null ) rb = uncoveredSumRight ( t . right ) ; return t . key + lb + rb ; } boolean isSumSame ( Node root ) { int sumUC = uncoverSum ( root ) ; int sumT = sum ( root ) ; return ( sumUC == ( sumT - sumUC ) ) ; } void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . key + " ▁ " ) ; inorder ( root . right ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 8 ) ; tree . root . left = new Node ( 3 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . left . right . left = new Node ( 4 ) ; tree . root . left . right . right = new Node ( 7 ) ; tree . root . right = new Node ( 10 ) ; tree . root . right . right = new Node ( 14 ) ; tree . root . right . right . left = new Node ( 13 ) ; if ( tree . isSumSame ( tree . root ) ) System . out . println ( " Sum ▁ of ▁ covered ▁ and ▁ uncovered ▁ is ▁ same " ) ; else System . out . println ( " Sum ▁ of ▁ covered ▁ and ▁ uncovered ▁ is ▁ not ▁ same " ) ; } }
Check if a number is Bleak | An efficient Java program to check Bleak Number ; Function to get no of set bits in binary representation of passed binary no . ; A function to return ceiling of log x in base 2. For example , it returns 3 for 8 and 4 for 9. ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
import java . io . * ; class GFG { static int countSetBits ( int x ) { int count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } static int ceilLog2 ( int x ) { int count = 0 ; x -- ; while ( x > 0 ) { x = x >> 1 ; count ++ ; } return count ; } static boolean isBleak ( int n ) { for ( int x = n - ceilLog2 ( n ) ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } public static void main ( String [ ] args ) { if ( isBleak ( 3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isBleak ( 4 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count strings with consecutive 1 's | Java program to count all distinct binary strings with two consecutive 1 's ; Returns count of n length binary strings with consecutive 1 's ; Count binary strings without consecutive 1 's.  See the approach discussed on be  ( http:goo.gl/p8A3sW ) ; Subtract a [ n - 1 ] + b [ n - 1 ] ; Driver code
class GFG { static int countStrings ( int n ) { int a [ ] = new int [ n ] , b [ ] = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } from 2 ^ n return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] ; } public static void main ( String args [ ] ) { System . out . println ( countStrings ( 5 ) ) ; } }
How to swap two bits in a given integer ? | Java code for swapping given bits of a number ; left - shift 1 p1 and p2 times and using XOR ; Driver code
import java . util . * ; class Main { public static int swapBits ( int n , int p1 , int p2 ) { n ^= 1 << p1 ; n ^= 1 << p2 ; return n ; } public static void main ( String [ ] args ) { System . out . print ( " Result ▁ = ▁ " + swapBits ( 28 , 0 , 3 ) ) ; } }
Maximum length sub | Java implementation of the approach ; Function to return the maximum length of the required sub - array ; To store the maximum length for a valid subarray ; To store the count of contiguous similar elements for previous group and the current group ; If current element is equal to the previous element then it is a part of the same group ; Else update the previous group and start counting elements for the new group ; Update the maximum possible length for a group ; Return the maximum length of the valid subarray ; Driver code
class GFG { static int maxLength ( int a [ ] , int n ) { int maxLen = 0 ; int prev_cnt = 0 , curr_cnt = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) curr_cnt ++ ; else { prev_cnt = curr_cnt ; curr_cnt = 1 ; } maxLen = Math . max ( maxLen , Math . min ( prev_cnt , curr_cnt ) ) ; } return ( 2 * maxLen ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( maxLength ( arr , n ) ) ; } }
Check if two nodes are cousins in a Binary Tree | A Binary Tree Node ; Recursive function to check if two Nodes are siblings ; Base case ; Recursive function to find level of Node ' ptr ' in a binary tree ; base cases ; Return level if Node is present in left subtree ; Else search in right subtree ; Returns 1 if a and b are cousins , otherwise 0 ; 1. The two Nodes should be on the same level in the binary tree . 2. The two Nodes should not be siblings ( means that they should not have the same parent Node ) . ; 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 ; boolean isSibling ( Node node , Node a , Node b ) { if ( node == null ) return false ; return ( ( node . left == a && node . right == b ) || ( node . left == b && node . right == a ) || isSibling ( node . left , a , b ) || isSibling ( node . right , a , b ) ) ; } int level ( Node node , Node ptr , int lev ) { if ( node == null ) return 0 ; if ( node == ptr ) return lev ; int l = level ( node . left , ptr , lev + 1 ) ; if ( l != 0 ) return l ; return level ( node . right , ptr , lev + 1 ) ; } boolean isCousin ( Node node , Node a , Node b ) { return ( ( level ( node , a , 1 ) == level ( node , b , 1 ) ) && ( ! isSibling ( node , a , b ) ) ) ; } 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 ) ; tree . root . left . right . right = new Node ( 15 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . right . left . right = new Node ( 8 ) ; Node Node1 , Node2 ; Node1 = tree . root . left . left ; Node2 = tree . root . right . right ; if ( tree . isCousin ( tree . root , Node1 , Node2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if all leaves are at same level | A binary tree node ; Recursive function which checks whether all leaves are at same level ; Base case ; If a leaf node is encountered ; When a leaf node is found first time ; Set first found leaf 's level ; If this is not first leaf node , compare its level with first leaf 's level ; If this node is not leaf , recursively check left and right subtrees ; The main function to check if all leafs are at same level . It mainly uses checkUtil ( ) ; Driver Code ; Let us create the tree as shown in the example
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class Leaf { int leaflevel = 0 ; } class BinaryTree { Node root ; Leaf mylevel = new Leaf ( ) ; boolean checkUtil ( Node node , int level , Leaf leafLevel ) { if ( node == null ) return true ; if ( node . left == null && node . right == null ) { if ( leafLevel . leaflevel == 0 ) { leafLevel . leaflevel = level ; return true ; } return ( level == leafLevel . leaflevel ) ; } return checkUtil ( node . left , level + 1 , leafLevel ) && checkUtil ( node . right , level + 1 , leafLevel ) ; } boolean check ( Node node ) { int level = 0 ; return checkUtil ( node , level , mylevel ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 12 ) ; tree . root . left = new Node ( 5 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 9 ) ; tree . root . left . left . left = new Node ( 1 ) ; tree . root . left . right . left = new Node ( 1 ) ; if ( tree . check ( tree . root ) ) System . out . println ( " Leaves ▁ are ▁ at ▁ same ▁ level " ) ; else System . out . println ( " Leaves ▁ are ▁ not ▁ at ▁ same ▁ level " ) ; } }
Number of siblings of a given Node in n | Java program to find number of siblings of a given node ; Represents a node of an n - ary tree ; Function to calculate number of siblings of a given node ; Creating a queue and pushing the root ; Dequeue an item from queue and check if it is equal to x If YES , then return number of children ; Enqueue all children of the dequeued item ; If the value of children is equal to x , then return the number of siblings ; Driver code ; Creating a generic tree as shown in above figure ; Node whose number of siblings is to be calculated ; Function calling
import java . util . * ; class GFG { static class Node { int key ; Vector < Node > child ; Node ( int data ) { key = data ; child = new Vector < Node > ( ) ; } } ; static int numberOfSiblings ( Node root , int x ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { Node p = q . peek ( ) ; q . remove ( ) ; for ( int i = 0 ; i < p . child . size ( ) ; i ++ ) { if ( p . child . get ( i ) . key == x ) return p . child . size ( ) - 1 ; q . add ( p . child . get ( i ) ) ; } } return - 1 ; } public static void main ( String args [ ] ) { Node root = new Node ( 50 ) ; ( root . child ) . add ( new Node ( 2 ) ) ; ( root . child ) . add ( new Node ( 30 ) ) ; ( root . child ) . add ( new Node ( 14 ) ) ; ( root . child ) . add ( new Node ( 60 ) ) ; ( root . child . get ( 0 ) . child ) . add ( new Node ( 15 ) ) ; ( root . child . get ( 0 ) . child ) . add ( new Node ( 25 ) ) ; ( root . child . get ( 0 ) . child . get ( 1 ) . child ) . add ( new Node ( 70 ) ) ; ( root . child . get ( 0 ) . child . get ( 1 ) . child ) . add ( new Node ( 100 ) ) ; ( root . child . get ( 1 ) . child ) . add ( new Node ( 6 ) ) ; ( root . child . get ( 1 ) . child ) . add ( new Node ( 1 ) ) ; ( root . child . get ( 2 ) . child ) . add ( new Node ( 7 ) ) ; ( root . child . get ( 2 ) . child . get ( 0 ) . child ) . add ( new Node ( 17 ) ) ; ( root . child . get ( 2 ) . child . get ( 0 ) . child ) . add ( new Node ( 99 ) ) ; ( root . child . get ( 2 ) . child . get ( 0 ) . child ) . add ( new Node ( 27 ) ) ; ( root . child . get ( 3 ) . child ) . add ( new Node ( 16 ) ) ; int x = 100 ; System . out . println ( numberOfSiblings ( root , x ) ) ; } }
Check if all leaves are at same level | Java program to check if all leaf nodes are at same level of binary tree ; User defined node class ; Constructor to create a new tree node ; return true if all leaf nodes are at same level , else false ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check for left child ; if its leaf node ; if it 's first leaf node, then update result ; if it 's not first leaf node, then compare   the level with level of previous leaf node. ; check for right child ; if its leaf node ; if it 's first leaf node, then update result ; if it 's not first leaf node, then compare   the level with level of previous leaf node. ; Driver code ; construct a tree
import java . util . * ; class Node { int data ; Node left , right ; Node ( int key ) { int data = key ; left = right = null ; } } class GFG { static boolean checkLevelLeafNode ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int result = Integer . MAX_VALUE ; int level = 0 ; while ( q . size ( ) != 0 ) { int size = q . size ( ) ; level ++ ; while ( size > 0 ) { Node temp = q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; if ( temp . left . left == null && temp . left . right == null ) { if ( result == Integer . MAX_VALUE ) result = level ; else if ( result != level ) return false ; } } if ( temp . right != null ) { q . add ( temp . right ) ; if ( temp . right . left == null && temp . right . right == null ) { if ( result == Integer . MAX_VALUE ) result = level ; else if ( result != level ) return false ; } } size -- ; } } return true ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . right = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; boolean result = checkLevelLeafNode ( root ) ; if ( result == true ) System . out . println ( " All ▁ leaf ▁ nodes ▁ are ▁ at ▁ same ▁ level " ) ; else System . out . println ( " Leaf ▁ nodes ▁ not ▁ at ▁ same ▁ level " ) ; } }
FIFO ( First | Java program to demonstrate working of FIFO using Queue interface in Java ; Driver code ; Adds elements { 0 , 1 , 2 , 3 , 4 } to queue ; Display contents of the queue . ; To remove the head of queue . In this the oldest element '0' will be removed ; To view the head of queue ; Rest all methods of collection interface , Like size and contains can be used with this implementation .
import java . util . LinkedList ; import java . util . Queue ; public class QueueExample { public static void main ( String [ ] args ) { Queue < Integer > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) q . add ( i ) ; System . out . println ( " Elements ▁ of ▁ queue - " + q ) ; int removedele = q . remove ( ) ; System . out . println ( " removed ▁ element - " + removedele ) ; System . out . println ( q ) ; int head = q . peek ( ) ; System . out . println ( " head ▁ of ▁ queue - " + head ) ; int size = q . size ( ) ; System . out . println ( " Size ▁ of ▁ queue - " + size ) ; } }
LIFO ( Last | Java program to demonstrate working of LIFO using Stack in Java ; Pushing element on the top of the stack ; Popping element from the top of the stack ; Displaying element on the top of the stack ; Searching element in the stack ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void stack_push ( Stack < Integer > stack ) { for ( int i = 0 ; i < 5 ; i ++ ) { stack . push ( i ) ; } } static void stack_pop ( Stack < Integer > stack ) { System . out . println ( " Pop ▁ : " ) ; for ( int i = 0 ; i < 5 ; i ++ ) { Integer y = ( Integer ) stack . pop ( ) ; System . out . println ( y ) ; } } static void stack_peek ( Stack < Integer > stack ) { Integer element = ( Integer ) stack . peek ( ) ; System . out . println ( " Element ▁ on ▁ stack ▁ top ▁ : ▁ " + element ) ; } static void stack_search ( Stack < Integer > stack , int element ) { Integer pos = ( Integer ) stack . search ( element ) ; if ( pos == - 1 ) System . out . println ( " Element ▁ not ▁ found " ) ; else System . out . println ( " Element ▁ is ▁ found ▁ at ▁ position ▁ " + pos ) ; } public static void main ( String [ ] args ) { Stack < Integer > stack = new Stack < Integer > ( ) ; stack_push ( stack ) ; stack_pop ( stack ) ; stack_push ( stack ) ; stack_peek ( stack ) ; stack_search ( stack , 2 ) ; stack_search ( stack , 6 ) ; } }
Reversing a queue using recursion | Java program to reverse a Queue by recursion ; Utility function to print the queue ; Recurrsive function to reverse the queue ; Base case ; Dequeue current item ( from front ) ; Reverse remaining queue ; Enqueue current item ( to rear ) ; Driver code
import java . util . LinkedList ; import java . util . Queue ; import java . util . Stack ; public class Queue_reverse { static Queue < Integer > queue ; static void Print ( ) { while ( ! queue . isEmpty ( ) ) { System . out . print ( queue . peek ( ) + " ▁ " ) ; queue . remove ( ) ; } } static Queue < Integer > reverseQueue ( Queue < Integer > q ) { if ( q . isEmpty ( ) ) return q ; int data = q . peek ( ) ; q . remove ( ) ; q = reverseQueue ( q ) ; q . add ( data ) ; return q ; } public static void main ( String args [ ] ) { queue = new LinkedList < Integer > ( ) ; queue . add ( 56 ) ; queue . add ( 27 ) ; queue . add ( 30 ) ; queue . add ( 45 ) ; queue . add ( 85 ) ; queue . add ( 92 ) ; queue . add ( 58 ) ; queue . add ( 80 ) ; queue . add ( 90 ) ; queue . add ( 100 ) ; queue = reverseQueue ( queue ) ; Print ( ) ; } }
Sorting a Queue without extra space | Java program to implement sorting a queue data structure ; Queue elements after sortIndex are already sorted . This function returns index of minimum element from front to sortIndex ; This is dequeue ( ) in Java STL ; we add the condition i <= sortIndex because we don 't want to traverse  on the sorted part of the queue,  which is the right part. ; This is enqueue ( ) in C ++ STL ; Moves given minimum element to rear of queue ; SortQueue Function ; Driver function ; Sort Queue ; print sorted Queue
import java . util . LinkedList ; import java . util . Queue ; class GFG { public static int minIndex ( Queue < Integer > list , int sortIndex ) { int min_index = - 1 ; int min_value = Integer . MAX_VALUE ; int s = list . size ( ) ; for ( int i = 0 ; i < s ; i ++ ) { int current = list . peek ( ) ; list . poll ( ) ; if ( current <= min_value && i <= sortIndex ) { min_index = i ; min_value = current ; } list . add ( current ) ; } return min_index ; } public static void insertMinToRear ( Queue < Integer > list , int min_index ) { int min_value = 0 ; int s = list . size ( ) ; for ( int i = 0 ; i < s ; i ++ ) { int current = list . peek ( ) ; list . poll ( ) ; if ( i != min_index ) list . add ( current ) ; else min_value = current ; } list . add ( min_value ) ; } public static void sortQueue ( Queue < Integer > list ) { for ( int i = 1 ; i <= list . size ( ) ; i ++ ) { int min_index = minIndex ( list , list . size ( ) - i ) ; insertMinToRear ( list , min_index ) ; } } public static void main ( String [ ] args ) { Queue < Integer > list = new LinkedList < Integer > ( ) ; list . add ( 30 ) ; list . add ( 11 ) ; list . add ( 15 ) ; list . add ( 4 ) ; sortQueue ( list ) ; while ( list . isEmpty ( ) == false ) { System . out . print ( list . peek ( ) + " ▁ " ) ; list . poll ( ) ; } } }
Check if removing an edge can divide a Binary Tree in two halves | Java program to check if there exist an edge whose removal creates two trees of same size ; To calculate size of tree with given root ; This function returns true if there is an edge whose removal can divide the tree in two halves n is size of tree ; Base cases ; Check for root ; Check for rest of the nodes ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Now recursively check all nodes ; Driver code
class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class BinaryTree { Node root ; int count ( Node node ) { if ( node == null ) return 0 ; return count ( node . left ) + count ( node . right ) + 1 ; } boolean checkRec ( Node node , int n ) { if ( node == null ) return false ; if ( count ( node ) == n - count ( node ) ) return true ; return checkRec ( node . left , n ) || checkRec ( node . right , n ) ; } boolean check ( Node node ) { int n = count ( node ) ; return checkRec ( node , n ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 1 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 4 ) ; if ( tree . check ( tree . root ) == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Sliding Window Maximum ( Maximum of all subarrays of size k ) | Java Program to find the maximum for each and every contiguous subarray of size k . ; A Dequeue ( Double ended queue ) based method for printing maximum element of all subarrays of size k ; Create a Double Ended Queue , Qi that will store indexes of array elements The queue will store indexes of useful elements in every window and it will maintain decreasing order of values from front to rear in Qi , i . e . , arr [ Qi . front [ ] ] to arr [ Qi . rear ( ) ] are sorted in decreasing order ; Process first k ( or first window ) elements of array ; For every element , the previous smaller elements are useless so remove them from Qi ; Remove from rear ; Add new element at rear of queue ; Process rest of the elements , i . e . , from arr [ k ] to arr [ n - 1 ] ; The element at the front of the queue is the largest element of previous window , so print it ; Remove the elements which are out of this window ; Remove from front of queue ; Remove all elements smaller than the currently being added element ( remove useless elements ) ; Add current element at the rear of Qi ; Print the maximum element of last window ; Driver code
import java . util . Deque ; import java . util . LinkedList ; public class SlidingWindow { static void printMax ( int arr [ ] , int n , int k ) { Deque < Integer > Qi = new LinkedList < Integer > ( ) ; int i ; for ( i = 0 ; i < k ; ++ i ) { while ( ! Qi . isEmpty ( ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . removeLast ( ) ; Qi . addLast ( i ) ; } for ( ; i < n ; ++ i ) { System . out . print ( arr [ Qi . peek ( ) ] + " ▁ " ) ; while ( ( ! Qi . isEmpty ( ) ) && Qi . peek ( ) <= i - k ) Qi . removeFirst ( ) ; while ( ( ! Qi . isEmpty ( ) ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . removeLast ( ) ; Qi . addLast ( i ) ; } System . out . print ( arr [ Qi . peek ( ) ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1 , 78 , 90 , 57 , 89 , 56 } ; int k = 3 ; printMax ( arr , arr . length , k ) ; } }