Minimum decrements or division by a proper divisor required to reduce N to 1
Last Updated :
04 Jun, 2021
Given a positive integer N, the task is to find the minimum number of operations required to reduce N to 1 by repeatedly dividing N by its proper divisors or by decreasing N by 1.
Examples:
Input: N = 9
Output: 3
Explanation:
The proper divisors of N(= 9) are {1, 3}. Following operations are performed to reduced N to 1:
Operation 1: Divide N(= 9) by 3(which is a proper divisor of N(= 9) modifies the value of N to 9/3 = 1.
Operation 2: Decrementing the value of N(= 3) by 1 modifies the value of N to 3 – 1 = 2.
Operation 3: Decrementing the value of N(= 2) by 1 modifies the value of N to 2 – 1 = 1.
Therefore, the total number of operations required is 3.
Input: N = 4
Output: 2
Approach: The given problem can be solved based on the following observations:
- If the value of N is even, then it can be reduced to value 2 by dividing N by N / 2 followed by decrementing 2 to 1. Therefore, the minimum number of steps required is 2.
- Otherwise, the value of N can be made even by decrementing it and can be reduced to 1 using the above steps.
Follow the steps given below to solve the problem
- Initialize a variable, say cnt as 0, to store the minimum number of steps required to reduce N to 1.
- Iterate a loop until N reduces to 1 and perform the following steps:
- If the value of N is equal to 2 or N is odd, then update the value of N = N – 1 and increment cnt by 1.
- Otherwise, update the value of N = N / (N / 2) and increment cnt by 1.
- After completing the above steps, print the value of cnt as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int reduceToOne( long long int N)
{
int cnt = 0;
while (N != 1) {
if (N == 2 or (N % 2 == 1)) {
N = N - 1;
cnt++;
}
else if (N % 2 == 0) {
N = N / (N / 2);
cnt++;
}
}
return cnt;
}
int main()
{
long long int N = 35;
cout << reduceToOne(N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int reduceToOne( long N)
{
int cnt = 0 ;
while (N != 1 )
{
if (N == 2 || (N % 2 == 1 ))
{
N = N - 1 ;
cnt++;
}
else if (N % 2 == 0 )
{
N = N / (N / 2 );
cnt++;
}
}
return cnt;
}
public static void main(String[] args)
{
long N = 35 ;
System.out.println(reduceToOne(N));
}
}
|
Python3
def reduceToOne(N):
cnt = 0
while (N ! = 1 ):
if (N = = 2 or (N % 2 = = 1 )):
N = N - 1
cnt + = 1
elif (N % 2 = = 0 ):
N = N / (N / 2 )
cnt + = 1
return cnt
if __name__ = = '__main__' :
N = 35
print (reduceToOne(N))
|
C#
using System;
class GFG
{
static int reduceToOne( long N)
{
int cnt = 0;
while (N != 1)
{
if (N == 2 || (N % 2 == 1))
{
N = N - 1;
cnt++;
}
else if (N % 2 == 0)
{
N = N / (N / 2);
cnt++;
}
}
return cnt;
}
public static void Main()
{
long N = 35;
Console.WriteLine(reduceToOne(N));
}
}
|
Javascript
<script>
function reduceToOne( N)
{
let cnt = 0;
while (N != 1) {
if (N == 2 || (N % 2 == 1)) {
N = N - 1;
cnt++;
}
else if (N % 2 == 0) {
N = Math.floor(N / Math.floor(N / 2));
cnt++;
}
}
return cnt;
}
let N = 35;
document.write(reduceToOne(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...