geeksforgeeks@ Equal to product (Binary Search)
Posted 流白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了geeksforgeeks@ Equal to product (Binary Search)相关的知识,希望对你有一定的参考价值。
http://www.practice.geeksforgeeks.org/problem-page.php?pid=667
Equal to product
Given an array of integers check whether there are two numbers present with given product.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N and a product p.
The second line of each test case contain N number of a[].
Output:
Print Yes if two 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
import java.util.*; import java.lang.*; import java.io.*; class GFG { public static boolean check(int[] arr, int q) { Arrays.sort(arr); int n = arr.length; for(int i=0; i<n; ++i) { if(arr[i] == 0) { if(q == 0) return true; else continue; } int remain = q % arr[i]; if(remain != 0) continue; int div = q / arr[i]; int pos = Arrays.binarySearch(arr, div); if(pos >= 0) { return true; } } return false; } public static void main (String[] args) { Scanner in = new Scanner(System.in); int times = in.nextInt(); while(times > 0) { int n = in.nextInt(); int q = in.nextInt(); int[] arr = new int[n]; for(int i=0; i<n; ++i) { arr[i] = in.nextInt(); } boolean rs = check(arr, q); System.out.println(rs? "Yes": "No"); --times; } } }
以上是关于geeksforgeeks@ Equal to product (Binary Search)的主要内容,如果未能解决你的问题,请参考以下文章
geeksforgeeks@ Minimum Points To Reach Destination (Dynamic Programming)
[GeeksForGeeks] Convert an array to reduced form
[GeeksForGeeks] Write a program to delete a tree
[GeeksForGeeks] Generate all root to leaf paths of a binary tree
[GeeksForGeeks] Count Number of ways to reach a given score in a game
matplotlib (equal unit length): with 'equal' aspect ratio z-axis is not equal to x- and y-