Reduce a number to 1 by performing given operations | Set 2
Last Updated :
12 Jul, 2025
Given an integer N. The task is to reduce the given number N to 1 in minimum number of given operations. You can perform any one of the below operations in each step.
- If the number is even then you can divide the number by 2.
- If the number is odd then you are allowed to perform either (N + 1) or (N - 1).
The task is to print the minimum number of steps required to reduce the number N to 1 by performing the above operations.
Examples:
Input: N = 15
Output: 5
15 is odd 15 + 1 = 16
16 is even 16 / 2 = 8
8 is even 8 / 2 = 4
4 is even 4 / 2 = 2
2 is even 2 / 2 = 1
Input: N = 4
Output: 2
Approach: A recursive approach to solve the above problem has already been discussed in this article. In this article, an even optimised approach will be discussed.
The first step towards the solution is to realize that you're allowed to remove the LSB only if it's zero i.e. the operation of the first type. Now, what about the odd numbers. One may think that you just need to remove as many 1's as possible to increase the evenness of the number which is not correct, for example:
111011 -> 111010 -> 11101 -> 11100 -> 1110 -> 111 -> 1000 -> 100 -> 10 -> 1
And yet, this is not the best way because
111011 -> 111100 -> 11110 -> 1111 -> 10000 -> 1000 -> 100 -> 10 -> 1
Both 111011 -> 111010 and 111011 -> 111100 remove the same number of 1's, but the second way is better.
So, maximum number of 1's have to be removed, doing +1 in case of a tie will fail for the testcase when n = 3 because 11 -> 10 -> 1 is better than 11 -> 100 -> 10 -> 1. Fortunately, that's the only exception.
So the logic is:
- If N is even.
- Perform the first operation i.e. division by 2.
- If N is odd.
- If N = 3 or (N - 1) has less number of 1's than (N + 1).
- else
Below is the implementation of the above approach:
CPP
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the number
// of set bits in n
int set_bits(int n)
{
int count = 0;
while (n) {
count += n % 2;
n /= 2;
}
return count;
}
// Function to return the minimum
// steps required to reach 1
int minSteps(int n)
{
int ans = 0;
while (n != 1) {
// If n is even then divide it by 2
if (n % 2 == 0)
n /= 2;
// If n is 3 or the number of set bits
// in (n - 1) is less than the number
// of set bits in (n + 1)
else if (n == 3
or set_bits(n - 1) < set_bits(n + 1))
n--;
else
n++;
// Increment the number of steps
ans++;
}
// Return the minimum number of steps
return ans;
}
// Driver code
int main()
{
int n = 15;
cout << minSteps(n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the number
// of set bits in n
static int set_bits(int n)
{
int count = 0;
while (n > 0)
{
count += n % 2;
n /= 2;
}
return count;
}
// Function to return the minimum
// steps required to reach 1
static int minSteps(int n)
{
int ans = 0;
while (n != 1)
{
// If n is even then divide it by 2
if (n % 2 == 0)
n /= 2;
// If n is 3 or the number of set bits
// in (n - 1) is less than the number
// of set bits in (n + 1)
else if (n == 3
|| set_bits(n - 1) < set_bits(n + 1))
n--;
else
n++;
// Increment the number of steps
ans++;
}
// Return the minimum number of steps
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 15;
System.out.print(minSteps(n));
}
}
// This code is contributed by PrinciRaj1992
Python
# Python3 implementation of the approach
# Function to return the number
# of set bits in n
def set_bits(n):
count = 0
while (n):
count += n % 2
n //= 2
return count
# Function to return the minimum
# steps required to reach 1
def minSteps(n):
ans = 0
while (n != 1):
# If n is even then divide it by 2
if (n % 2 == 0):
n //= 2
# If n is 3 or the number of set bits
# in (n - 1) is less than the number
# of set bits in (n + 1)
elif (n == 3 or set_bits(n - 1) < set_bits(n + 1)):
n -= 1
else:
n += 1
# Increment the number of steps
ans += 1
# Return the minimum number of steps
return ans
# Driver code
n = 15
print(minSteps(n))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the number
// of set bits in n
static int set_bits(int n)
{
int count = 0;
while (n > 0)
{
count += n % 2;
n /= 2;
}
return count;
}
// Function to return the minimum
// steps required to reach 1
static int minSteps(int n)
{
int ans = 0;
while (n != 1)
{
// If n is even then divide it by 2
if (n % 2 == 0)
n /= 2;
// If n is 3 or the number of set bits
// in (n - 1) is less than the number
// of set bits in (n + 1)
else if (n == 3
|| set_bits(n - 1) < set_bits(n + 1))
n--;
else
n++;
// Increment the number of steps
ans++;
}
// Return the minimum number of steps
return ans;
}
// Driver code
public static void Main(String[] args)
{
int n = 15;
Console.Write(minSteps(n));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript implementation of the approach
// Function to return the number
// of set bits in n
function set_bits(n)
{
let count = 0;
while (n) {
count += n % 2;
n = parseInt(n / 2);
}
return count;
}
// Function to return the minimum
// steps required to reach 1
function minSteps(n)
{
let ans = 0;
while (n != 1) {
// If n is even then divide it by 2
if (n % 2 == 0)
n = parseInt(n / 2);
// If n is 3 or the number of set bits
// in (n - 1) is less than the number
// of set bits in (n + 1)
else if (n == 3
|| set_bits(n - 1) < set_bits(n + 1))
n--;
else
n++;
// Increment the number of steps
ans++;
}
// Return the minimum number of steps
return ans;
}
// Driver code
let n = 15;
document.write(minSteps(n));
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Reduce a number to 1 by performing given operations | Set 3 Given an integer N, the task is to find the number of steps required to reduce the given number N to 1 by performing the following operations: If the number is a power of 2, then divide the number by 2.Otherwise, subtract the greatest power of 2 smaller than N from N. Examples: Input: N = 2 Output:
10 min read
Reduce a number to 1 by performing given operations Given a number N. The task is to reduce the given number N to 1 in the minimum number of steps. You can perform any one of the below operations in each step.Operation 1: If the number is even then you can divide the number by 2.Operation 2: If the number is odd then you are allowed to perform either
7 min read
Reduce N to 1 by given operations Given an integer N. Then your task is to output a minimum number of operations to reduce N into 1. You can below operations to do the same: Subtract 1 from NUpdate N to N/2, if N is divisible by 2Update N to N/3, if N is divisible by 3Examples: Input: N = 10Output: 3Explanation: The operations are p
5 min read
Minimum steps to reduce N to 0 by given operations Give an integer N, the task is to find the minimum number of moves to reduce N to 0 by one of the following operations: Reduce N by 1.Reduce N to (N/2), if N is divisible by 2.Reduce N to (N/3), if N is divisible by 3. Examples: Input: N = 10Output: 4Explanation: Here N = 10Step 1: Reducing N by 1 i
11 min read
Minimum number of given operations required to be performed to reduce N to 0 Given an integer N, the task is to reduce N to 0 in the minimum number of operations using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of N.Change the ith bit in the binary representation of N if the (i-1)th bit is set to 1 and the (i-2)t
5 min read
Reduce N to 0 or less by given X and Y operations Given three integers N, X, and Y, the task is to check if it is possible to reduce N to 0 or less by the following operations: Update N to ?N/2? + 10, at most X timesUpdate N to N - 10, at most Y times. Example: Input: N = 100, X = 3, Y = 4Output: YesExplanation:Update N = 100 to ?100/2? + 10 = 60.U
6 min read