http://www.practice.geeksforgeeks.org/problem-page.php?pid=667
Equal to Product
Given an array of integers check whether there is both numbers present with Given product.
Input:
The first line of input contains an integer T denoting the number of the test cases.
The first line of all test case is N and a product p.
The second line of all test case contain N number of a[].
Output:
Print Yes If both numbers product is equal to P else No.
Constraints:
1≤t≤51
1≤n≤100
0≤a[]≤1000
1≤pro≤2000000
Example:
Input:
2
5 2
1 2 3) 4 5
8 46
5 7 9 22 15 344 92 8
Output:
Yes
No
ImportJava.util.*;Importjava.lang.*;ImportJava.io.*;classGFG { Public Static BooleanCheckint[] arr,intq) {arrays.sort (arr); intn =arr.length; for(inti=0; i<n; ++i) {if(Arr[i] = = 0) { if(q = 0)return true; Else Continue; } intremain = q%Arr[i]; if(Remain! = 0)Continue; intdiv = Q/Arr[i]; intpos =Arrays.binarysearch (arr, div); if(POS >= 0) { return true; } } return false; } Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intTimes =In.nextint (); while(Times > 0) { intn =In.nextint (); intQ =In.nextint (); int[] arr =New int[n]; for(inti=0; i<n; ++i) {Arr[i]=In.nextint (); } Booleanrs =Check (arr, q); System.out.println (Rs? "Yes": "No"); --Times ; } }}
View Code
[email protected] Equal to Product (Binary Search)