Problem Solution

1 solutions

  • 1
    @ 2024-03-18 18:00:58

    Only if after some operation A and B becomes equal, right after that in one operation one of them will be 0. So while A is not equal to B we can find the answer with brute force solution.
    But there is a problem with the huge constrains. To avoid TLE we can shorten the process by calculating the number of steps the bigger number will take to become equal or smaller than the smaller number.

    say for example if A=3 and B=10 after three operations B becomes smaller than A (A=3,B=1). so without running a loop for three times we can count this with an equation in a single operation.

    operation = ceil(current_max / current_min)-1;

    then current maximum number will be changed as :
    current_max -= operation * current_min

    As the current max number is being devided with current minimum number every time, this solution works in O(logN) complexity.

  • 1

Information

ID
1029
Difficulty
7
Category
Beginners Click to Show
Tags
(None)
# Submissions
72
Accepted
13
Accepted Ratio
18%
Uploaded By