Find all powers of 2 less than or equal to a given number
Last Updated :
24 Apr, 2023
Given a positive number N, the task is to find out all the perfect powers of two which are less than or equal to the given number N.
Examples:
Input: N = 63
Output: 32 16 8 4 2 1
Explanation: There are total of 6 powers of 2, which are less than or equal to the given number N.
Input: N = 193
Output: 128 64 32 16 8 4 2 1
Explanation: There are total of 8 powers of 2, which are less than or equal to the given number N.
Naive Approach: The idea is to traverse each number from N to 1 and check if it is a perfect power of 2 or not. If yes, then print that number.
Another Approach: The idea is to find all powers of 2 and simply print the powers that are lesser than or equal to N.
Another Approach: The idea is based on the concept that all powers of 2 has all bits set, in its binary form. Bitset function is used in this approach solve the above problem.
Below are the steps:
- Find the largest power of 2(say temp) which is used to evaluate the number less than or equal to N.
- Initialise an bitset array arr[] of maximum size 64, to store the binary representation of the given number N.
- Reset all the bits in the bitset array using reset() function.
- Iterate a loop from total to 0, and sequentially make each bit 1, and find the value of that binary expression and then reset the bit.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
const int MAX = 64;
int max_exponent( int n)
{
return ( int )(log2(n));
}
void all_powers( int N)
{
bitset<64> arr(N);
arr.reset();
int total = max_exponent(N);
for ( int i = total; i >= 0; i--) {
arr.reset(i + 1);
arr.set(i);
cout << arr.to_ulong() << " " ;
}
}
int main()
{
int N = 63;
all_powers(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int max_exponent( int n)
{
return ( int )(Math.log(n) / Math.log( 2 ));
}
static void all_powers( int N)
{
int total = max_exponent(N);
char [] arr = new char [total + 2 ];
for ( int i = 0 ; i <= total + 1 ; i++)
arr[i] = '0' ;
for ( int i = 1 ; i < total + 2 ; i++) {
arr[i - 1 ] = '0' ;
arr[i] = '1' ;
System.out.print(
Integer.parseInt( new String(arr), 2 ) + " " );
}
System.out.print( "\n" );
}
public static void main(String[] args)
{
int N = 63 ;
all_powers(N);
}
}
|
Python3
from math import log
MAX = 64 ;
def max_exponent(n):
return int (log(n, 2 ))
def all_powers(N):
total = max_exponent(N);
arr = [ '0' for _ in range (total + 2 )]
for i in range ( 1 , total + 2 ):
arr[i - 1 ] = '0' ;
arr[i] = '1' ;
print ( int (" ".join(arr), 2), end = " ")
print ()
N = 63 ;
all_powers(N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int max_exponent( int n)
{
return ( int )(Math.Log(n) / Math.Log(2));
}
static void all_powers( int N)
{
int total = max_exponent(N);
char [] arr = new char [total + 2];
for ( int i = 0; i <= total + 1; i++)
arr[i] = '0' ;
for ( int i = 1; i < total + 2; i++)
{
arr[i - 1] = '0' ;
arr[i] = '1' ;
Console.Write(Convert.ToInt32( new string (arr), 2) + " " );
}
Console.Write( "\n" );
}
public static void Main( string [] args)
{
int N = 63;
all_powers(N);
}
}
|
Javascript
let MAX = 64;
function max_exponent(n)
{
return Math.floor(Math.log(n) / Math.log(2))
}
function all_powers(N)
{
let total = max_exponent(N);
let arr = new Array(total + 1);
arr.fill(0)
for ( var i = total; i >= 0; i--) {
arr[i + 1] = 0;
arr[i] = 1;
process.stdout.write( parseInt(arr.join( "" ), 2)/2 + " " );
}
}
let N = 63;
all_powers(N);
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...