Count number of step required to reduce N to 1 by following certain rule

Given a positive integerĀ . Find the number of steps required to minimize it to 1. In a single step N either got reduced to half if it is power of 2 else N is reduced to difference of N and its nearest power of 2 which is smaller than N.
Examples:Ā
Input : N = 2 Output : 1
Input : N = 20 Output : 3
Simple Approach: As per question a very simple and brute force approach is to iterate over N until it got reduced to 1, where reduction involve two cases:Ā Ā
- N is power of 2 : reduce n to n/2
- N is not power of 2: reduce n to n ā (2^log2(n))
Efficient approach: Before proceeding to actual result lets have a look over bit representation of an integer n as per problem statement.Ā Ā
- When an integer is power of 2: In this case bit -representation includes only one set bit and that too is left most. Hence log2(n) i.e. bit-position minus One is the number of step required to reduce it to n. Which is also equal to number of set bit in n-1.
- When an integer is not power of 2:The remainder of n ā 2^(log2(n)) is equal to integer which can be obtained by un-setting the left most set bit. Hence, one set bit removal count as one step in this case.
Hence the actual answer for steps required to reduce n is equal to number of set bits in n-1. Which can be easily calculated either by using the loop or any of method described in the post: Count Set bits in an Integer.
Below is the implementation of the above approach:Ā
Ā
C++
// Cpp to find the number of step to reduce n to 1// C++ program to demonstrate __builtin_popcount()#include <iostream>using namespace std;Ā
// Function to return number of steps for reductionint stepRequired(int n){Ā Ā Ā Ā // builtin function to count set bitsĀ Ā Ā Ā return __builtin_popcount(n - 1);}Ā
// Driver programint main(){Ā Ā Ā Ā int n = 94;Ā Ā Ā Ā cout << stepRequired(n) << endl;Ā Ā Ā Ā return 0;} |
Java
// Java program to find the number of step to reduce n to 1Ā
import java.io.*;class GFG{Ā Ā Ā Ā // Function to return number of steps for reductionĀ Ā Ā Ā static int stepRequired(int n)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // builtin function to count set bitsĀ Ā Ā Ā Ā Ā Ā Ā return Integer.bitCount(n - 1);Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā // Driver programĀ Ā Ā Ā public static voidĀ main(String []args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int n = 94;Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(stepRequired(n)); Ā Ā Ā Ā Ā Ā Ā Ā Ā }}Ā
Ā
// This code is contributed by // ihritik |
Python3
# Python3 to find the number of step# to reduce n to 1 # Python3 program to demonstrate# __builtin_popcount() Ā
# Function to return number of# steps for reduction def stepRequired(n) :Ā
Ā Ā Ā Ā # step to count set bits Ā Ā Ā Ā return bin(94).count('1')Ā
# Driver Codeif __name__ == "__main__" :Ā
Ā Ā Ā Ā n = 94Ā Ā Ā Ā print(stepRequired(n))Ā
# This code is contributed by Ryuga |
C#
// C# program to find the number of step to reduce n to 1Ā
using System;class GFG{Ā Ā Ā Ā Ā Ā Ā Ā Ā // function to count set bitsĀ Ā Ā Ā static int countSetBits(int n) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // base case Ā Ā Ā Ā Ā Ā Ā Ā if (n == 0) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if last bit set Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // add 1 else add 0 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return (n & 1) + countSetBits(n >> 1); Ā Ā Ā Ā }Ā Ā Ā Ā // Function to return number of steps for reductionĀ Ā Ā Ā static int stepRequired(int n)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return countSetBits(n - 1);Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā // Driver programĀ Ā Ā Ā public static void Main()Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int n = 94;Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(stepRequired(n)); Ā Ā Ā Ā Ā Ā Ā Ā Ā }}Ā
Ā
// This code is contributed by // ihritik |
PHP
<?php// PHP program to find the number of step to reduce n to 1Ā
// recursive function to // count set bits function countSetBits($n) { Ā Ā Ā Ā // base case Ā Ā Ā Ā if ($n == 0) Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā return 1 +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā countSetBits($n &Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ($n - 1)); } Ā
// Function to return number of steps for reductionfunction stepRequired($n){Ā Ā Ā Ā Ā Ā return countSetBits($n - 1);}Ā Ā Ā Ā Ā // Driver programĀ
$n = 94;echo stepRequired($n); Ā
Ā
Ā
// This code is contributed by // ihritikĀ
?> |
Javascript
<script>Ā Ā Ā Ā // Javascript program to find the number of step to reduce n to 1Ā Ā Ā Ā Ā Ā Ā Ā Ā // function to count set bitsĀ Ā Ā Ā function countSetBits(n) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // base case Ā Ā Ā Ā Ā Ā Ā Ā if (n == 0) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if last bit set Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // add 1 else add 0 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return (n & 1) + countSetBits(n >> 1); Ā Ā Ā Ā }Ā Ā Ā Ā // Function to return number of steps for reductionĀ Ā Ā Ā function stepRequired(n)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return countSetBits(n - 1);Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā let n = 94;Ā Ā Ā Ā Ā Ā document.write(stepRequired(n)); Ā
// This code is contributed by decode2207.</script> |
5
Ā
Time Complexity: O(1) as constant time is taken.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



