geeksforgeeks@ Equal to Product (Binary Search)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.