codeparrot/xlcost-text-to-code huggingface dataset:
Check whether a given binary tree is perfect or not | Java program to check whether a given Binary Tree is Perfect or not ; Tree node structure ; Returns depth of leftmost leaf . ; This function tests if a binary tree is perfect or not . It basically checks for two things : 1 ) All leaves are at same level 2 ) All internal nodes have two children ; An empty tree is perfect ; If leaf node , then its depth must be same as depth of all other leaves . ; If internal node and one child is empty ; Left and right subtrees must be perfect . ; Wrapper over isPerfectRec ( ) ; Helper function that allocates a new node with the given key and NULL left and right pointer . ; Driver Program
class GfG { static class Node { int key ; Node left , right ; } static int findADepth ( Node node ) { int d = 0 ; while ( node != null ) { d ++ ; node = node . left ; } return d ; } static boolean isPerfectRec ( Node root , int d , int level ) { if ( root == null ) return true ; if ( root . left == null && root . right == null ) return ( d == level + 1 ) ; if ( root . left == null root . right == null ) return false ; return isPerfectRec ( root . left , d , level + 1 ) && isPerfectRec ( root . right , d , level + 1 ) ; } static boolean isPerfect ( Node root ) { int d = findADepth ( root ) ; return isPerfectRec ( root , d , 0 ) ; } static Node newNode ( int k ) { Node node = new Node ( ) ; node . key = k ; node . right = null ; node . left = null ; return node ; } public static void main ( String args [ ] ) { Node root = null ; root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; root . right . left = newNode ( 60 ) ; root . right . right = newNode ( 70 ) ; if ( isPerfect ( root ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find pair with greatest product in array | Java program to find a pair with product in given array . ; Function to find greatest number that us ; Driver code
import java . io . * ; class GFG { static int findGreatest ( int [ ] arr , int n ) { int result = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ j ] * arr [ k ] == arr [ i ] ) result = Math . max ( result , arr [ i ] ) ; return result ; } static public void main ( String [ ] args ) { int [ ] arr = { 30 , 10 , 9 , 3 , 35 } ; int n = arr . length ; System . out . println ( findGreatest ( arr , n ) ) ; } }
Find pair with greatest product in array | Java program to find the largest product number ; Function to find greatest number ; Store occurrences of all elements in hash array ; m [ arr [ i ] ] ++ ; Sort the array and traverse all elements from end . ; For every element , check if there is another element which divides it . ; Check if the result value exists in array or not if yes the return arr [ i ] ; To handle the case like arr [ i ] = 4 and arr [ j ] = 2 ; Driver code
import java . util . * ; class GFG { static int findGreatest ( int arr [ ] , int n ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , m . get ( arr [ i ] ) ) ; } } Arrays . sort ( arr ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= Math . sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 ) { int result = arr [ i ] / arr [ j ] ; if ( result != arr [ j ] && m . get ( result ) == null || m . get ( result ) > 0 ) { return arr [ i ] ; } else if ( result == arr [ j ] && m . get ( result ) > 1 ) { return arr [ i ] ; } } } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 17 , 2 , 1 , 15 , 30 } ; int n = arr . length ; System . out . println ( findGreatest ( arr , n ) ) ; } }
Remove minimum number of elements such that no common element exist in both array | JAVA Code to Remove minimum number of elements such that no common element exist in both array ; To find no elements to remove so no common element exist ; To store count of array element ; Count elements of a ; Count elements of b ; Traverse through all common element , and pick minimum occurrence from two arrays ; To return count of minimum elements ; Driver program to test above function
import java . util . * ; class GFG { public static int minRemove ( int a [ ] , int b [ ] , int n , int m ) { HashMap < Integer , Integer > countA = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > countB = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( countA . containsKey ( a [ i ] ) ) countA . put ( a [ i ] , countA . get ( a [ i ] ) + 1 ) ; else countA . put ( a [ i ] , 1 ) ; } for ( int i = 0 ; i < m ; i ++ ) { if ( countB . containsKey ( b [ i ] ) ) countB . put ( b [ i ] , countB . get ( b [ i ] ) + 1 ) ; else countB . put ( b [ i ] , 1 ) ; } int res = 0 ; Set < Integer > s = countA . keySet ( ) ; for ( int x : s ) if ( countB . containsKey ( x ) ) res += Math . min ( countB . get ( x ) , countA . get ( x ) ) ; return res ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 } ; int b [ ] = { 2 , 3 , 4 , 5 , 8 } ; int n = a . length ; int m = b . length ; System . out . println ( minRemove ( a , b , n , m ) ) ; } }
Count items common to both the lists but with different prices | Java implementation to count items common to both the lists but with different prices ; details of an item ; function to count items common to both the lists but with different prices ; for each item of ' list1' check if it is in ' list2' but with a different price ; required count of items ; Driver code
class GFG { static class item { String name ; int price ; public item ( String name , int price ) { this . name = name ; this . price = price ; } } ; static int countItems ( item list1 [ ] , int m , item list2 [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( list1 [ i ] . name . compareTo ( list2 [ j ] . name ) == 0 ) && ( list1 [ i ] . price != list2 [ j ] . price ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { item list1 [ ] = { new item ( " apple " , 60 ) , new item ( " bread " , 20 ) , new item ( " wheat " , 50 ) , new item ( " oil " , 30 ) } ; item list2 [ ] = { new item ( " milk " , 20 ) , new item ( " bread " , 15 ) , new item ( " wheat " , 40 ) , new item ( " apple " , 60 ) } ; int m = list1 . length ; int n = list2 . length ; System . out . print ( " Count ▁ = ▁ " + countItems ( list1 , m , list2 , n ) ) ; } }
Count items common to both the lists but with different prices | Java implementation to count items common to both the lists but with different prices ; Details of an item ; comparator function used for sorting ; Function to search ' str ' in ' list2 [ ] ' . If it exists then price associated with ' str ' in ' list2 [ ] ' is being returned else - 1 is returned . Here binary search technique is being applied for searching ; if true the item ' str ' is in ' list2' ; item ' str ' is not in ' list2' ; Function to count items common to both the lists but with different prices ; sort ' list2' in alphabetcal order of items name ; initial count ; get the price of item ' list1 [ i ] ' from ' list2' if item in not present in second list then - 1 is being obtained ; if item is present in list2 with a different price ; Required count of items ; Driver code
import java . util . * ; class GFG { static class item { String name ; int price ; item ( String name , int price ) { this . name = name ; this . price = price ; } } ; static class Com implements Comparator < item > { public int compare ( item a , item b ) { return a . name . compareTo ( b . name ) ; } } static int binary_search ( item list2 [ ] , int low , int high , String str ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( list2 [ mid ] . name . compareTo ( str ) == 0 ) return list2 [ mid ] . price ; else if ( list2 [ mid ] . name . compareTo ( str ) < 0 ) low = mid + 1 ; else high = mid - 1 ; } return - 1 ; } static int countItems ( item list1 [ ] , int m , item list2 [ ] , int n ) { Arrays . sort ( list2 , new Com ( ) ) ; int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int r = binary_search ( list2 , 0 , n - 1 , list1 [ i ] . name ) ; if ( ( r != - 1 ) && ( r != list1 [ i ] . price ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { item [ ] list1 = { new item ( " apple " , 60 ) , new item ( " bread " , 20 ) , new item ( " wheat " , 50 ) , new item ( " oil " , 30 ) } ; item list2 [ ] = { new item ( " milk " , 20 ) , new item ( " bread " , 15 ) , new item ( " wheat " , 40 ) , new item ( " apple " , 60 ) } ; int m = list1 . length ; int n = list2 . length ; System . out . print ( " Count ▁ = ▁ " + countItems ( list1 , m , list2 , n ) ) ; } }
Count items common to both the lists but with different prices | Java implementation to count items common to both the lists but with different prices ; details of an item ; function to count items common to both the lists but with different prices ; ' um ' implemented as hash table that contains item name as the key and price as the value associated with the key ; insert elements of ' list1' in ' um ' ; for each element of ' list2' check if it is present in ' um ' with a different price value ; required count of items ; Driver program to test above
import java . util . * ; class GFG { static class item { String name ; int price ; public item ( String name , int price ) { this . name = name ; this . price = price ; } } ; static int countItems ( item list1 [ ] , int m , item list2 [ ] , int n ) { HashMap < String , Integer > um = new HashMap < > ( ) ; int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) um . put ( list1 [ i ] . name , list1 [ i ] . price ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( um . containsKey ( list2 [ i ] . name ) ) && ( um . get ( list2 [ i ] . name ) != list2 [ i ] . price ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { item list1 [ ] = { new item ( " apple " , 60 ) , new item ( " bread " , 20 ) , new item ( " wheat " , 50 ) , new item ( " oil " , 30 ) } ; item list2 [ ] = { new item ( " milk " , 20 ) , new item ( " bread " , 15 ) , new item ( " wheat " , 40 ) , new item ( " apple " , 60 ) } ; int m = list1 . length ; int n = list2 . length ; System . out . print ( " Count ▁ = ▁ " + countItems ( list1 , m , clist2 , n ) ) ; } }
Minimum Index Sum for Common Elements of Two Lists |  ; Function to print common Strings with minimum index sum ; resultant list ; iterating over sum in ascending order ; iterating over one list and check index ( Corresponding to given sum ) in other list ; put common Strings in resultant list ; if common String found then break as we are considering index sums in increasing order . ; print the resultant list ; Driver code ; Creating list1 ; Creating list2
import java . util . * ; class GFG { static void find ( Vector < String > list1 , Vector < String > list2 ) { Vector < String > res = new Vector < > ( ) ; int max_possible_sum = list1 . size ( ) + list2 . size ( ) - 2 ; for ( int sum = 0 ; sum <= max_possible_sum ; sum ++ ) { for ( int i = 0 ; i <= sum ; i ++ ) if ( i < list1 . size ( ) && ( sum - i ) < list2 . size ( ) && list1 . get ( i ) == list2 . get ( sum - i ) ) res . add ( list1 . get ( i ) ) ; if ( res . size ( ) > 0 ) break ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { Vector < String > list1 = new Vector < > ( ) ; list1 . add ( " GeeksforGeeks " ) ; list1 . add ( " Udemy " ) ; list1 . add ( " Coursera " ) ; list1 . add ( " edX " ) ; Vector < String > list2 = new Vector < > ( ) ; list2 . add ( " Codecademy " ) ; list2 . add ( " Khan ▁ Academy " ) ; list2 . add ( " GeeksforGeeks " ) ; find ( list1 , list2 ) ; } }
Minimum Index Sum for Common Elements of Two Lists | Hashing based Java program to find common elements with minimum index sum . ; Function to print common Strings with minimum index sum ; mapping Strings to their indices ; resultant list ; If current sum is smaller than minsum ; if index sum is same then put this String in resultant list as well ; Print result ; Driver code ; Creating list1 ; Creating list2
import java . util . * ; class GFG { static void find ( Vector < String > list1 , Vector < String > list2 ) { Map < String , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < list1 . size ( ) ; i ++ ) map . put ( list1 . get ( i ) , i ) ; Vector < String > res = new Vector < String > ( ) ; int minsum = Integer . MAX_VALUE ; for ( int j = 0 ; j < list2 . size ( ) ; j ++ ) { if ( map . containsKey ( list2 . get ( j ) ) ) { int sum = j + map . get ( list2 . get ( j ) ) ; if ( sum < minsum ) { minsum = sum ; res . clear ( ) ; res . add ( list2 . get ( j ) ) ; } else if ( sum == minsum ) res . add ( list2 . get ( j ) ) ; } } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { Vector < String > list1 = new Vector < String > ( ) ; list1 . add ( " GeeksforGeeks " ) ; list1 . add ( " Udemy " ) ; list1 . add ( " Coursera " ) ; list1 . add ( " edX " ) ; Vector < String > list2 = new Vector < String > ( ) ; list2 . add ( " Codecademy " ) ; list2 . add ( " Khan ▁ Academy " ) ; list2 . add ( " GeeksforGeeks " ) ; find ( list1 , list2 ) ; } }
Check whether a binary tree is a full binary tree or not | Tree node structure ; this function checks if a binary tree is full or not ; if empty tree ; if leaf node ; if both left and right subtrees are not null the are full ; if none work ; Driver program
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; boolean isFullTree ( Node node ) { if ( node == null ) return true ; if ( node . left == null && node . right == null ) return true ; if ( ( node . left != null ) && ( node . right != null ) ) return ( isFullTree ( node . left ) && isFullTree ( node . right ) ) ; return false ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 20 ) ; tree . root . right = new Node ( 30 ) ; tree . root . left . right = new Node ( 40 ) ; tree . root . left . left = new Node ( 50 ) ; tree . root . right . left = new Node ( 60 ) ; tree . root . left . left . left = new Node ( 80 ) ; tree . root . right . right = new Node ( 70 ) ; tree . root . left . left . right = new Node ( 90 ) ; tree . root . left . right . left = new Node ( 80 ) ; tree . root . left . right . right = new Node ( 90 ) ; tree . root . right . left . left = new Node ( 80 ) ; tree . root . right . left . right = new Node ( 90 ) ; tree . root . right . right . left = new Node ( 80 ) ; tree . root . right . right . right = new Node ( 90 ) ; if ( tree . isFullTree ( tree . root ) ) System . out . print ( " The ▁ binary ▁ tree ▁ is ▁ full " ) ; else System . out . print ( " The ▁ binary ▁ tree ▁ is ▁ not ▁ full " ) ; } }
Change the array into a permutation of numbers from 1 to n | Java program to make a permutation of numbers from 1 to n using minimum changes . ; Store counts of all elements . ; Find next missing element to put in place of current element . ; Replace with next missing and insert the missing element in hash . ; Driver Code
import java . util . * ; class GFG { static void makePermutation ( int [ ] a , int n ) { HashMap < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . containsKey ( a [ i ] ) ) { count . put ( a [ i ] , count . get ( a [ i ] ) + 1 ) ; } else { count . put ( a [ i ] , 1 ) ; } } int next_missing = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . containsKey ( a [ i ] ) && count . get ( a [ i ] ) != 1 a [ i ] > n a [ i ] < 1 ) { count . put ( a [ i ] , count . get ( a [ i ] ) - 1 ) ; while ( count . containsKey ( next_missing ) ) next_missing ++ ; a [ i ] = next_missing ; count . put ( next_missing , 1 ) ; } } } public static void main ( String [ ] args ) { int A [ ] = { 2 , 2 , 3 , 3 } ; int n = A . length ; makePermutation ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; } }
Count pairs with given sum | Java implementation of simple method to find count of pairs with given sum . ; Prints number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Initialize result ; Consider all possible pairs and check their sums ; Driver function to test the above function
public class find { public static void getPairsCount ( int [ ] arr , int sum ) { int count = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) for ( int j = i + 1 ; j < arr . length ; j ++ ) if ( ( arr [ i ] + arr [ j ] ) == sum ) count ++ ; System . out . printf ( " Count ▁ of ▁ pairs ▁ is ▁ % d " , count ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 5 , 7 , - 1 , 5 } ; int sum = 6 ; getPairsCount ( arr , sum ) ; } }
Check whether a binary tree is a full binary tree or not | Iterative Approach | Java implementation to check whether a binary tree is a full binary tree or not ; structure of a node of binary tree ; function to get a new node ; allocate space ; put in the data ; function to check whether a binary tree is a full binary tree or not ; if tree is empty ; queue used for level order traversal ; push ' root ' to ' q ' ; traverse all the nodes of the binary tree level by level until queue is empty ; get the pointer to ' node ' at front of queue ; if it is a leaf node then continue ; if either of the child is not null and the other one is null , then binary tree is not a full binary tee ; push left and right childs of ' node ' on to the queue ' q ' ; binary tree is a full binary tee ; Driver program to test above
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return newNode ; } static boolean isFullBinaryTree ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { Node node = q . peek ( ) ; q . remove ( ) ; if ( node . left == null && node . right == null ) continue ; if ( node . left == null node . right == null ) return false ; q . add ( node . left ) ; q . add ( node . right ) ; } return true ; } public static void main ( String [ ] args ) { Node root = getNode ( 1 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 3 ) ; root . left . left = getNode ( 4 ) ; root . left . right = getNode ( 5 ) ; if ( isFullBinaryTree ( root ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Analysis of Algorithms | Big | Java program for the above approach ; Function to find whether a key exists in an array or not using linear search ; Traverse the given array , a [ ] ; Check if a [ i ] is equal to key ; Driver code ; Given Input ; Function Call
import java . lang . * ; import java . util . * ; class GFG { static boolean linearSearch ( int a [ ] , int n , int key ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == key ) return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int n = arr . length ; if ( linearSearch ( arr , n , x ) ) System . out . println ( " Element ▁ is ▁ present ▁ in ▁ array " ) ; else System . out . println ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; } }
Count pairs with given sum | Java implementation of simple method to find count of pairs with given sum ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Store counts of all elements in map hm ; initializing value to 0 , if key not found ; iterate through each element and increment the count ( Notice that every pair is counted twice ) ; if ( arr [ i ] , arr [ i ] ) pair satisfies the condition , then we need to ensure that the count is decreased by one such that the ( arr [ i ] , arr [ i ] ) pair is not considered ; return the half of twice_count ; Driver method to test the above function
import java . util . HashMap ; class Test { static int arr [ ] = new int [ ] { 1 , 5 , 7 , - 1 , 5 } ; static int getPairsCount ( int n , int sum ) { HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , 0 ) ; hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; } int twice_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . get ( sum - arr [ i ] ) != null ) twice_count += hm . get ( sum - arr [ i ] ) ; if ( sum - arr [ i ] == arr [ i ] ) twice_count -- ; } return twice_count / 2 ; } public static void main ( String [ ] args ) { int sum = 6 ; System . out . println ( " Count ▁ of ▁ pairs ▁ is ▁ " + getPairsCount ( arr . length , sum ) ) ; } }
Count pairs from two sorted arrays whose sum is equal to a given value x | Java implementation to count pairs from both sorted arrays whose sum is equal to a given value ; function to count all pairs from both the sorted arrays whose sum is equal to a given value ; generating pairs from both the arrays ; if sum of pair is equal to ' x ' increment count ; required count of pairs ; Driver Code
import java . io . * ; class GFG { static int countPairs ( int [ ] arr1 , int [ ] arr2 , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++ ; return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }
Count pairs from two sorted arrays whose sum is equal to a given value x | Java implementation to count pairs from both sorted arrays whose sum is equal to a given value ; function to search ' value ' in the given array ' arr [ ] ' it uses binary search technique as ' arr [ ] ' is sorted ; value found ; value not found ; function to count all pairs from both the sorted arrays whose sum is equal to a given value ; for each arr1 [ i ] ; check if the ' value ' is present in ' arr2 [ ] ' ; required count of pairs ; Driver Code
import java . io . * ; class GFG { static boolean isPresent ( int arr [ ] , int low , int high , int value ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == value ) return true ; else if ( arr [ mid ] > value ) high = mid - 1 ; else low = mid + 1 ; } return false ; } static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int value = x - arr1 [ i ] ; if ( isPresent ( arr2 , 0 , n - 1 , value ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }