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.
Input: M = 2, N = 8, A = 3, B = 3
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
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:
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.
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Check if elements of array can be made equal by multiplying given prime numbers
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- Check if an array is increasing or decreasing
- Check if it is possible to make array increasing or decreasing by rotating the array
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Generate an alternate increasing and decreasing Array
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Check if X and Y can be made zero by using given operation any number of times
- Check whether an Array can be made 0 by splitting and merging repeatedly
- Sum of array elements that is first continuously increasing then decreasing
- Count permutations that are first decreasing then increasing.
- Check whether the number can be made perfect square after adding 1
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Check if given array can be made 0 with given operations performed any number of times
- Check whether the number can be made perfect square after adding K
- Check whether the number can be made palindromic after adding K
- 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 email@example.com. 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.
Improved By : Yash_R