The Wayback Machine - https://web.archive.org/web/20211028212642/https://www.geeksforgeeks.org/minimum-operations-required-to-reduce-n-to-0-by-either-replacing-n-with-n-m-or-incrementing-m-by-1/
Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Minimum operations required to reduce N to 0 by either replacing N with N/M or incrementing M by 1

  • Last Updated : 28 Oct, 2021

Given two integers N and M, the task is to calculate the minimum number of operations required to reduce N to 0 using the following operations:

  • Replace N with (N/M).
  • Increment the value of M by 1.

Example:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: N = 9, M = 2
Output: 4
Explanation: The given example can be solved by following the below sequence of operations:



  • In 1st operation, replace N with (N/M), i.e, N = 9/2 = 4. 
  • In 2nd operation, again replace N with N/M, i.e, N = 4/2 = 2. 
  • In 3rd operation, increment M by 1, i.e, M = M+1 = 2+1 = 3. 
  • In 4th operation, replace N with N/M, i.e, N = 2/3 = 0.

Hence, the number of required operations is 4 which is the minimum possible.

Input: N = 15, M = 1
Output: 5

Approach: The given problem can be solved by observing the fact that the most optimal choice of operations is to increment the value of M let’s say x times and then reduce the value of N to N / (M+x) until it becomes 0. To find the best case, iterate over all values of x in the range [0, √N] using a variable i and calculate the number of steps required to reduce N to 0 by dividing it by (M+i). Keep track of the minimum number of operations over all possible values of (M+i) in a variable ans, which is the required value. 

Below is the implementation of the above approach:

C++




// C++ Program of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the minimum count of
// operations to reduce N to 0 using M
int findMinimum(int N, int M)
{
    // If N is already 0
    if (N == 0) {
        return 0;
    }
  
    // Stores the minimum count of operations
    int ans = INT_MAX;
  
    // Loop to iterate in the range [0, √N]
    for (int i = 0; i * i <= N; i++) {
  
        // Edge case to prevent infinite looping
        if (M == 1 && i == 0) {
            continue;
        }
  
        // Stores the current count of moves
        int count = i;
        int tempN = N;
  
        // Number of operations required to
        // reduce N to 0 by dividing by M + i
        while (tempN != 0) {
            tempN /= (M + i);
            count++;
        }
  
        // Update the final count
        ans = min(count, ans);
    }
  
    // Return answer
    return ans;
}
  
// Driver code
int main()
{
    int N = 9;
    int M = 2;
  
    cout << findMinimum(N, M);
  
    return 0;
}
Output
4

Time complexity: O(√N*log N ) 
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :