Given initial values of two positive integers X and Y, Find the final value of X and Y according to below alterations:
1. If X=0 or Y=0, terminate the process. Else, go to step 2;
2. If X >= 2*Y, then change the value of X to X – 2*Y, and repeat step 1. Else, go to step 3;
3. If Y >= 2*X, then assign the value of Y to Y – 2*X, and repeat step 1. Else, end the process.
Constraints: 1<=X, Y<=10^18
Input: X=12, Y=5 Output: X=0, Y=1 Explanation: Initially X = 12, Y = 5 --> X = 2, Y = 5 (as X = X-2*Y) --> X = 2, Y = 1 (as Y = Y-2*X) --> X = 0, Y = 1 (as X = X-2*Y) --> Stop (as X = 0) Input: X=31, Y=12 Output: X=7, Y=12 Explanation: Initially X = 31, Y = 12 --> X = 7, Y = 12 (as X = X-2*Y) --> Stop (as (Y - 2*X) < 0)
Approach: Since the initial values of X and Y can be as high as 10^18. Simple brute force approach will not work.
If we observe carefully, the problem statement is nothing but a sort of Euclid Algorithm, where we will replace all subtractions with modulo.
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.
- Find sub-arrays from given two arrays such that they have equal sum
- Find smallest number with given number of digits and sum of digits under given constraints
- Find Four points such that they form a square whose sides are parallel to x and y axes
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
- Find the final sequence of the array after performing given operations
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Find the final radiations of each Radiated Stations
- Final Matrix after incrementing submatrices by K in range given by Q queries
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Find power of power under mod of a prime
- Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
- Find the lexicographically smallest string which satisfies the given condition
- Find permutation of first N natural numbers that satisfies the given condition
- Find the Number of Permutations that satisfy the given condition in an array
- Smallest index in the given array that satisfies the given condition
- Final cell position in the matrix
- Final state of the string after modification
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.