Problem · Math

Make Numbers Equal

Learn this problem
EasyXperiINTERNOA

Problem statement

There are two numbers number A and number B. Your task is to make A and B equal by doing few operations. In each operation:

  • If A is greater than B then reduce A by B i.e A = A - B
  • If B is greater than A then reduce B by A i.e B = B - A
  • Cost of each operation is 1. Find the total cost of making A equal to B.

    Input Format:

    Two spaced-separated integers A and B

    Output:

    Total cost

    Function

    makeNumbersEqual(A: int, B: int) → int

    Examples

    Example 1

    A = 7B = 9return = 5
    Operation 1: B > A so B = 9 - 7 = 2
    Operation 2: A > B so A = 7 - 2 = 5
    Operation 3: A > B so A = 5 - 2 = 3
    Operation 4: A > B so A = 3 - 2 = 1
    Operation 5: B > A so B = 2 - 1 = 1
    A and B are equal so we stop cost = 5.

    More Xperi problems

    drafts saved locally
    public int makeNumbersEqual(int A, int B) {
      // write your code here
    }
    
    A7
    B9
    expected5
    checking account