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.
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
Method 1 –
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 above approach:
The above-mentioned approach has a time complexity of O(2^n). It is possible to reduce this complexity to O(log n).
Method 2 – (Efficient Solution)
It is clear with little observation that performing an increment of 1 or a decrement of 1 on an odd number can result to an even number, one of it divisible by 4. For an odd number the only operation possible is either an increment of 1 or a decrement of 1, most certainly one operation will result in a number divisible by four, this is the optimal choice clearly.
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;
Time complexity : O( logN )
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum score possible after performing given operations on an Array
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Minimize Cost to reduce the Array to a single element by given operations
- Minimum steps to reduce N to 0 by given operations
- Number of array elements derivable from D after performing certain operations
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Minimum number of operations required to reduce N to 0
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Minimize given flips required to reduce N to 0
- Count number of ways to convert string S to T by performing K cyclic shifts
- Maximum number of given operations to remove the entire string
- Number from a range [L, R] having Kth minimum cost of conversion to 1 by given operations
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Kth character from the Nth string obtained by the given operations
- Mth bit in Nth binary string from a sequence generated by the given operations
- Minimum Steps to obtain N from 1 by the given operations
- Find minimum number of merge operations to make an array palindrome
- Minimum number operations required to convert n to m | Set-2
- Minimize operations required to obtain N
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.