Given two numbers X and Y. The task is to check whether X and Y can be reduced to zero at the same time by performing the following operation any number of times:
- Choose any natural number (say z) and reduce X and Y as one of the following at each operation:
- X = X – z and Y = Y – 2*z
- X = X – 2*z and Y = Y – z
Input: X = 6, Y = 9
We can perform operation in following way:
if z = 1, then
X = X – 2*z = 6 – 2*(1)
Y = Y – z = 9 – 1
=> X = 4 & Y = 8
Now again if z = 4, then
X = X – z = 4 – 4
Y = Y – 2*z = 8 – 2*(4)
=> X = 0 & Y = 0
Therefore, X & Y become zero in 2 steps assuming z as 1 and 4 respectively.
Input: X = 1, Y = 1
We don’t have any possible value for z such that X & Y can become zero simultaneously.
Below are the observation for the given problem statement:
- Since X and Y are updated to (X – z and Y – 2*z) or (X – 2*z and Y – z), therefore after n number of operations (X + Y) is updated to (X + Y – 3*n*z). Hence X and Y can be reduced to zero at simultaneously if (X+Y)%3 equals 0.
- At each step one of the X or Y is reduced by 2*z. To reduced X and Y simultaneously zero it must satify this condition: max(X, Y)≤ 2*min(X, Y).
Let X = 6 and Y = 15
Since (X+Y)%3 = (21%3) = 0
As our first condition is satisfied,
But by taking z = 6
X = X – z = 6 – 6 = 0
Y = Y – 2*z = 15 – 12 = 3
Since Y is not less than or equals to 2*X, therefore X and Y cannot be reduced to zero at same time.
If the above two conditions satisfy the values of X and Y, then X and Y can be reduced to 0 simultaneously.
Below is the implementation of the above approach:
Time Complexity: O(1)
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
- Make the array non-decreasing with the given operation
- Make all elements of an array equal with the given operation
- Check if two arrays are permutations of each other using Mathematical Operation
- Check if X and Y can be made zero by using given operation any number of times
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Make all elements zero by decreasing any two elements by one at a time
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Program to check if tank will overflow, underflow or filled in given time
- Check if it is possible to make array increasing or decreasing by rotating the array
- Minimum time to reach a point with +t and -t moves at time t
- Minimum possible number with the given operation
- Maximum possible number with the given operation
- Count the numbers which can convert N to 1 using given operation
- Minimize the non-zero elements in the Array by given operation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.