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 ...
随机推荐
- [原]poj-2680-Choose the best route-dijkstra(基础最短路)
题目大意: 已知n 个点,m条路线,s为终点:给出m条路线及其权值:给出w个起点,求最短路! 思路:基础的dijkstra,有向无环正权最短路,只要把终点和起点 reverse考虑便可. AC代码如下 ...
- PowerDesign不让name和code联动
当name和code一样时,修改name的话,code的值将跟着变动,很不方便.如果能让code不随着name编码就好了. PowerDesign中的选项菜单,在[Tool]-->[Genera ...
- HeadFirst Jsp 04 (请求和响应作为servlet)
servlet 的存在就是为了客服服务, servlet的任务是得到一个客户的请求, 再发回一个响应. 由上图可知, web 容器会在启动后就加载所有的servlet类, 并为之创建实例和初始化 注意 ...
- HTML网页插入图像
一.WEB上支持的图片格式: GIF:能保存256中颜色,支持透明色,支持动画效果 JPEG:不支持透明色和动画,颜色可达1670种 PNG:支持透明色,不支持动画,颜色有几种到1670种 二.将图片 ...
- 为laravel5.1生产环境linux从源代码安装PHP
laravel5.1正式发布,该版本号称是第一个LTS的版本,它对环境的要求也比较高,至少要PHP5.59以上. 现在网上找了很久,只能自己从头安装新版本的PHP yum install libmcr ...
- 一招解决OpenERP8.0安装旧版模块报错
有喜欢尝鲜的网友开始玩8.0了,可是版本还没发布,社区的很多特别好的模块还没有升级到8,所以经常碰到模块无法安装的问题. No module name osv 网友提出将模块的 from osv im ...
- Windows上安装Yeoman
之前直接安装完Node.js后,运行npm install -g yo命令,结果出现什么"要安装framework2.0 sdk,vcbuild"什么的错误,怎么也弄不好,结果是各 ...
- swun 1612 合并果子
//思路:这题思路似乎很简单,每次取出最小的两个堆合并, //但是由于数据太大,不能采取每次进行排序的方式,所以 //想到用优先队列,以数据小的优先级更高为标准,但是 //优先队列中的数据默认情况 ...
- 构建通过 Database.com 提供技术支持的 PhoneGap 应用程序
要求 其他必要产品 Database.com account 用户级别 全部 必需产品 PhoneGap Build 范例文件 Database.Com-PhoneGap-Sample 在这篇文章中, ...
- hdu 5423 Rikka with Tree(dfs)bestcoder #53 div2 1002
题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1. 定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2. 对于树A与树B,如 ...