codeparrot/xlcost-text-to-code huggingface dataset:
Check if leaf traversal of two Binary Trees is same ? | Java program to check if two Leaf Traversal of Two Binary Trees is same or not ; Binary Tree node ; checks if a given node is leaf or not . ; Returns true of leaf traversal of two trees is same , else false ; Create empty stacks . These stacks are going to be used for iterative traversals . ; Loop until either of two stacks is not empty ; If one of the stacks is empty means other stack has extra leaves so return false ; Push right and left children of temp1 . Note that right child is inserted before left ; same for tree2 ; If one is null and other is not , then return false ; If both are not null and data is not same return false ; If control reaches this point , all leaves are matched ; Driver code ; Let us create trees in above example 1
import java . util . * ; import java . lang . * ; import java . io . * ; class Node { int data ; Node left , right ; public Node ( int x ) { data = x ; left = right = null ; } public boolean isLeaf ( ) { return ( left == null && right == null ) ; } } class LeafOrderTraversal { public static boolean isSame ( Node root1 , Node root2 ) { Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; s1 . push ( root1 ) ; s2 . push ( root2 ) ; while ( ! s1 . empty ( ) || ! s2 . empty ( ) ) { if ( s1 . empty ( ) || s2 . empty ( ) ) return false ; Node temp1 = s1 . pop ( ) ; while ( temp1 != null && ! temp1 . isLeaf ( ) ) { if ( temp1 . right != null ) s1 . push ( temp1 . right ) ; if ( temp1 . left != null ) s1 . push ( temp1 . left ) ; temp1 = s1 . pop ( ) ; } Node temp2 = s2 . pop ( ) ; while ( temp2 != null && ! temp2 . isLeaf ( ) ) { if ( temp2 . right != null ) s2 . push ( temp2 . right ) ; if ( temp2 . left != null ) s2 . push ( temp2 . left ) ; temp2 = s2 . pop ( ) ; } if ( temp1 == null && temp2 != null ) return false ; if ( temp1 != null && temp2 == null ) return false ; if ( temp1 != null && temp2 != null ) { if ( temp1 . data != temp2 . data ) return false ; } } return true ; } public static void main ( String [ ] args ) { Node root1 = new Node ( 1 ) ; root1 . left = new Node ( 2 ) ; root1 . right = new Node ( 3 ) ; root1 . left . left = new Node ( 4 ) ; root1 . right . left = new Node ( 6 ) ; root1 . right . right = new Node ( 7 ) ; Node root2 = new Node ( 0 ) ; root2 . left = new Node ( 1 ) ; root2 . right = new Node ( 5 ) ; root2 . left . right = new Node ( 4 ) ; root2 . right . left = new Node ( 6 ) ; root2 . right . right = new Node ( 7 ) ; if ( isSame ( root1 , root2 ) ) System . out . println ( " Same " ) ; else System . out . println ( " Not ▁ Same " ) ; } }
Most frequent element in an array | Java program to find the most frequent element in an array ; Sort the array ; find the max frequency using linear traversal ; If last element is most frequent ; Driver program
import java . util . * ; class GFG { static int mostFrequent ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int max_count = 1 , res = arr [ 0 ] ; int curr_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . length ; System . out . println ( mostFrequent ( arr , n ) ) ; } }
Most frequent element in an array | Java program to find the most frequent element in an array ; Insert all elements in hash ; find max frequency . ; Driver code
import java . util . HashMap ; import java . util . Map ; import java . util . Map . Entry ; class GFG { static int mostFrequent ( int arr [ ] , int n ) { Map < Integer , Integer > hp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int key = arr [ i ] ; if ( hp . containsKey ( key ) ) { int freq = hp . get ( key ) ; freq ++ ; hp . put ( key , freq ) ; } else { hp . put ( key , 1 ) ; } } int max_count = 0 , res = - 1 ; for ( Entry < Integer , Integer > val : hp . entrySet ( ) ) { if ( max_count < val . getValue ( ) ) { res = val . getKey ( ) ; max_count = val . getValue ( ) ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . length ; System . out . println ( mostFrequent ( arr , n ) ) ; } }
Smallest subarray with all occurrences of a most frequent element | Java implementation to find smallest subarray with all occurrences of a most frequent element ; To store left most occurrence of elements ; To store counts of elements ; To store maximum frequency ; To store length and starting index of smallest result window ; First occurrence of an element , store the index ; increase the frequency of elements ; Find maximum repeated element and store its last occurrence and first occurrence ; length of subsegment ; select subsegment of smallest size ; Print the subsegment with all occurrences of a most frequent element ; Driver program
import java . io . * ; import java . util . * ; class GfG { static void smallestSubsegment ( int a [ ] , int n ) { HashMap < Integer , Integer > left = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; int mx = 0 ; int mn = - 1 , strindex = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( count . get ( x ) == null ) { left . put ( x , i ) ; count . put ( x , 1 ) ; } else count . put ( x , count . get ( x ) + 1 ) ; if ( count . get ( x ) > mx ) { mx = count . get ( x ) ; mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } else if ( ( count . get ( x ) == mx ) && ( i - left . get ( x ) + 1 < mn ) ) { mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } } for ( int i = strindex ; i < strindex + mn ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 2 , 2 , 1 } ; int n = A . length ; smallestSubsegment ( A , n ) ; } }
Given an array of pairs , find all symmetric pairs in it | A Java program to find all symmetric pairs in a given array of pairs ; Print all pairs that have a symmetric counterpart ; Creates an empty hashMap hM ; Traverse through the given array ; First and second elements of current pair ; Look for second element of this pair in hash ; If found and value in hash matches with first element of this pair , we found symmetry ; Else put sec element of this pair in hash ; Driver method
import java . util . HashMap ; class SymmetricPairs { static void findSymPairs ( int arr [ ] [ ] ) { HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { int first = arr [ i ] [ 0 ] ; int sec = arr [ i ] [ 1 ] ; Integer val = hM . get ( sec ) ; if ( val != null && val == first ) System . out . println ( " ( " + sec + " , ▁ " + first + " ) " ) ; else hM . put ( first , sec ) ; } } public static void main ( String arg [ ] ) { int arr [ ] [ ] = new int [ 5 ] [ 2 ] ; arr [ 0 ] [ 0 ] = 11 ; arr [ 0 ] [ 1 ] = 20 ; arr [ 1 ] [ 0 ] = 30 ; arr [ 1 ] [ 1 ] = 40 ; arr [ 2 ] [ 0 ] = 5 ; arr [ 2 ] [ 1 ] = 10 ; arr [ 3 ] [ 0 ] = 40 ; arr [ 3 ] [ 1 ] = 30 ; arr [ 4 ] [ 0 ] = 10 ; arr [ 4 ] [ 1 ] = 5 ; findSymPairs ( arr ) ; } }
Find any one of the multiple repeating elements in read only array | Java program to find one of the repeating elements in a read only array ; Function to find one of the repeating elements ; Size of blocks except the last block is sq ; Number of blocks to incorporate 1 to n values blocks are numbered from 0 to range - 1 ( both included ) ; Count array maintains the count for all blocks ; Traversing the read only array and updating count ; arr [ i ] belongs to block number ( arr [ i ] - 1 ) / sq i is considered to start from 0 ; The selected_block is set to last block by default . Rest of the blocks are checked ; after finding block with size > sq method of hashing is used to find the element repeating in this block ; checks if the element belongs to the selected_block ; repeating element found ; return - 1 if no repeating element exists ; Driver code ; read only array , not to be modified ; array of size 6 ( n + 1 ) having elements between 1 and 5
import java . io . * ; import java . util . * ; class GFG { static int findRepeatingNumber ( int [ ] arr , int n ) { int sq = ( int ) Math . sqrt ( n ) ; int range = ( n / sq ) + 1 ; int [ ] count = new int [ range ] ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i < range - 1 ; i ++ ) { if ( count [ i ] > sq ) { selected_block = i ; break ; } } HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ( ( selected_block * sq ) < arr [ i ] ) && ( arr [ i ] <= ( ( selected_block + 1 ) * sq ) ) ) { m . put ( arr [ i ] , 1 ) ; if ( m . get ( arr [ i ] ) == 1 ) return arr [ i ] ; } } return - 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 1 , 2 , 3 , 5 , 4 } ; int n = 5 ; System . out . println ( " One ▁ of ▁ the ▁ numbers ▁ repeated ▁ in ▁ the ▁ array ▁ is : ▁ " + findRepeatingNumber ( arr , n ) ) ; } }
Find top three repeated in array | Java Program to Find the top three repeated numbers ; User defined Pair class ; Function to print top three repeated numbers ; There should be atleast two elements ; Count Frequency of each element ; Initialize first value of each variable of Pair type is INT_MIN ; If frequency of current element is not zero and greater than frequency of first largest element ; Update second and third largest ; Modify values of x Number ; If frequency of current element is not zero and frequency of current element is less than frequency of first largest element , but greater than y element ; Modify values of third largest ; Modify values of second largest ; If frequency of current element is not zero and frequency of current element is less than frequency of first element and second largest , but greater than third largest . ; Modify values of z Number ; Driver 's Code
import java . io . * ; import java . util . * ; class Pair { int first , second ; } class GFG { static void top3Repeated ( int [ ] arr , int n ) { if ( n < 3 ) { System . out . print ( " Invalid ▁ Input " ) ; return ; } TreeMap < Integer , Integer > freq = new TreeMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( freq . containsKey ( arr [ i ] ) ) freq . put ( arr [ i ] , 1 + freq . get ( arr [ i ] ) ) ; else freq . put ( arr [ i ] , 1 ) ; Pair x = new Pair ( ) ; Pair y = new Pair ( ) ; Pair z = new Pair ( ) ; x . first = y . first = z . first = Integer . MIN_VALUE ; for ( Map . Entry curr : freq . entrySet ( ) ) { if ( Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) > x . first ) { z . first = y . first ; z . second = y . second ; y . first = x . first ; y . second = x . second ; x . first = Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) ; x . second = Integer . parseInt ( String . valueOf ( curr . getKey ( ) ) ) ; } else if ( Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) > y . first ) { z . first = y . first ; z . second = y . second ; y . first = Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) ; y . second = Integer . parseInt ( String . valueOf ( curr . getKey ( ) ) ) ; } else if ( Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) > z . first ) { z . first = Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) ; z . second = Integer . parseInt ( String . valueOf ( curr . getKey ( ) ) ) ; } } System . out . print ( " Three ▁ largest ▁ elements ▁ are ▁ " + x . second + " ▁ " + y . second + " ▁ " + z . second ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 3 , 4 , 2 , 3 , 16 , 3 , 15 , 16 , 15 , 15 , 16 , 2 , 3 } ; int n = arr . length ; top3Repeated ( arr , n ) ; } }
Group multiple occurrence of array elements ordered by first occurrence | A simple Java program to group multiple occurrences of individual array elements ; A simple method to group all occurrences of individual elements ; Initialize all elements as not visited ; Traverse all elements ; Check if this is first occurrence ; If yes , print it and all subsequent occurrences ; Driver code
class GFG { static void groupElements ( int arr [ ] , int n ) { boolean visited [ ] = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { visited [ i ] = false ; } for ( int i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { System . out . print ( arr [ i ] + " ▁ " ) ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { System . out . print ( arr [ i ] + " ▁ " ) ; visited [ j ] = true ; } } } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 9 , 2 , 3 , 4 , 9 , 6 , 10 , 4 } ; int n = arr . length ; groupElements ( arr , n ) ; } }
Group multiple occurrence of array elements ordered by first occurrence | Java program to group multiple occurrences of individual array elements ; A hashing based method to group all occurrences of individual elements ; Creates an empty hashmap ; Traverse the array elements , and store count for every element in HashMap ; Check if element is already in HashMap ; Increment count of element element in HashMap ; Traverse array again ; Check if this is first occurrence ; If yes , then print the element ' count ' times ; And remove the element from HashMap . ; Driver method to test above method
import java . util . HashMap ; class Main { static void orderedGroup ( int arr [ ] ) { HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { Integer prevCount = hM . get ( arr [ i ] ) ; if ( prevCount == null ) prevCount = 0 ; hM . put ( arr [ i ] , prevCount + 1 ) ; } for ( int i = 0 ; i < arr . length ; i ++ ) { Integer count = hM . get ( arr [ i ] ) ; if ( count != null ) { for ( int j = 0 ; j < count ; j ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; hM . remove ( arr [ i ] ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 3 , 10 , 10 , 4 , 1 , 3 } ; orderedGroup ( arr ) ; } }
How to check if two given sets are disjoint ? | Java program to check if two sets are disjoint ; Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Take every element of set1 [ ] and search it in set2 ; If no element of set1 is present in set2 ; Driver program to test above function
public class disjoint1 { boolean aredisjoint ( int set1 [ ] , int set2 [ ] ) { for ( int i = 0 ; i < set1 . length ; i ++ ) { for ( int j = 0 ; j < set2 . length ; j ++ ) { if ( set1 [ i ] == set2 [ j ] ) return false ; } } return true ; } public static void main ( String [ ] args ) { disjoint1 dis = new disjoint1 ( ) ; int set1 [ ] = { 12 , 34 , 11 , 9 , 3 } ; int set2 [ ] = { 7 , 2 , 1 , 5 } ; boolean result = dis . aredisjoint ( set1 , set2 ) ; if ( result ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
How to check if two given sets are disjoint ? | Java program to check if two sets are disjoint ; Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Sort the given two sets ; Check for same elements using merge like process ; if set1 [ i ] == set2 [ j ] ; Driver program to test above function
import java . util . Arrays ; public class disjoint1 { boolean aredisjoint ( int set1 [ ] , int set2 [ ] ) { int i = 0 , j = 0 ; Arrays . sort ( set1 ) ; Arrays . sort ( set2 ) ; while ( i < set1 . length && j < set2 . length ) { if ( set1 [ i ] < set2 [ j ] ) i ++ ; else if ( set1 [ i ] > set2 [ j ] ) j ++ ; else return false ; } return true ; } public static void main ( String [ ] args ) { disjoint1 dis = new disjoint1 ( ) ; int set1 [ ] = { 12 , 34 , 11 , 9 , 3 } ; int set2 [ ] = { 7 , 2 , 1 , 5 } ; boolean result = dis . aredisjoint ( set1 , set2 ) ; if ( result ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
How to check if two given sets are disjoint ? | Java program to check if two sets are distinct or not ; This function prints all distinct elements ; Creates an empty hashset ; Traverse the first set and store its elements in hash ; Traverse the second set and check if any element of it is already in hash or not . ; Driver method to test above method
import java . util . * ; class Main { static boolean areDisjoint ( int set1 [ ] , int set2 [ ] ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < set1 . length ; i ++ ) set . add ( set1 [ i ] ) ; for ( int i = 0 ; i < set2 . length ; i ++ ) if ( set . contains ( set2 [ i ] ) ) return false ; return true ; } public static void main ( String [ ] args ) { int set1 [ ] = { 10 , 5 , 3 , 4 , 6 } ; int set2 [ ] = { 8 , 7 , 9 , 3 } ; if ( areDisjoint ( set1 , set2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Non | Java program to find Non - overlapping sum ; function for calculating Non - overlapping sum of two array ; Insert elements of both arrays ; calculate non - overlapped sum ; Driver code ; size of array ; function call
import java . io . * ; import java . util . * ; class GFG { static int findSum ( int [ ] A , int [ ] B , int n ) { HashMap < Integer , Integer > hash = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . containsKey ( A [ i ] ) ) hash . put ( A [ i ] , 1 + hash . get ( A [ i ] ) ) ; else hash . put ( A [ i ] , 1 ) ; if ( hash . containsKey ( B [ i ] ) ) hash . put ( B [ i ] , 1 + hash . get ( B [ i ] ) ) ; else hash . put ( B [ i ] , 1 ) ; } int sum = 0 ; for ( Map . Entry entry : hash . entrySet ( ) ) { if ( Integer . parseInt ( ( entry . getValue ( ) ) . toString ( ) ) == 1 ) sum += Integer . parseInt ( ( entry . getKey ( ) ) . toString ( ) ) ; } return sum ; } public static void main ( String args [ ] ) { int [ ] A = { 5 , 4 , 9 , 2 , 3 } ; int [ ] B = { 2 , 8 , 7 , 6 , 3 } ; int n = A . length ; System . out . println ( findSum ( A , B , n ) ) ; } }
Find elements which are present in first array and not in second | Java simple program to find elements which are not present in second array ; Function for finding elements which are there in a [ ] but not in b [ ] . ; Driver Code
class GFG { static void findMissing ( int a [ ] , int b [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < m ; j ++ ) if ( a [ i ] == b [ j ] ) break ; if ( j == m ) System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 6 , 3 , 4 , 5 } ; int b [ ] = { 2 , 4 , 3 , 1 , 0 } ; int n = a . length ; int m = b . length ; findMissing ( a , b , n , m ) ; } }
Find elements which are present in first array and not in second | Java efficient program to find elements which are not present in second array ; Function for finding elements which are there in a [ ] but not in b [ ] . ; Store all elements of second array in a hash table ; Print all elements of first array that are not present in hash table ; Driver code
import java . util . HashSet ; import java . util . Set ; public class GfG { static void findMissing ( int a [ ] , int b [ ] , int n , int m ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 0 ; i < m ; i ++ ) s . add ( b [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ! s . contains ( a [ i ] ) ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 6 , 3 , 4 , 5 } ; int b [ ] = { 2 , 4 , 3 , 1 , 0 } ; int n = a . length ; int m = b . length ; findMissing ( a , b , n , m ) ; } }
Check if two arrays are equal or not | Java program to find given two array are equal or not ; Returns true if arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] contain same elements . ; If lengths of array are not equal means array are not equal ; Sort both arrays ; Linearly compare elements ; If all elements were same . ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static boolean areEqual ( int arr1 [ ] , int arr2 [ ] ) { int n = arr1 . length ; int m = arr2 . length ; if ( n != m ) return false ; Arrays . sort ( arr1 ) ; Arrays . sort ( arr2 ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 3 , 5 , 2 , 5 , 2 } ; int arr2 [ ] = { 2 , 3 , 5 , 5 , 2 } ; if ( areEqual ( arr1 , arr2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Pair with given sum and maximum shortest distance from end | Java code to find maximum shortest distance from endpoints ; Store counts of all elements . ; function to find maximum shortest distance ; stores the shortest distance of every element in original array . ; shortest distance from ends ; if duplicates are found , b [ x ] is replaced with minimum of the previous and current position 's  shortest distance ; similar elements ignore them cause we need distinct elements ; 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 ) ; } } } static int find_maximum ( int a [ ] , int n , int k ) { HashMap < Integer , Integer > b = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; int d = Math . min ( 1 + i , n - i ) ; if ( ! b . containsKey ( x ) ) b . put ( x , d ) ; else { b . put ( x , Math . min ( d , b . get ( x ) ) ) ; } } int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( x != k - x && b . containsKey ( k - x ) ) ans = Math . min ( Math . max ( b . get ( x ) , b . get ( k - x ) ) , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 5 , 8 , 6 , 7 } ; int K = 11 ; int n = a . length ; System . out . println ( find_maximum ( a , n , K ) ) ; } }
Pair with given product | Set 1 ( Find if any pair exists ) | Java program to find if there is a pair with given product . ; Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Consider all possible pairs and check for every pair . ; Driver code
class GFG { boolean isProduct ( int arr [ ] , int n , int x ) { for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] * arr [ j ] == x ) return true ; return false ; } public static void main ( String [ ] args ) { GFG g = new GFG ( ) ; int arr [ ] = { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = arr . length ; if ( g . isProduct ( arr , n , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; x = 190 ; if ( g . isProduct ( arr , n , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Pair with given product | Set 1 ( Find if any pair exists ) | Java program if there exists a pair for given product ; Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Create an empty set and insert first element into it ; Traverse remaining elements ; 0 case must be handles explicitly as x % 0 is undefined ; x / arr [ i ] exists in hash , then we found a pair ; Insert arr [ i ] ; driver code
import java . util . HashSet ; class GFG { static boolean isProduct ( int arr [ ] , int n , int x ) { HashSet < Integer > hset = new HashSet < > ( ) ; if ( n < 2 ) return false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { if ( x == 0 ) return true ; else continue ; } if ( x % arr [ i ] == 0 ) { if ( hset . contains ( x / arr [ i ] ) ) return true ; hset . add ( arr [ i ] ) ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = arr . length ; if ( isProduct ( arr , arr . length , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; x = 190 ; if ( isProduct ( arr , arr . length , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }