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: 1
Explanation: The given number can be reduced to 1 by following the following steps:
Divide the number by 2 as N is a power of 2 which modifies the N to 1.
Therefore, the N can be reduced to 1 in only 1 step.
Input: N = 7
Output: 2
Explanation: The given number can be reduced to 1 by following the following steps:
Subtract 4 the greatest power of 2 less than N. After the step the N modifies to N = 7-4 = 3.
Subtract 2 the greatest power of 2 less than N. After the step the N modifies to N = 3-2 = 1.
Therefore, the N can be reduced to 1 in 2 steps.
Method 1 –
Approach: The idea is to recursively compute the minimum number of steps required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int highestPowerof2( int n)
{
int p = ( int )log2(n);
return ( int ) pow (2, p);
}
bool isPowerOfTwo( int n)
{
if (n==0)
return false ;
return ( ceil (log2(n)) == floor (log2(n)));
}
int reduceToOne( int N)
{
if (N == 1){
return 0;
}
if (isPowerOfTwo(N) == true ){
return 1 + reduceToOne(N/2);
}
else {
return 1 + reduceToOne(N - highestPowerof2(N));
}
}
int main()
{
int N = 7;
cout << reduceToOne(N) << endl;
}
|
Java
class GFG {
static int highestPowerof2( int n)
{
int p = ( int )(Math.log(n) / Math.log( 2 ));
return ( int )Math.pow( 2 , p);
}
static boolean isPowerOfTwo( int n)
{
if (n== 0 )
return false ;
return ( int )(Math.ceil((Math.log(n) / Math.log( 2 )))) ==
( int )(Math.floor(((Math.log(n) / Math.log( 2 )))));
}
static int reduceToOne( int N)
{
if (N == 1 ){
return 0 ;
}
if (isPowerOfTwo(N) == true ){
return 1 + reduceToOne(N/ 2 );
}
else {
return 1 + reduceToOne(N - highestPowerof2(N));
}
}
public static void main(String [] args)
{
int N = 7 ;
System.out.println(reduceToOne(N));
}
}
|
Python
import math
def Log2(x):
if x = = 0 :
return false;
return (math.log10(x) /
math.log10( 2 ));
def isPowerOfTwo(n):
return (math.ceil(Log2(n)) = =
math.floor(Log2(n)));
def highestPowerof2(n):
p = int (math.log(n, 2 ));
return int ( pow ( 2 , p));
def reduceToOne(N):
if (N = = 1 ):
return 0
if (isPowerOfTwo(N) = = True ):
return 1 + reduceToOne(N / 2 )
else :
return 1 + reduceToOne(N - highestPowerof2(N))
N = 7 ;
print (reduceToOne(N))
|
C#
using System;
class GFG {
static int highestPowerof2( int n)
{
int p = ( int )(Math.Log(n) / Math.Log(2));
return ( int )Math.Pow(2, p);
}
static bool isPowerOfTwo( int n)
{
if (n == 0)
return false ;
return ( int )(Math.Ceiling((Math.Log(n) / Math.Log(2)))) ==
( int )(Math.Floor(((Math.Log(n) / Math.Log(2)))));
}
static int reduceToOne( int N)
{
if (N == 1){
return 0;
}
if (isPowerOfTwo(N) == true ){
return 1 + reduceToOne(N/2);
}
else {
return 1 + reduceToOne(N - highestPowerof2(N));
}
}
public static void Main()
{
int N = 7;
Console.Write(reduceToOne(N));
}
}
|
Javascript
<script>
function isPowerOfTwo(n)
{
if (n == 0)
return false ;
return parseInt( (Math.ceil((Math.log(n) / Math.log(2))))) == parseInt( (Math.floor(((Math.log(n) / Math.log(2))))));
}
function highestPowerof2(n)
{
let p = parseInt(Math.log(n) / Math.log(2), 10);
return Math.pow(2, p);
}
function reduceToOne(N)
{
if (N == 1){
return 0;
}
if (isPowerOfTwo(N) == true ){
return 1 + reduceToOne(N/2);
}
else {
return 1 + reduceToOne(N - highestPowerof2(N));
}
}
let N = 7;
document.write(reduceToOne(N));
</script>
|
Method 2 –
Approach: The given problem can be solved using bitwise operators. Follow the steps below to solve the problem:
- Initialize two variables say res with value 0 and MSB with value log2(N) to store the number of steps needed to reduce the number to 1 and the most significant bit of N.
- Iterate while N is not equal to 1:
- Finally, print res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int reduceToOne( int N)
{
int MSB = log2(N);
int res = 0;
while (N != 1) {
res++;
if (N & (N - 1) == 0) {
N /= 2;
}
else {
N -= (1 << MSB);
}
MSB--;
}
return res;
}
int main()
{
int N = 7;
cout << reduceToOne(N) << endl;
}
|
Java
import java.io.*;
class GFG {
public static int logarithm( int number, int base)
{
int res = ( int )(Math.log(number) / Math.log(base));
return res;
}
public static int reduceToOne( int N)
{
int MSB = logarithm(N, 2 );
int res = 0 ;
while (N != 1 ) {
res++;
if ((N & (N - 1 )) == 0 ) {
N /= 2 ;
}
else {
N -= ( 1 << MSB);
}
MSB--;
}
return res;
}
public static void main(String[] args)
{
int N = 7 ;
int res = reduceToOne(N);
System.out.println(res);
}
}
|
Python3
import math
def reduceToOne(N):
MSB = math.floor(math.log2(N))
res = 0
while (N ! = 1 ):
res + = 1
if (N & (N - 1 ) = = 0 ):
N / / = 2
else :
N - = ( 1 << MSB)
MSB - = 1
return res
N = 7
print (reduceToOne(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int reduceToOne( int N)
{
int MSB = ( int )(Math.Log(N)/Math.Log(2));
int res = 0;
while (N != 1) {
res++;
if ((N & (N - 1)) == 0) {
N /= 2;
}
else {
N -= (1 << MSB);
}
MSB--;
}
return res;
}
public static void Main()
{
int N = 7;
Console.Write(reduceToOne(N));
}
}
|
Javascript
<script>
function logarithm(number, base)
{
let res = (Math.log(number) / Math.log(base));
return res;
}
function reduceToOne(N)
{
let MSB = logarithm(N, 2);
let res = 0;
while (N != 1) {
res++;
if ((N & (N - 1)) == 0) {
N /= 2;
}
else {
N -= (1 << MSB);
}
MSB--;
}
return res;
}
let N = 7;
let res = reduceToOne(N);
document.write(res);
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Last Updated :
27 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...