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 = 5Output:YESExplanation:11 = 5 + 5 + 5 – 2 -2Input:N = 11, A = 2, B = 4Output:NOExplanation:Not possible to obtain 11 from 2 and 4 only.

**Approach: **

Follow the steps below to solve the problem:

- The task is to check if it is possible to add or subtract
**A**and**B**multiple times and obtain**N**as the end result. - Hence, in terms of linear equation, it can be written as:
**Ax + By = N**,

where**x**and**y**represent the number of times**A**and**B**are added or subtracted. Negative**x**represents**A**is subtracted**x**times and similarly, negative**y**represents**B**is subtracted**y**times - Now, the aim is to find the integral solutions for the above equation. Here, it is quite valid to use
**Extended Euclid Algorithm**which says that the solutions exist if and only if**N % gcd(a, b)**is 0.

Below is the implementation of the above approach:

## C++

`// 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"` `; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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 ` |

*chevron_right*

*filter_none*

## Python3

`# 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 ` |

*chevron_right*

*filter_none*

## C#

`// 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 ` |

*chevron_right*

*filter_none*

**Output:**

YES

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

## Recommended Posts:

- Check perfect square using addition/subtraction
- Addition and Subtraction of Matrix using pthreads
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Check if sum Y can be obtained from the Array by the given operations
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Check if N can be obtained from 1 by repetitively multiplying by 10 or 20
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Repeated subtraction among two numbers
- Subtraction of two large numbers using 9's compliment
- Subtraction of two large numbers using 10's compliment
- First number to leave an odd remainder after repetitive division by 2
- Addition of two numbers without propagating Carry
- Maximum array sum that can be obtained after exactly k changes
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- BCD addition of given Decimal numbers
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Check if given number can be represented as sum of two great numbers
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Split N into two integers whose addition to A and B makes them equal
- Find the total marks obtained according to given marking scheme

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.