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 ...
随机推荐
- 安装hadoop
生成yum源 cd /var/ftp/pub/cdh/5 createrepo --update . 从节点 yum clean all 配置yum库 /etc/yum.repos.d # cat / ...
- hdu 4856 Tunnels (bfs + 状压dp)
题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...
- 为Gradle添加UTF-8支持
gradle默认使用系统字符编码,大多数中文系统是使用GBK编码 但程序员绝大部分都是使用UTF-8写各类java文件以及其他资源文件 编译时很容易报错,比如下面的错误: ”警告:编码 GBK 的不可 ...
- css圣杯布局、等高布局
所谓圣杯布局,就是自适应浏览器的分辨率,也称双飞翼布局. 例子: <style> body{margin:0;} .center{height:600px;background:#f60; ...
- 【经验】Windows7、8、8.1 MSI安装错误Error Code 2502 & 2503 解决方法
[因] 今天升级TortoiseSVN到1.8.8,出现问题:Error Code 2502 & 2503,一直不能安装成功. 上网一搜,国内没找到好的解决方法,在一个外文网上找到了方案,原链 ...
- 比较满意设计的一次作业 JDBC宠物管理
package com.dao; import java.util.List; import com.entity.Master; public interface MasterDao { publi ...
- android SDK 快速更新配置(转)
http://blog.csdn.net/yy1300326388/article/details/45074447 1.强制使用http替换https链接 Tools>选择Options,勾选 ...
- vc判断文件是否存在
#include <io.h> #include <stdio.h> #include <stdlib.h> void main( void ) { /* Chec ...
- python工厂方式创建list
>>> l = list('sdfsdf') >>> l ['s', 'd', 'f', 's', 'd', 'f']
- Hadoop 学习总结之一:HDFS简介
一.HDFS的基本概念 1.1.数据块(block) HDFS(Hadoop Distributed File System)默认的最基本的存储单位是64M的数据块. 和普通文件系统相同的是,HDFS ...