VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-minimum-number-of-operations-to-make-x-and-y-equal/

⇱ Find Minimum Number of Operations to Make X and Y Equal - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find Minimum Number of Operations to Make X and Y Equal

Last Updated : 11 Jun, 2024

You are given two positive integers x and y. In one operation, you can do one of the four following operations:

  • Divide x by 11 if x is a multiple of 11.
  • Divide x by 5 if x is a multiple of 5.
  • Decrement x by 1.
  • Increment x by 1.

Return the minimum number of operations required to make x and y equal.

Examples:

Input: x = 26, y = 1
Output: 3
Explanation: We can make 26 equal to 1 by applying the following operations:

  • Decrement x by 1
  • Divide x by 5
  • Divide x by 5

It can be shown that 3 is the minimum number of operations required to make 26 equal to 1.

Input: x = 54, y = 2
Output: 4
Explanation: We can make 54 equal to 2 by applying the following operations:

  • Increment x by 1
  • Divide x by 11
  • Divide x by 5
  • Increment x by 1

It can be shown that 4 is the minimum number of operations required to make 54 equal to 2.

Approach:

We need to check for 5 ways in our recursive calls :

  • Just abs diff of x & y can be ans. So initialise res = abs(x - y)
  • We may go to multiple of 5 which is smaller than x. This can be achieved by just subtracting x%5 from x and divide x by 5. Here total operations = x%5 ( this many time decreament ) + 1( for division by 5).
  • We may go to multiple of 5 which is larger than x. This can be achieved by just adding (5 - x%5) to x and then divid x by 5. Here total operations = 5 - x%5 ( this many time increment ) + 1( for division by 5).
  • We may go to multiple of 11 which is smaller than x. This can be achieved by just subtracting x%11 from x and divide x by 11. Here total operations = x%11 ( this many time decreament ) + 1( for division by 11).
  • We may go to multiple of 11 which is larger than x. This can be achieved by just adding (11 - x%11) to x and then divid x by 11. Here total operations = 11 - x%11 ( this many time increment ) + 1( for division by 11).
  • With considereing above cases we need to write recursive code to find the min operations.

Step-by-step algorithm:

  • First checks if x is already less than or equal to y, in which case it simply returns the difference y - x.
  • Then checks if the result for the current x value has already been calculated and stored in the dp array. If so, it returns the pre-calculated result.
  • If the result is not pre-calculated, it initializes the res variable with the absolute difference abs(x - y).
  • It then considers five possible cases to minimize the number of operations:
    • Case 1: Subtract 1 from x and recursively call solve with the updated x value.
    • Case 2: Divide x by 5 and add the remainder, then recursively call solve with the updated x value.
    • Case 3: Divide x by 5, add the remaining value to reach a multiple of 5, and then subtract 1, then recursively call solve with the updated x value.
    • Case 4: Divide x by 11 and add the remainder, then recursively call solve with the updated x value.
    • Case 5: Divide x by 11, add the remaining value to reach a multiple of 11, and then subtract 1, then recursively call solve with the updated x value.
  • then stores the calculated result in the dp array and returns the res value.

Below is the implementation of the algorithm:


Output
Minimum operations to make 26 and 1 equal: 3

Time Complexity: O(N)
Auxiliary Space: O(N)

Comment
Article Tags: