Open In App

Check if N can be obtained by repetitive addition or subtraction of two given numbers

Given three positive integers N, A, and B, the task is to check if it is possible to obtain N by adding or subtracting A and B multiple times. 

Examples:



Input: N = 11, A = 2, B = 5 
Output: YES 
Explanation:  11 = 5 + 5 + 5 – 2 -2

Input: N = 11, A = 2, B = 4 
Output: NO 
Explanation: Not possible to obtain 11 from 2 and 4 only. 
 



Approach: 
Follow the steps below to solve the problem:

Below is the implementation of the above approach: 




// C++ Program to check if
// a number can be obtained
// by repetitive addition
// or subtraction of two numbers
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check and return if
// it is possible to obtain N by
// repetitive addition or subtraction
// of A and B
bool isPossible(int N, int a, int b)
{
    // Calculate GCD of A and B
    int g = __gcd(a, b);
 
    // Condition to check
    // if N can be obtained
    if (N % g == 0)
        return true;
    else
        return false;
}
 
// Driver Code
int main()
{
    int N = 11, a = 2;
    int b = 5;
 
    if (isPossible(N, a, b))
        cout << "YES";
    else
        cout << "NO";
}




// Java program to check if
// a number can be obtained
// by repetitive addition
// or subtraction of two numbers
class GFG{
     
// Recursive function to return
// gcd of a and b
public static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
     
// Function to check and return if
// it is possible to obtain N by
// repetitive addition or subtraction
// of A and B
public static boolean isPossible(int N, int a,
                                 int b)
{
     
    // Calculate GCD of A and B
    int g = gcd(a, b);
     
    // Condition to check
    // if N can be obtained
    if (N % g == 0)
        return true;
    else
        return false;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 11, a = 2;
    int b = 5;
     
    if (isPossible(N, a, b))
        System.out.print("YES");
    else
        System.out.print("NO");
}
}
 
// This code is contributed by divyeshrabadiya07




# Python3 program to check if
# a number can be obtained
# by repetitive addition
# or subtraction of two numbers
 
# Recursive function to return
# gcd of a and b
def gcd(a, b):
     
    if (b == 0):
        return a
    return gcd(b, a % b)
     
# Function to check and return if
# it is possible to obtain N by
# repetitive addition or subtraction
# of A and B
def isPossible(N, a, b):
     
    # Calculate GCD of A and B
    g = gcd(a, b)
     
    # Condition to check
    # if N can be obtained
    if (N % g == 0):
        return True
    else:
        return False
         
# Driver code
N = 11
a = 2
b = 5
 
if (isPossible(N, a, b) != False):
    print("YES")
else:
    print("NO")
 
# This code is contributed by code_hunt




// C# program to check if
// a number can be obtained
// by repetitive addition
// or subtraction of two numbers
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 to check and return if
// it is possible to obtain N by
// repetitive addition or subtraction
// of A and B
static bool isPossible(int N, int a,
                              int b)
{
     
    // Calculate GCD of A and B
    int g = gcd(a, b);
     
    // Condition to check
    // if N can be obtained
    if (N % g == 0)
        return true;
    else
        return false;
}
 
// Driver code
public static void Main()
{
    int N = 11, a = 2;
    int b = 5;
     
    if (isPossible(N, a, b))
        Console.Write("YES");
    else
        Console.Write("NO");
}
}
 
// This code is contributed by chitranayal




<script>
 
// Javascript program to check if
// a number can be obtained
// by repetitive addition
// or subtraction of two numbers
 
// Recursive function to return
// gcd of a and b
function gcd(a, b)
{
    if (b == 0)
        return a;
         
    return gcd(b, a % b);
}
 
// Function to check and return if
// it is possible to obtain N by
// repetitive addition or subtraction
// of A and B
function isPossible(N, a, b)
{
     
    // Calculate GCD of A and B
    var g = gcd(a, b);
     
    // Condition to check
    // if N can be obtained
    if (N % g == 0)
        return true;
    else
        return false;
}
 
// Driver code
var N = 11, a = 2;
var b = 5;
 
if (isPossible(N, a, b))
    document.write("YES");
else
    document.write("NO");  
     
// This code is contributed by Ankita saini
    
</script>

Output: 
YES

Time Complexity: O(log(min(A, B)) 
Auxiliary Space: O(1)
 


Article Tags :