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)的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.
http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...
- my understanding of (lower bound,upper bound) binary search, in C++, thanks to two post 分类: leetcode 2015-08-01 14:35 113人阅读 评论(0) 收藏
If you understand the comments below, never will you make mistakes with binary search! thanks to A s ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- Print Common Nodes in Two Binary Search Trees
Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two B ...
- 04-树5 Complete Binary Search Tree
这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...
- 04-树6 Complete Binary Search Tree
完全二叉树 刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两 ...
- 1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)
Pat1043代码 题目描写叙述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the f ...
随机推荐
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- 二维码(2)二维码登录原理及Android客户端示例
1,原理 服务器: 数据库: 建立一个2维码登录的数据表,产生一个登录页时,插入一条记录X,X含将要登录的用户名字段(初始为空),2维码中的数据字段(唯一) 登录页面: 在产生的2维码中包含关键数据Y ...
- 如何获取supersocket的源代码
源代码的地址:https://github.com/kerryjiang/SuperSocket 安装git之后,可以使用命令行git clone https://github.com/kerryji ...
- Oracle HRMS API – Create Employee
-- Create Employee -- ------------------------- DECLARE lc_employee_number PER_ALL_PEOP ...
- [HDOJ5573]Binary Tree(找规律,贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573 这个题……规律暂时还找不到,先贡献两发TLE的代码吧,一个dfs一个状压枚举. #include ...
- Trianglify – 五彩缤纷的 SVG 背景图案
Trianglify 是一个能够生成五颜六色的三角形图案的 JavaScript 库,可以用来作为 SVG 图像和 CSS 背景.它的灵感来自于 Btmills 的 Geopattern,并使用 d3 ...
- mysql三
修改字段名 mysql> alter table users change id user_id INT UNSIGNED AUTO_INCREMENT; 修改字段类型 mysql> al ...
- 【C#学习笔记】网页弹出提示框
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 关于Java接口
1 接口的本质 (1)一组有规则的集合: (2)一定视角上的同类事物的抽象:同类事物的概念是相对的 2 接口与抽象类的区别 (1)java不支持类的多继承,但可以实现多个接口: (2)使用动机:抽象类 ...
- 定制Bootstrap遇到无法下载的解决——Blob下载注意事项
今天定制bootstrap(在这里),全部的勾都选过了,于是兴高采烈地点击“编译并下载”.等了一会儿,迅雷7跳出来了“新建下载任务”,但是它居然说这个url不合法! url像这样: blob:http ...