Minimum operations required to reduce N to 0 by either replacing N with N/M or incrementing M by 1
Last Updated :
11 Nov, 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:
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++
#include <bits/stdc++.h>
using namespace std;
int findMinimum( int N, int M)
{
if (N == 0) {
return 0;
}
int ans = INT_MAX;
for ( int i = 0; i * i <= N; i++) {
if (M == 1 && i == 0) {
continue ;
}
int count = i;
int tempN = N;
while (tempN != 0) {
tempN /= (M + i);
count++;
}
ans = min(count, ans);
}
return ans;
}
int main()
{
int N = 9;
int M = 2;
cout << findMinimum(N, M);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int findMinimum( int N, int M)
{
if (N == 0 ) {
return 0 ;
}
int ans = 1000000007 ;
for ( int i = 0 ; i * i <= N; i++) {
if (M == 1 && i == 0 ) {
continue ;
}
int count = i;
int tempN = N;
while (tempN != 0 ) {
tempN /= (M + i);
count++;
}
if (count < ans){
ans = count;
}
}
return ans;
}
public static void main(String[] args)
{
int N = 9 ;
int M = 2 ;
System.out.println(findMinimum(N, M));
}
}
|
Python3
def findMinimum(N, M):
if (N = = 0 ):
return 0
ans = 10 * * 9
i = 0
while (i * i < = N):
i + = 1
if (M = = 1 and i = = 0 ):
continue
count = i
tempN = N
while (tempN ! = 0 ):
tempN = tempN / / (M + i)
count + = 1
ans = min (count, ans)
return ans
N = 9
M = 2
print (findMinimum(N, M))
|
C#
using System;
class GFG
{
public static int findMinimum( int N, int M)
{
if (N == 0)
{
return 0;
}
int ans = 1000000007;
for ( int i = 0; i * i <= N; i++)
{
if (M == 1 && i == 0)
{
continue ;
}
int count = i;
int tempN = N;
while (tempN != 0)
{
tempN /= (M + i);
count++;
}
if (count < ans)
{
ans = count;
}
}
return ans;
}
public static void Main()
{
int N = 9;
int M = 2;
Console.WriteLine(findMinimum(N, M));
}
}
|
Javascript
<script>
function findMinimum(N, M)
{
if (N == 0) {
return 0;
}
let ans = Number.MAX_VALUE;
for (let i = 0; i * i <= N; i++) {
if (M == 1 && i == 0) {
continue ;
}
let count = i;
let tempN = N;
while (tempN != 0) {
tempN = Math.floor(tempN / (M + i));
count++;
}
ans = Math.min(count, ans);
}
return ans;
}
let N = 9;
let M = 2;
document.write(findMinimum(N, M));
</script>
|
Time complexity: O(√N*log N )
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...