[email protected] 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;
        }
    }
}

时间: 2024-10-22 16:01:35

[email protected] Equal to product (Binary Search)的相关文章

[email&#160;protected] [173] Binary Search Tree Iterator (InOrder traversal)

https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: nex

ElasticSearch报 EsThreadPoolExecutor[search, queue capacity = 1000, [email&#160;protected]efba

ElasticSearch报以下错误的解决办法: "type": "es_rejected_execution_exception", "reason": "rejected execution of [email protected] on EsThreadPoolExecutor[search, queue capacity = 1000, [email protected]efba[Running, pool size = 7,

[email&#160;protected]一个高效的配置管理工具--Ansible configure management--翻译(六)

无书面许可请勿转载 高级playbook Finding files with variables All modules can take variables as part of their arguments by dereferencing them with {{ and }} . You can use this to load a particular file based on a variable. For example, you might want to select a

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and

【PWN】[email&#160;protected] 日课

Code:int __cdecl main(int argc, const char **argv, const char **envp){ int result; // [email protected] char sloganstr; // [sp+1Ch] [bp-9Ch]@1 char namestr[16]; // [sp+9Ch] [bp-1Ch]@1 size_t nbytes; // [sp+ACh] [bp-Ch]@1 nbytes = 16; *(_DWORD *)names

【PWN】[email&#160;protected]

#Exploit for [email protected] #@Windcarp 2015.07.05 from pwn import * #init context(arch = 'i386', os = 'linux') local=True if local: p = process("./urldecoder") libc = ELF("/lib/i386-linux-gnu/libc.so.6") else: p = remote("166.1

Go语言实现二叉查找树(Binary Search Trees)

官网有一个二叉排序树的例子,在此基础上增加了查找和删除节点功能. 代码: package main //Binary Search Trees //author: Xiong Chuan Liang //date: 2015-2-1 import ( "fmt" "math/rand" ) func main() { t := New(10, 1) if Search(t, 6) { fmt.Println("Search(6) true") }

数据结构-二叉搜索树(Binary Search Tree)的C++实现模板

笔者最近开始学习了二叉树这种数据结构,于是写出了一个二叉树的实现~ 二叉树真是个好东西 =.= 该图显示了在二叉树中插入一个节点的步骤...下面就用这个二叉树做测试好了 /** "BST.h"  * The Binary Search Tree Data Structure in C++  * Time Cost : Inorder / Preorder / Postorder Traversal : O(n)  *             Search / Find / Insert

代写java binary search trees|代写Java Data Structures CS作业|代写Java作业|Java 编程作业代写|Java作业代写

CS2230 Computer Science II: Data Structures Homework 7 Implementing Sets with binary search trees 30 points Goals for this assignment ? Learn about the implementation of Sets using binary search trees, both unbalanced and balanced ? Implement methods