Check whether the exchange is possible or not

Given N types of money denomination in a country as an array arr[]. A person and the shopkeeper can choose notes of each denomination any number of times. The task is to check whether the exchange is possible when the person wants to buy a product worth P from the shop.

Examples:

Input: arr[] = {6, 9}, P = 3
Output: Yes
The person can pay 9 and get 6 in return.



Input: arr[] = {6, 9}, P = 4
Output: No

Approach: In order for the exchange to be possible, P must be divisible by the GCD of all the denominations. So, find the gcd of the array elements and check whether P is divisible by it or not.

Below is the implementation of the approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns true if
// the exchange is possible
bool isPossible(int arr[], int n, int p)
{
  
    // Find the GCD of the array elements
    int gcd = 0;
    for (int i = 0; i < n; i++)
        gcd = __gcd(gcd, arr[i]);
  
    // If the exchange is possible
    if (p % gcd == 0)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int arr[] = { 6, 9 };
    int n = sizeof(arr) / sizeof(int);
    int p = 3;
  
    if (isPossible(arr, n, p))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
      
    // Recursive function to 
    // return gcd of a and b 
    static int __gcd(int a, int b) 
    
        if (b == 0
            return a; 
        return __gcd(b, a % b); 
    
      
    // Function that returns true if 
    // the exchange is possible 
    static boolean isPossible(int []arr, 
                              int n, int p) 
    
      
        // Find the GCD of the array elements 
        int gcd = 0
        for (int i = 0; i < n; i++) 
            gcd = __gcd(gcd, arr[i]); 
      
        // If the exchange is possible 
        if (p % gcd == 0
            return true
      
        return false
    
      
    // Driver code 
    public static void main (String[] args) 
    
        int arr[] = { 6, 9 }; 
        int n = arr.length; 
        int p = 3
      
        if (isPossible(arr, n, p)) 
            System.out.println("Yes"); 
        else
            System.out.println("No"); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
from math import gcd as __gcd
  
# Function that returns true if 
# the exchange is possible 
def isPossible(arr, n, p) : 
  
    # Find the GCD of the array elements 
    gcd = 0
    for i in range(n) :
        gcd = __gcd(gcd, arr[i]); 
  
    # If the exchange is possible 
    if (p % gcd == 0) :
        return True
  
    return False;
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 6, 9 ]; 
    n = len(arr); 
    p = 3
  
    if (isPossible(arr, n, p)) :
        print("Yes"); 
    else :
        print("No"); 
          
# This code is contributed by kanugargng

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG 
{
      
    // Recursive function to 
    // return gcd of a and b 
    static int __gcd(int a, int b) 
    
        if (b == 0) 
            return a; 
        return __gcd(b, a % b); 
    
      
    // Function that returns true if 
    // the exchange is possible 
    static bool isPossible(int []arr, 
                           int n, int p) 
    
      
        // Find the GCD of the array elements 
        int gcd = 0; 
        for (int i = 0; i < n; i++) 
            gcd = __gcd(gcd, arr[i]); 
      
        // If the exchange is possible 
        if (p % gcd == 0) 
            return true
      
        return false
    
      
    // Driver code 
    public static void Main (String[] args) 
    
        int []arr = { 6, 9 }; 
        int n = arr.Length; 
        int p = 3; 
      
        if (isPossible(arr, n, p)) 
            Console.WriteLine("Yes"); 
        else
            Console.WriteLine("No"); 
    
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

Yes

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.