The Wayback Machine - https://web.archive.org/web/20220325225217/https://www.geeksforgeeks.org/minimize-operations-to-reduce-n-to-0-by-replacing-n-by-its-divisor-at-each-step/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimize operations to reduce N to 0 by replacing N by its divisor at each step

  • Last Updated : 24 Mar, 2022

Given a positive integer N. Find the minimum number of operations needed to reduce N to 0 when N can reduced by its divisor at each operation.

Example:

Input: N = 5
Output: 4
Explanation: 
Reduce 5 as 5-1=4.
Reduce 4 as 4-2=2.
Reduce 2 as 2-1=1.
Reduce 1 as 1-1=0.

Input: N = 8
Output: 4
Explanation:
Reduce 8 as 8-4=4.
Reduce 4 as 4-2=2.
Reduce 2 as 2-1=1.
Reduce 1 as 1-1=0.

 

Naive Approach:

One easy approach is to find highest divisor of N each time until N becomes 0.

Follow the below steps to solve this problem:

  • Traverse until N becomes 0.
  •  Find the highest divisor of N and subtract from N.
  • Count the number of iterations required for N to become 0 this way.
  •  Return the count calculated above as the final answer.

C++14




// C++ program to minimize operations
// to reduce N to 0 by replacing
// N by its divisor at each step
 
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
ll findElement(ll N)
{
    for (ll i = 2; i * i <= N; i++) {
        if (N % i == 0)
            return N / i;
    }
    return 1;
}
 
// Function to count minimum number
// of operation
ll minOperations(ll N)
{
    if (N < 0)
        return -1;
 
    ll count = 0;
    while (N) {
        ll divisor = findElement(N);
        N -= divisor;
        count++;
    }
    return count;
}
 
// Driver code
int main()
{
    ll N = 5;
    cout << minOperations(N);
 
    return 0;
}

Javascript




<script>
    // JavaScript program to minimize operations
    // to reduce N to 0 by replacing
    // N by its divisor at each step
    const findElement = (N) => {
        for (let i = 2; i * i <= N; i++) {
            if (N % i == 0)
                return parseInt(N / i);
        }
        return 1;
    }
 
    // Function to count minimum number
    // of operation
    const minOperations = (N) => {
        if (N < 0)
            return -1;
 
        let count = 0;
        while (N) {
            let divisor = findElement(N);
            N -= divisor;
            count++;
        }
        return count;
    }
 
    // Driver code
    let N = 5;
    document.write(minOperations(N));
 
// This code is contributed by rakeshsahni
 
</script>
Output:
4

Time Complexity: O(N^(3/2))
Auxiliary Space: O(1)

Efficient Approach: Above solution can be optimized using pre-computation using Sieve of Eratosthenes for smallest prime factor and modifying it a bit for storing highest factor of N.

Follow the below steps to solve this problem: 

  • Precompute sieve using Sieve of Eratosthenes for least prime factor of numbers till N
  • Modify above sieve by storing 1 for all zero values else i/sieve[i] for every i-th value
  • Traverse until N becomes 0.
  • Subtract sieve[N] for every iteration of N.
  • Count the number of iterations required for N to become 0 this way.
  • Return the count calculated above as the final answer.

C++14




// C++ program to minimize operations
// to reduce N to 0 by replacing
// N by its divisor at each step
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 10000001
ll sieve[MAX];
 
// Function to calculate sieve
void makeSieve()
{
    ll i, j;
    sieve[0] = 0;
    sieve[1] = 1;
 
    for (i = 2; i < MAX; i++)
        sieve[i] = 0;
 
    for (i = 2; i * i <= MAX; i++) {
        if (!sieve[i]) {
            sieve[i] = i;
            for (j = i * i; j < MAX; j += i)
                if (!sieve[j])
                    sieve[j] = i;
        }
    }
    for (i = 2; i < MAX; i++) {
        if (!sieve[i])
            sieve[i] = 1;
        else
            sieve[i] = i / sieve[i];
    }
}
 
// Function to count minimum operations
ll minOperations(ll& N)
{
    if (N < 0)
        return -1;
 
    ll count = 0;
    makeSieve();
    while (N) {
        N -= sieve[N];
        count++;
    }
    return count;
}
 
// Driver Code
int main()
{
    ll N = 8;
    cout << minOperations(N);
 
    return 0;
}
Output:
4

Time Complexity: O(N * log(log N)
Auxiliary Space: O(MAX), where MAX is the limit for sieve (here MAX = 10000001).


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!