codeparrot/xlcost-text-to-code huggingface dataset:
Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | Java program for the above approach ; Function to convert obtained binary representation to decimal value ; Stores the resultant number ; Traverse string arr ; Return the number formed ; Function to count the number of set bits in the number num ; Stores the count of set bits ; Iterate until num > 0 ; Function to check if the given matrix satisfies the given condition or not ; To get P , S , MR , and MC ; Stores decimal equivalents of binary representations ; Gett the number of set bits ; Print the answer ; Driver code
import java . util . ArrayList ; import java . util . Collections ; class GFG { static int convert ( ArrayList < Integer > p ) { int ans = 0 ; for ( int i : p ) { ans = ( ans << 1 ) | i ; } return ans ; } static int count ( int num ) { int ans = 0 ; while ( num > 0 ) { ans += num & 1 ; num >>= 1 ; } return ans ; } static void checkGoodMatrix ( int mat [ ] [ ] ) { ArrayList < Integer > P = new ArrayList < Integer > ( ) ; ArrayList < Integer > S = new ArrayList < Integer > ( ) ; ArrayList < Integer > MR = new ArrayList < Integer > ( ) ; ArrayList < Integer > MC = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < mat . length ; i ++ ) { for ( int j = 0 ; j < mat [ 0 ] . length ; j ++ ) { if ( i == j ) P . add ( mat [ i ] [ j ] ) ; if ( i + j == mat . length - 1 ) S . add ( mat [ i ] [ j ] ) ; if ( i == Math . floor ( ( mat . length - 1 ) / 2 ) ) MR . add ( mat [ i ] [ j ] ) ; if ( j == Math . floor ( ( mat . length - 1 ) / 2 ) ) MC . add ( mat [ i ] [ j ] ) ; } } Collections . reverse ( S ) ; int P0 = convert ( P ) ; int S0 = convert ( S ) ; int MR0 = convert ( MR ) ; int MC0 = convert ( MC ) ; int setBitsPS = count ( ( P0 & S0 ) ) ; int setBitsMM = count ( ( MR0 & MC0 ) ) ; if ( setBitsPS > setBitsMM ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 1 } , { 0 , 0 , 1 } , { 0 , 1 , 1 } } ; checkGoodMatrix ( mat ) ; } }
Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path | Java program for the above approach ; Function to assign values to nodes of the tree s . t . sum of values of nodes of path between any 2 nodes is not divisible by length of path ; Stores the adjacency list ; Create a adjacency list ; Stores whether node is visited or not ; Stores the node values ; Variable used to assign values to the nodes alternatively to the parent child ; Declare a queue ; Push the 1 st node ; Assign K value to this node ; Dequeue the node ; Mark it as visited ; Upgrade the value of K ; Assign K to the child nodes ; If the child is unvisited ; Enqueue the child ; Assign K to the child ; Print the value assigned to the nodes ; Driver code ; Function Call
import java . util . * ; class GFG { static void assignValues ( int Edges [ ] [ ] , int n ) { ArrayList < ArrayList < Integer > > tree = new ArrayList < > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) tree . add ( new ArrayList < > ( ) ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; tree . get ( u ) . add ( v ) ; tree . get ( v ) . add ( u ) ; } boolean [ ] visited = new boolean [ n + 1 ] ; int [ ] answer = new int [ n + 1 ] ; int K = 1 ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( 1 ) ; answer [ 1 ] = K ; while ( ! q . isEmpty ( ) ) { int node = q . peek ( ) ; q . poll ( ) ; visited [ node ] = true ; K = ( ( answer [ node ] == 1 ) ? 2 : 1 ) ; for ( Integer child : tree . get ( node ) ) { if ( ! visited [ child ] ) { q . add ( child ) ; answer [ child ] = K ; } } } for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( answer [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 11 ; int Edges [ ] [ ] = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 1 , 5 } , { 2 , 6 } , { 2 , 10 } , { 10 , 11 } , { 3 , 7 } , { 4 , 8 } , { 5 , 9 } } ; assignValues ( Edges , N ) ; } }
Number of full binary trees such that each node is product of its children | Java program to find number of full binary tree such that each node is product of its children . ; Return the number of all possible full binary tree with given product property . ; Finding the minimum and maximum values in given array . ; Marking the presence of each array element and initialising the number of possible full binary tree for each integer equal to 1 because single node will also contribute as a full binary tree . ; From minimum value to maximum value of array finding the number of all possible Full Binary Trees . ; Find if value present in the array ; For each multiple of i , less than equal to maximum value of array ; If multiple is not present in the array then continue . ; Finding the number of possible Full binary trees for multiple j by multiplying number of possible Full binary tree from the number i and number of possible Full binary tree from i / j . ; Condition for possiblity when left chid became right child and vice versa . ; driver code
import java . util . Arrays ; class GFG { static int numoffbt ( int arr [ ] , int n ) { int maxvalue = - 2147483647 ; int minvalue = 2147483647 ; for ( int i = 0 ; i < n ; i ++ ) { maxvalue = Math . max ( maxvalue , arr [ i ] ) ; minvalue = Math . min ( minvalue , arr [ i ] ) ; } int mark [ ] = new int [ maxvalue + 2 ] ; int value [ ] = new int [ maxvalue + 2 ] ; Arrays . fill ( mark , 0 ) ; Arrays . fill ( value , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { mark [ arr [ i ] ] = 1 ; value [ arr [ i ] ] = 1 ; } int ans = 0 ; for ( int i = minvalue ; i <= maxvalue ; i ++ ) { if ( mark [ i ] != 0 ) { for ( int j = i + i ; j <= maxvalue && j / i <= i ; j += i ) { if ( mark [ j ] == 0 ) continue ; value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; if ( i != j / i ) value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; } } ans += value [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 6 } ; int n = arr . length ; System . out . print ( numoffbt ( arr , n ) ) ; } }
Minimum prime numbers required to be subtracted to make all array elements equal | Java program for the above approach ; Stores the sieve of prime numbers ; Function that performs the Sieve of Eratosthenes ; Initialize all numbers as prime ; Iterate over the range [ 2 , 1000 ] ; If the current element is a prime number ; Mark all its multiples as false ; Function to find the minimum number of subtraction of primes numbers required to make all array elements the same ; Perform sieve of eratosthenes ; Find the minimum value ; Stores the value to each array element should be reduced ; If an element exists with value ( M + 1 ) ; Stores the minimum count of subtraction of prime numbers ; Traverse the array ; If D is equal to 0 ; If D is a prime number ; Increase count by 1 ; If D is an even number ; Increase count by 2 ; If D - 2 is prime ; Increase count by 2 ; Otherwise , increase count by 3 ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int limit = 100000 ; static boolean prime [ ] ; static void sieve ( ) { prime = new boolean [ limit + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= limit ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= limit ; i += p ) prime [ i ] = false ; } } } static int findOperations ( int arr [ ] , int n ) { sieve ( ) ; int minm = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { minm = Math . min ( minm , arr [ i ] ) ; } int val = minm ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == minm + 1 ) { val = minm - 2 ; break ; } } int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int D = arr [ i ] - val ; if ( D == 0 ) { continue ; } else if ( prime [ D ] == true ) { cnt += 1 ; } else if ( D % 2 == 0 ) { cnt += 2 ; } else { if ( prime [ D - 2 ] == true ) { cnt += 2 ; } else { cnt += 3 ; } } } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 10 , 4 , 5 } ; int N = 4 ; System . out . println ( findOperations ( arr , N ) ) ; } }
Maximize sum of Bitwise AND of same | Java program for the above approach ; Function to calculate sum of Bitwise AND of same indexed elements of the arrays p [ ] and arr [ ] ; Stores the resultant sum ; Traverse the array ; Update sum of Bitwise AND ; Return the value obtained ; Function to generate all permutations and calculate the maximum sum of Bitwise AND of same indexed elements present in any permutation and an array arr [ ] ; If the size of the array is N ; Calculate cost of permutation ; Generate all permutations ; Update chosen [ i ] ; Update the permutation p [ ] ; Generate remaining permutations ; Return the resultant sum ; Function to find the maximum sum of Bitwise AND of same indexed elements in a permutation of first N natural numbers and arr [ ] ; Stores the resultant maximum sum ; Stores the generated permutation P ; Function call to store result ; Print the result ; Driver Code ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int calcScore ( ArrayList < Integer > p , int arr [ ] ) { int ans = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { ans += ( p . get ( i ) & arr [ i ] ) ; } return ans ; } static int getMaxUtil ( ArrayList < Integer > p , int arr [ ] , int ans , boolean chosen [ ] , int N ) { if ( p . size ( ) == N ) { ans = Math . max ( ans , calcScore ( p , arr ) ) ; return ans ; } for ( int i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) { continue ; } chosen [ i ] = true ; p . add ( i ) ; ans = getMaxUtil ( p , arr , ans , chosen , N ) ; chosen [ i ] = false ; p . remove ( p . size ( ) - 1 ) ; } return ans ; } static void getMax ( int arr [ ] , int N ) { int ans = 0 ; boolean chosen [ ] = new boolean [ N ] ; ArrayList < Integer > p = new ArrayList < > ( ) ; int res = getMaxUtil ( p , arr , ans , chosen , N ) ; System . out . println ( res ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 3 , 6 } ; int N = arr . length ; getMax ( arr , N ) ; } }
Sum of array elements whose count of set bits are unique | Java program for the approach ; Function to count the number of set bits in an integer N ; Stores the count of set bits ; Iterate until N is non - zero ; Stores the resultant count ; Function to calculate sum of all array elements whose count of set bits are unique ; Stores frequency of all possible count of set bits ; Stores the sum of array elements ; Traverse the array ; Count the number of set bits ; Traverse the array And Update the value of ans ; If frequency is 1 ; Driver Code
import java . util . * ; class GFG { static int setBitCount ( int n ) { int ans = 0 ; while ( n != 0 ) { ans += n & 1 ; n >>= 1 ; } return ans ; } static void getSum ( int [ ] arr , int n ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int key = setBitCount ( arr [ i ] ) ; if ( mp . containsKey ( key ) ) mp . put ( key , mp . get ( key ) + 1 ) ; else mp . put ( key , 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { int key = setBitCount ( arr [ i ] ) ; if ( mp . containsKey ( key ) && mp . get ( key ) == 1 ) ans += arr [ i ] ; } System . out . print ( ans ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 8 , 3 , 7 , 5 , 3 } ; int n = arr . length ; getSum ( arr , n ) ; } }
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | Java program for the above approach ; Function to check if array can be split into three equal sum subarrays by removing a pair ; Stores prefix sum array ; Copy array elements ; Traverse the array ; Stores sums of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no such pair exists , print - 1 ; Driver Code ; Given array ; Size of the array
import java . util . * ; class GFG { static void findSplit ( int arr [ ] , int N ) { int [ ] sum = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { sum [ i ] = arr [ i ] ; } for ( int i = 1 ; i < N ; i ++ ) { sum [ i ] += sum [ i - 1 ] ; } for ( int l = 1 ; l <= N - 4 ; l ++ ) { for ( int r = l + 2 ; r <= N - 2 ; r ++ ) { int lsum = 0 , rsum = 0 , msum = 0 ; lsum = sum [ l - 1 ] ; msum = sum [ r - 1 ] - sum [ l ] ; rsum = sum [ N - 1 ] - sum [ r ] ; if ( lsum == rsum && rsum == msum ) { System . out . print ( l + " ▁ " + r + "NEW_LINE"); return ; } } } System . out . print ( - 1 + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 12 , 7 , 19 , 4 , 3 } ; int N = arr . length ; findSplit ( arr , N ) ; } }
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | Java program for the above approach ; Function to check if array can be split into three equal sum subarrays by removing a pair ; Two pointers l and r ; Stores prefix sum array ; Traverse the array ; Two pointer approach ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Print split indices if sum is equal ; Move left pointer if lsum < rsum ; Move right pointer if rsum > lsum ; Move both pointers if lsum = rsum but they are not equal to msum ; If no possible pair exists , print - 1 ; Driver Code ; Given array ; Size of the array
public class GFG { static void findSplit ( int [ ] arr , int N ) { int l = 1 , r = N - 2 ; int lsum , msum , rsum ; int sum [ ] = new int [ N ] ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { sum [ i ] = sum [ i - 1 ] + arr [ i ] ; } while ( l < r ) { lsum = sum [ l - 1 ] ; msum = sum [ r - 1 ] - sum [ l ] ; rsum = sum [ N - 1 ] - sum [ r ] ; if ( lsum == msum && msum == rsum ) { System . out . println ( l + " ▁ " + r ) ; return ; } if ( lsum < rsum ) l ++ ; else if ( lsum > rsum ) r -- ; else { l ++ ; r -- ; } } System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 12 , 7 , 19 , 4 , 3 } ; int N = arr . length ; findSplit ( arr , N ) ; } }
Number of subtrees having odd count of even numbers | Java implementation to find number of subtrees having odd count of even numbers ; A binary tree Node ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ;  ; Returns count of subtrees having odd count of even numbers ; base condition ; count even nodes in left subtree ; Add even nodes in right subtree ; Check if root data is an even number ; if total count of even numbers for the subtree is odd ; Total count of even nodes of the subtree ; A wrapper over countRec ( ) ; Driver program to test above ; binary tree formation ; 2 ; / \ ; 1 3 ; / \ / \ ; 4 10 8 5 ; / ; 6
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 = null ; node . right = null ; return ( node ) ; } static class P { int pcount = 0 ; } static int countRec ( Node root , P p ) { if ( root == null ) return 0 ; int c = countRec ( root . left , p ) ; c += countRec ( root . right , p ) ; if ( root . data % 2 == 0 ) c += 1 ; if ( c % 2 != 0 ) ( p . pcount ) ++ ; return c ; } static int countSubtrees ( Node root ) { P p = new P ( ) ; countRec ( root , p ) ; return p . pcount ; } public static void main ( String [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 10 ) ; root . right . left = newNode ( 8 ) ; root . right . right = newNode ( 5 ) ; root . left . right . left = newNode ( 6 ) ; System . out . println ( " Count ▁ = ▁ " + countSubtrees ( root ) ) ; } }
Inorder Successor of a node in Binary Tree | Java program to find inorder successor of a node ; A Binary Tree Node ; Temporary node for case 2 ; Utility function to create a new tree node ; function to find left most node in a tree ; function to find right most node in a tree ; recursive function to find the Inorder Scuccessor when the right child of node x is null ; function to find inorder successor of a node ; Case1 : If right child is not null ; Case2 : If right child is null ; case3 : If x is the right most node ; Driver program to test above functions ; Case 1 ; case 2 ; case 3
class Solution { static class Node { int data ; Node left , right ; } static Node temp = new Node ( ) ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node leftMostNode ( Node node ) { while ( node != null && node . left != null ) node = node . left ; return node ; } static Node rightMostNode ( Node node ) { while ( node != null && node . right != null ) node = node . right ; return node ; } static Node findInorderRecursive ( Node root , Node x ) { if ( root == null ) return null ; if ( root == x || ( temp = findInorderRecursive ( root . left , x ) ) != null || ( temp = findInorderRecursive ( root . right , x ) ) != null ) { if ( temp != null ) { if ( root . left == temp ) { System . out . print ( " Inorder ▁ Successor ▁ of ▁ " + x . data ) ; System . out . print ( " ▁ is ▁ " + root . data + "NEW_LINE");  return null ; } } return root ; } return null ; } static void inorderSuccesor ( Node root , Node x ) { if ( x . right != null ) { Node inorderSucc = leftMostNode ( x . right ) ; System . out . print ( " Inorder ▁ Successor ▁ of ▁ " + x . data + " ▁ is ▁ " ) ; System . out . print ( inorderSucc . data + "NEW_LINE");  } if ( x . right == null ) { int f = 0 ; Node rightMost = rightMostNode ( root ) ; if ( rightMost == x ) System . out . print ( "No inorder successor! Right most node.NEW_LINE");  else findInorderRecursive ( root , x ) ; } } public static void main ( String args [ ] ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; inorderSuccesor ( root , root . right ) ; inorderSuccesor ( root , root . left . left ) ; inorderSuccesor ( root , root . right . right ) ; } }
Binary Tree | Set 1 ( Introduction ) | Class containing left and right child of current node and key value ; Binary Tree ; Driver code
class Node { int key ; Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } class BinaryTree { Node root ; BinaryTree ( int key ) { root = new Node ( key ) ; } BinaryTree ( ) { root = null ; } 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 ) ; } }
Find distance from root to given node in a binary tree | Java program to find distance of a given node from root . ; A Binary Tree Node ; A utility function to create a new Binary Tree Node ; Returns - 1 if x doesn 't exist in tree. Else returns distance of x from root ; Base case ; Initialize distance ; Check if x is present at root or in left subtree or right subtree . ; Driver Program to test above functions
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . left = null ; temp . right = null ; return temp ; } static int findDistance ( Node root , int x ) { if ( root == null ) return - 1 ; int dist = - 1 ; if ( ( root . data == x ) || ( dist = findDistance ( root . left , x ) ) >= 0 || ( dist = findDistance ( root . right , x ) ) >= 0 ) return dist + 1 ; return dist ; } public static void main ( String [ ] args ) { Node root = newNode ( 5 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 15 ) ; root . left . left = newNode ( 20 ) ; root . left . right = newNode ( 25 ) ; root . left . right . right = newNode ( 45 ) ; root . right . left = newNode ( 30 ) ; root . right . right = newNode ( 35 ) ; System . out . println ( findDistance ( root , 45 ) ) ; } }
Find right sibling of a binary tree with parent pointers | Java program to print right sibling of a node ; A Binary Tree Node ; Constructor ; Method to find right sibling ; GET Parent pointer whose right child is not a parent or itself of this node . There might be case when parent has no right child , but , current node is left child of the parent ( second condition is for that ) . ; Move to the required child , where right sibling can be present ; find right sibling in the given subtree ( from current node ) , when level will be 0 ; Iterate through subtree ; if no child are there , we cannot have right sibling in this path ; This is the case when we reach 9 node in the tree , where we need to again recursively find the right sibling ; Driver Program to test above functions ; passing 10
public class Right_Sibling { static class Node { int data ; Node left , right , parent ; public Node ( int data , Node parent ) { this . data = data ; left = null ; right = null ; this . parent = parent ; } } ; static Node findRightSibling ( Node node , int level ) { if ( node == null node . parent == null ) return null ; while ( node . parent . right == node || ( node . parent . right == null && node . parent . left == node ) ) { if ( node . parent == null ) return null ; node = node . parent ; level -- ; } node = node . parent . right ; while ( level < 0 ) { if ( node . left != null ) node = node . left ; else if ( node . right != null ) node = node . right ; else break ; level ++ ; } if ( level == 0 ) return node ; return findRightSibling ( node , level ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 , null ) ; root . left = new Node ( 2 , root ) ; root . right = new Node ( 3 , root ) ; root . left . left = new Node ( 4 , root . left ) ; root . left . right = new Node ( 6 , root . left ) ; root . left . left . left = new Node ( 7 , root . left . left ) ; root . left . left . left . left = new Node ( 10 , root . left . left . left ) ; root . left . right . right = new Node ( 9 , root . left . right ) ; root . right . right = new Node ( 5 , root . right ) ; root . right . right . right = new Node ( 8 , root . right . right ) ; root . right . right . right . right = new Node ( 12 , root . right . right . right ) ; System . out . println ( findRightSibling ( root . left . left . left . left , 0 ) . data ) ; } }