Reduce a number to 1 by performing given operations
Last Updated :
24 Jul, 2024
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 (n+1) or (n-1).
You need 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 = 7
Output : 4
7->6
6->3
3->2
2->1
There is one more way to get in 4 steps :
7->8, 8->4, 4->2, 2->1
Simple Recursive Solution:
The idea is to recursively compute the minimum number of steps required.
- If the number is even, then we are allowed to only divide the number by 2.
- But, when the number is Odd, we can either increment or decrement it by 1. So, we will use recursion for both n-1 and n+1 and return the one with the minimum number of operations.
Below is the implementation of the above approach:
C++
// C++ program to count minimum
// steps to reduce a number
#include <cmath>
#include <iostream>
using namespace std;
int countways(int n)
{
if (n == 1)
return 0;
else if (n % 2 == 0)
return 1 + countways(n / 2);
else
return 1 + min(countways(n - 1),
countways(n + 1));
}
// Driver code
int main()
{
int n = 15;
cout << countways(n) << "\n";
return 0;
}
Java
// Java program to count minimum
// steps to reduce a number
class Geeks {
static int countways(int n)
{
if (n == 1)
return 0;
else if (n % 2 == 0)
return 1 + countways(n / 2);
else
return 1 + Math.min(countways(n - 1), countways(n + 1));
}
// Driver code
public static void main(String args[])
{
int n = 15;
System.out.println(countways(n));
}
}
// This code is contributed by ankita_saini
Python3
# Python3 program to count minimum
# steps to reduce a number
def countways(n):
if (n == 1):
return 0;
elif (n % 2 == 0):
return 1 + countways(n / 2);
else:
return 1 + min(countways(n - 1),
countways(n + 1));
# Driver code
n = 15;
print(countways(n));
# This code is contributed by PrinciRaj1992
C#
// C# program to count minimum
// steps to reduce a number
using System;
class GFG {
static int countways(int n)
{
if (n == 1)
return 0;
else if (n % 2 == 0)
return 1 + countways(n / 2);
else
return 1 + Math.Min(countways(n - 1), countways(n + 1));
}
// Driver code
static public void Main()
{
int n = 15;
Console.Write(countways(n));
}
}
// This code is contributed by Raj
JavaScript
<script>
// Javascript program to count minimum
// steps to reduce a number
function countways(n)
{
if (n == 1)
return 0;
else if (n % 2 == 0)
return 1 + countways(n / 2);
else
return 1 + Math.min(countways(n - 1),
countways(n + 1));
}
// Driver code
let n = 15;
document.write(countways(n));
// This code is contributed by unknown2108
</script>
The above-mentioned approach has a time complexity of O(2^n). It is possible to reduce this complexity to O(log n).
Auxiliary Space: O(n), for recursive stack space.
We can optimize this solution using Dynamic Programming. But there is even a more efficient solution described below.
Using a Mathematical Fact
The idea is based on the observation that observation that performing an increment of 1 or a decrement of 1 on an odd number can result in an even number, one of it divisible by 4 (Note that every alternate even number is divisible by 4).
Algorithm :
1. Initialize count = 0
2. While number is greater than one perform following steps -
Perform count++ for each iteration
if num % 2 == 0, perform division
else if num % 4 == 3, perform increment
else perform decrement (as odd % 4 is either 1 or 3)
3. return count;
C++
// C++ program for the above approach
#include <iostream>
using namespace std;
int countSteps(int n)
{
int count = 0;
while (n > 1) {
count++;
// num even, divide by 2
if (n % 2 == 0)
n /= 2;
// num odd, n%4 == 1
// or n==3(special edge case),
// decrement by 1
else if (n % 4 == 1 || n == 3)
n -= 1;
// num odd, n%4 == 3, increment by 1
else
n += 1;
}
return count;
}
// driver code
int main()
{
int n = 15;
// Function call
cout << countSteps(n) << "\n";
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static int countSteps(int n)
{
int count = 0;
while (n > 1) {
count++;
// num even, divide by 2
if (n % 2 == 0)
n /= 2;
// num odd, n%4 == 1
// or n==3(special edge case),
// decrement by 1
else if (n % 4 == 1 || n == 3)
n -= 1;
// num odd, n%4 == 3, increment by 1
else
n += 1;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int n = 15;
// Function call
System.out.print(countSteps(n));
}
}
// This code is contributed by paragpallavsingh
Python
# Python3 program for the above approach
def countSteps(n):
count = 0
while (n > 1):
count += 1
# num even, divide by 2
if (n % 2 == 0):
n //= 2
# num odd, n%4 == 1
# or n==3(special edge case),
# decrement by 1
elif (n % 4 == 1 or n == 3):
n -= 1
# num odd, n%4 == 3, increment by 1
else:
n += 1
return count
# Driver code
if __name__ == "__main__":
n = 15
# Function call
print(countSteps(n))
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG {
public static int countSteps(int n)
{
int count = 0;
while (n > 1) {
count++;
// num even, divide by 2
if (n % 2 == 0)
n /= 2;
// num odd, n%4 == 1
// or n==3(special edge case),
// decrement by 1
else if (n % 4 == 1 || n == 3)
n -= 1;
// num odd, n%4 == 3, increment by 1
else
n += 1;
}
return count;
}
// Driver code
static public void Main()
{
int n = 15;
// Function call
Console.WriteLine(countSteps(n));
}
}
// This code is contributed by avanitrachhadiya2155
JavaScript
// Javascript program for the above approach
function countSteps(n)
{
let count = 0;
while (n > 1) {
count++;
// num even, divide by 2
if (n % 2 == 0)
n = Math.floor(n / 2);
// num odd, n%4 == 1
// or n==3(special edge case),
// decrement by 1
else if (n % 4 == 1 || n == 3)
n -= 1;
// num odd, n%4 == 3, increment by 1
else
n += 1;
}
return count;
}
// Driver code
let n = 15;
console.log(countSteps(n));
Time complexity : O(log N)
Auxiliary Space: O(1)
Similar Reads
Reduce a number to 1 by performing given operations | Set 2
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 -
7 min read
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 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
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
Reduce all array elements to zero by performing given operations thrice
Given an array arr[] of size N, the task is to convert every array element to 0 by applying the following operations exactly three times: Select a subarray.Increment every element of the subarray by the integer multiple of its length. Finally, print the first and last indices of the subarray involve
9 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 1 with minimum number of given operations
Given an integer N, the task is to reduce N to 1 with the following two operations: 1 can be subtracted from each of the digits of the number only if the digit is greater than 0 and the resultant number doesn't have any leading 0s.1 can be subtracted from the number itself. The task is to find the m
6 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 prime number operations to convert A to B
Given two integers A and B, the task is to convert A to B with a minimum number of the following operations: Multiply A by any prime number.Divide A by one of its prime divisors. Print the minimum number of operations required.Examples: Input: A = 10, B = 15 Output: 2 Operation 1: 10 / 2 = 5 Operati
7 min read
Number of operations to reduce Kth element to 0
Given an array arr[] of size N and an integer K, the task is to find the number of operations to reduce the Kth element(0-indexed) to 0. In one operation, the front element, that is arr[0] is decremented by 1 and is removed from the front of the array. If the removed element is still greater than 0,
8 min read