-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathexchangingmoney.cpp
57 lines (48 loc) · 1.19 KB
/
exchangingmoney.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//hackerearth problem picked from noveasy19
//Problem url:https://www.hackerearth.com/problem/algorithm/money-exchange-48c4a575/
/*question:There are n types of money denomination in your country having values a1,a2,..an .
You went to a shop to buy a product worth p units,
predict whether is it possible to complete the purchase or not
(You and the shopkeeper can choose notes of each denomination any number of times).
You need to process Q queries for this.
Print "YES" if it is possible to complete the purchase,
else print "NO".*/
//Program
#include<bits/stdc++.h>
using naemespace std;
#define llt long long int
int main()
{
llt t n,q;
cin>>n>>q;
//long long int type
llt a[n],g=0;
for(llt i=0;i<n;i++)
{
cin>>a[i];
//__gcd is inbuild funtion in cpp to calculate gcd of two numbers
g=__gcd(a[i],g);
}
//q number of query
while(q--)
{
llt p;
cin>>p;
if(__gcd(g,p)==g)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
/*
Input:
2 2
6 9
3
4
Output:
YES
NO
Note:For the first query p=3 , you give a note of 9 to the salesman and he returns 6 to you,
hence the purchase completed successfully.
*/