# Check if N and M can be made equal by increasing N by A and decreasing M by B

Given four numbers **M**, **N**, **A** and **B**, the task is to check whether **M** and **N** can be made equal to each other by doing any of the below operations:

**M**can be increased by**A**and**N**can be decreased by**B**- Leave both of them as it is.

**Examples:**

Input:M = 2, N = 8, A = 3, B = 3

Output:Yes

Explanation:

After first Operation:

M can be increased by A. Therefore, M = 2 + 3 = 5

N can be decreased by B. Therefore, N = 8 – 3 = 5

Finally, M = N = 5.

Input:M = 6, N = 4, A = 2, B = 1

Output:No

**Approach:** On careful observation, it can be observed that since we are increasing M and decreasing N, they can be made equal only when M is less than N. Therefore when M is less than N, there are two cases at each step:

- M can be increased by A and N can be decreased by B.
- Leave both of them as it is.

Another observation which can be made is that when **M** is increased and **N** is decreased, the absolute distance between **M** and **N** is reduced by the factor of **A + B**. For example:

Let M = 2, N = 14, A = 3 and B = 3.

- In step 1, M = 5 and N = 11. The absolute distance between M and N got reduced by 6. That is, initially, the absolute distance was 12(14 – 2). After performing the given step, the absolute distance became 6(11 – 5).
- In step 2, M = 8 and N = 8. The absolute distance between M and N again got reduced by 6 thereby making M and N equal.

From the above example, we can come to the conclusion that this problem can be solved in a constant time only by checking if the absolute distance between **M** and **N** is a multiple of **(A + B)** or not.

- If it is a multiple, then M and N can be made equal.
- Else, they cannot be made equal.

Below is the implementation of the above approach:

## C++

`// C++ program to check if two numbers ` `// can be made equal by increasing ` `// the first by a and decreasing ` `// the second by b ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to whether the numbers ` `// can be made equal or not ` `bool` `checkEqualNo(` `int` `m, ` `int` `n, ` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(m <= n) { ` ` ` ` ` `// Check whether the numbers can reach ` ` ` `// an equal point or not ` ` ` `if` `((n - m) % (a + b) == 0) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// M and N cannot be made equal by ` ` ` `// increasing M and decreasing N when ` ` ` `// M is already greater than N ` ` ` `return` `false` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `M = 2, N = 8; ` ` ` `int` `A = 3, B = 3; ` ` ` ` ` `if` `(checkEqualNo(M, N, A, B)) ` ` ` `cout << ` `"Yes"` `<< endl; ` ` ` `else` ` ` `cout << ` `"No"` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if two numbers ` `// can be made equal by increasing ` `// the first by a and decreasing ` `// the second by b ` `class` `GFG ` `{ ` ` ` ` ` `// Function to whether the numbers ` ` ` `// can be made equal or not ` ` ` `static` `boolean` `checkEqualNo(` `int` `m, ` `int` `n, ` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(m <= n) { ` ` ` ` ` `// Check whether the numbers can reach ` ` ` `// an equal point or not ` ` ` `if` `((n - m) % (a + b) == ` `0` `) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// M and N cannot be made equal by ` ` ` `// increasing M and decreasing N when ` ` ` `// M is already greater than N ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `M = ` `2` `, N = ` `8` `; ` ` ` `int` `A = ` `3` `, B = ` `3` `; ` ` ` ` ` `if` `(checkEqualNo(M, N, A, B) == ` `true` `) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to check if two numbers ` `# can be made equal by increasing ` `# the first by a and decreasing ` `# the second by b ` ` ` `# Function to whether the numbers ` `# can be made equal or not ` `def` `checkEqualNo(m, n, a, b) : ` ` ` `if` `(m <` `=` `n) : ` ` ` ` ` `# Check whether the numbers can reach ` ` ` `# an equal point or not ` ` ` `if` `((n ` `-` `m) ` `%` `(a ` `+` `b) ` `=` `=` `0` `) : ` ` ` `return` `True` `; ` ` ` ` ` `else` `: ` ` ` `return` `False` `; ` ` ` ` ` `else` `: ` ` ` ` ` `# M and N cannot be made equal by ` ` ` `# increasing M and decreasing N when ` ` ` `# M is already greater than N ` ` ` `return` `False` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `M ` `=` `2` `; N ` `=` `8` `; ` ` ` `A ` `=` `3` `; B ` `=` `3` `; ` ` ` ` ` `if` `(checkEqualNo(M, N, A, B)) : ` ` ` `print` `(` `"Yes"` `); ` ` ` `else` `: ` ` ` `print` `(` `"No"` `); ` ` ` `# This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if two numbers ` `// can be made equal by increasing ` `// the first by a and decreasing ` `// the second by b ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to whether the numbers ` ` ` `// can be made equal or not ` ` ` `static` `bool` `checkEqualNo(` `int` `m, ` `int` `n, ` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(m <= n) { ` ` ` ` ` `// Check whether the numbers can reach ` ` ` `// an equal point or not ` ` ` `if` `((n - m) % (a + b) == 0) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// M and N cannot be made equal by ` ` ` `// increasing M and decreasing N when ` ` ` `// M is already greater than N ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main (String[] args) ` ` ` `{ ` ` ` `int` `M = 2; ` ` ` `int` `N = 8; ` ` ` `int` `A = 3; ` ` ` `int` `B = 3; ` ` ` ` ` `if` `(checkEqualNo(M, N, A, B) == ` `true` `) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Check if an array is increasing or decreasing
- Check if it is possible to make array increasing or decreasing by rotating the array
- Check if elements of array can be made equal by multiplying given prime numbers
- Count permutations that are first decreasing then increasing.
- Generate an alternate increasing and decreasing Array
- Sum of array elements that is first continuously increasing then decreasing
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Largest number smaller than or equal to n and digits in non-decreasing order
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Print all non-increasing sequences of sum equal to a given number x
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Check if X and Y can be made zero by using given operation any number of times
- Check whether the number can be made palindromic after adding K
- Check whether an Array can be made 0 by splitting and merging repeatedly
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Check whether the number can be made perfect square after adding K
- Check whether the number can be made perfect square after adding 1
- Check if given intervals can be made non-overlapping by adding/subtracting some X

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.