Minimize operations to reduce N to 0 by replacing N by its divisor at each step
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 operationll minOperations(ll N){ if (N < 0) return -1; ll count = 0; while (N) { ll divisor = findElement(N); N -= divisor; count++; } return count;}// Driver codeint 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> |
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 10000001ll sieve[MAX];// Function to calculate sievevoid 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 operationsll minOperations(ll& N){ if (N < 0) return -1; ll count = 0; makeSieve(); while (N) { N -= sieve[N]; count++; } return count;}// Driver Codeint main(){ ll N = 8; cout << minOperations(N); return 0;} |
4
Time Complexity: O(N * log(log N)
Auxiliary Space: O(MAX), where MAX is the limit for sieve (here MAX = 10000001).

