Check if given coins can be used to pay a value of S
Given A coins of value N and B coins of value M, the task is to check if given coins can be used to pay a value of S.
Examples:
Input: A = 1, B = 2, N = 3, S = 4, M = 1
Output: YES
Explanation:
In this case if 1 coin of value 3 is chosen and 2 coins of value 1, then it is possible to pay a value of S.
Input: A = 1, B = 2, N = 3, S = 6, M = 1
Output: NO
In this case, It is not possible to pay a value of S
Approach:
The idea is to use greedy approach.
- Keep subtracting coins with value N from the required sum S.
- At each step, while subtracting coins of value N, check if the remaining sum is a multiple of coins with value M and we have sufficient coins of value M to get this remaining sum.
- If at any step, the above two conditions are satisfied, return YES.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void knowPair( int a, int b,
int n, int s, int m){
int i = 0, rem = 0;
int count_b = 0, flag = 0;
while (i <= a) {
rem = s - (n * i);
count_b = rem / m;
if (rem % m == 0 && count_b <= b){
flag = 1;
}
i++;
}
if (flag == 1) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
int main()
{
int A = 1;
int B = 2;
int n = 3;
int S = 4;
int m = 2;
knowPair(A, B, n, S, m);
return 0;
}
|
Java
class GFG{
static void knowPair( int a, int b,
int n, int s, int m){
int i = 0 , rem = 0 ;
int count_b = 0 , flag = 0 ;
while (i <= a) {
rem = s - (n * i);
count_b = rem / m;
if (rem % m == 0 && count_b <= b){
flag = 1 ;
}
i++;
}
if (flag == 1 ) {
System.out.print( "YES" + "\n" );
} else {
System.out.print( "NO" + "\n" );
}
}
public static void main(String[] args)
{
int A = 1 ;
int B = 2 ;
int n = 3 ;
int S = 4 ;
int m = 2 ;
knowPair(A, B, n, S, m);
}
}
|
Python3
def knowPair(a,b,n,s,m):
i = 0
rem = 0
count_b = 0
flag = 0
while (i < = a):
rem = s - (n * i)
count_b = rem / / m
if (rem % m = = 0 and count_b < = b):
flag = 1
i + = 1
if (flag = = 1 ):
print ( "YES" )
else :
print ( "NO" )
if __name__ = = '__main__' :
A = 1
B = 2
n = 3
S = 4
m = 2
knowPair(A, B, n, S, m)
|
C#
using System;
class GFG{
static void knowPair( int a, int b,
int n, int s, int m){
int i = 0, rem = 0;
int count_b = 0, flag = 0;
while (i <= a) {
rem = s - (n * i);
count_b = rem / m;
if (rem % m == 0 && count_b <= b){
flag = 1;
}
i++;
}
if (flag == 1) {
Console.Write( "YES" + "\n" );
} else {
Console.Write( "NO" + "\n" );
}
}
public static void Main(String[] args)
{
int A = 1;
int B = 2;
int n = 3;
int S = 4;
int m = 2;
knowPair(A, B, n, S, m);
}
}
|
Javascript
<script>
function knowPair(a, b, n, s, m)
{
var i = 0, rem = 0;
var count_b = 0, flag = 0;
while (i <= a)
{
rem = s - (n * i);
count_b = parseInt(rem / m);
if (rem % m == 0 && count_b <= b)
{
flag = 1;
}
i++;
}
if (flag == 1)
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
}
var A = 1;
var B = 2;
var n = 3;
var S = 4;
var m = 2;
knowPair(A, B, n, S, m);
</script>
|
Time Complexity: O(A)
Auxiliary Space: O(1)
Last Updated :
17 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...