![]() |
VOOZH | about |
Given two integers A and B. the task is to find the minimum number of operations required to make A and B equal. In each operation, either of the below steps can be performed:
Examples:
Input: A = 4, B = 10
Output: 4
Explanation:
Initially A = 4, B = 10
Operation 1: Increment A only: A = A + 4 = 8
Operation 2: Increment A only: A = A + 4 = 12
Operation 3: Increment A only: A = A + 4 = 16
Operation 4: Increment A and B: A = A + 4 = 20 and B = B + 10 = 20
They are equal now.
Input: A = 7, B = 23
Output: 22
Explanation:
Initially A = 7, B = 23
Operation 1 - 7: Increment A and B: A = 56 and B = 161
Operation 8 - 22: Increment A: A = 161 and B = 161
They are equal now.
Approach: This problem can be solved using GCD.
For example: If A = 4, B = 10:
Below is the implementation of the above approach.
14
Time Complexity: O(log(max(A, B))
Auxiliary Space: O(log(max(A, B))