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++

`// C++ implementation to check ` `// if it is possible to pay a value ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if it ` `// is possible to pay a value ` `void` `knowPair(` `int` `a, ` `int` `b, ` ` ` `int` `n, ` `int` `s, ` `int` `m){ ` ` ` ` ` `int` `i = 0, rem = 0; ` ` ` `int` `count_b = 0, flag = 0; ` ` ` ` ` `// Loop to add the value of coin A ` ` ` `while` `(i <= a) { ` ` ` `rem = s - (n * i); ` ` ` `count_b = rem / m; ` ` ` `if` `(rem % m == 0 && count_b <= b){ ` ` ` `flag = 1; ` ` ` `} ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// Condition to check if it is ` ` ` `// possible to pay a value of S ` ` ` `if` `(flag == 1) { ` ` ` `cout << ` `"YES"` `<< endl; ` ` ` `}` `else` `{ ` ` ` `cout << ` `"NO"` `<< endl; ` ` ` `} ` `} ` ` ` `// Driver Code ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to check ` `// if it is possible to pay a value ` `class` `GFG{ ` ` ` `// Function to check if it ` `// is possible to pay a value ` `static` `void` `knowPair(` `int` `a, ` `int` `b, ` ` ` `int` `n, ` `int` `s, ` `int` `m){ ` ` ` ` ` `int` `i = ` `0` `, rem = ` `0` `; ` ` ` `int` `count_b = ` `0` `, flag = ` `0` `; ` ` ` ` ` `// Loop to add the value of coin A ` ` ` `while` `(i <= a) { ` ` ` `rem = s - (n * i); ` ` ` `count_b = rem / m; ` ` ` `if` `(rem % m == ` `0` `&& count_b <= b){ ` ` ` `flag = ` `1` `; ` ` ` `} ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// Condition to check if it is ` ` ` `// possible to pay a value of S ` ` ` `if` `(flag == ` `1` `) { ` ` ` `System.out.print(` `"YES"` `+` `"\n"` `); ` ` ` `}` `else` `{ ` ` ` `System.out.print(` `"NO"` `+` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `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); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation to check ` `# if it is possible to pay a value ` ` ` `# Function to check if it ` `# is possible to pay a value ` `def` `knowPair(a,b,n,s,m): ` ` ` `i ` `=` `0` ` ` `rem ` `=` `0` ` ` `count_b ` `=` `0` ` ` `flag ` `=` `0` ` ` ` ` `# Loop to add the value of coin A ` ` ` `while` `(i <` `=` `a): ` ` ` `rem ` `=` `s ` `-` `(n ` `*` `i) ` ` ` `count_b ` `=` `rem ` `/` `/` `m ` ` ` `if` `(rem ` `%` `m ` `=` `=` `0` `and` `count_b <` `=` `b): ` ` ` `flag ` `=` `1` ` ` `i ` `+` `=` `1` ` ` ` ` `# Condition to check if it is ` ` ` `# possible to pay a value of S ` ` ` `if` `(flag ` `=` `=` `1` `): ` ` ` `print` `(` `"YES"` `) ` ` ` `else` `: ` ` ` `print` `(` `"NO"` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `A ` `=` `1` ` ` `B ` `=` `2` ` ` `n ` `=` `3` ` ` `S ` `=` `4` ` ` `m ` `=` `2` ` ` ` ` `knowPair(A, B, n, S, m) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to check ` `// if it is possible to pay a value ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to check if it ` `// is possible to pay a value ` `static` `void` `knowPair(` `int` `a, ` `int` `b, ` ` ` `int` `n, ` `int` `s, ` `int` `m){ ` ` ` ` ` `int` `i = 0, rem = 0; ` ` ` `int` `count_b = 0, flag = 0; ` ` ` ` ` `// Loop to add the value of coin A ` ` ` `while` `(i <= a) { ` ` ` `rem = s - (n * i); ` ` ` `count_b = rem / m; ` ` ` `if` `(rem % m == 0 && count_b <= b){ ` ` ` `flag = 1; ` ` ` `} ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// Condition to check if it is ` ` ` `// possible to pay a value of S ` ` ` `if` `(flag == 1) { ` ` ` `Console.Write(` `"YES"` `+ ` `"\n"` `); ` ` ` `}` `else` `{ ` ` ` `Console.Write(` `"NO"` `+ ` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `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); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

YES

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find out the minimum number of coins required to pay total amount
- Minimum cost for acquiring all coins with k extra coins allowed with every coin
- Check if characters of a given string can be used to form any N equal strings
- Minimum number of cuts required to pay salary from N length Gold Bar
- Maximum items that can be bought with the given type of coins
- Find minimum number of coins that make a given value
- Generate a combination of minimum coins that sums to a given value
- Find optimal weights which can be used to weigh all the weights in the range [1, X]
- Maximum number of distinct positive integers that can be used to represent N
- Program to find the count of coins of each type from the given ratio
- Buy minimum items without change and given coins
- Probability of getting at least K heads in N tosses of Coins
- Greedy Algorithm to find Minimum number of Coins
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Path traversed using exactly M coins in K jumps
- Bin Packing Problem (Minimize number of used Bins)
- Program for Least Recently Used (LRU) Page Replacement algorithm
- Implementation of Least Recently Used (LRU) page replacement algorithm using Counters
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Check if the given array can be reduced to zeros with the given operation performed given number of times

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.