Given an integer N, the task is to find the highest power of 2 that is smaller than or equal to N.
Examples:
Input: N = 9
Output: 8
Explanation:
Highest power of 2 less than 9 is 8.
Input: N = -20
Output: -32
Explanation:
Highest power of 2 less than -20 is -32.
Input: N = -84
Output: -128
Approach: The idea is to use logarithm to solve the above problem. For any given number N, it can be either positive or negative. The following task can be performed for each case:
- If the input is positive: floor(log2(N)) can be calculated.
- If the input is negative: ceil(log2(N)) can be calculated and a -ve sign can be added to the value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int powOfPositive( int n)
{
int pos = floor (log2(n));
return pow (2, pos);
}
int powOfNegative( int n)
{
int pos = ceil (log2(n));
return (-1 * pow (2, pos));
}
void highestPowerOf2( int n)
{
if (n > 0)
cout << powOfPositive(n);
else {
n = -n;
cout << powOfNegative(n);
}
}
int main()
{
int n = -24;
highestPowerOf2(n);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int powOfPositive( int n)
{
int pos = floor (log2(n));
return pow (2, pos);
}
int powOfNegative( int n)
{
int pos = ceil (log2(n));
return (-1 * pow (2, pos));
}
void highestPowerOf2( int n)
{
if (n > 0)
printf ( "%d" , powOfPositive(n));
else {
n = -n;
printf ( "%d" , powOfNegative(n));
}
}
int main()
{
int n = -24;
highestPowerOf2(n);
return 0;
}
|
Java
class GFG
{
static int powOfPositive( int n)
{
int pos = ( int )Math.floor((Math.log(n)/Math.log( 2 )));
return ( int )Math.pow( 2 , pos);
}
static int powOfNegative( int n)
{
int pos = ( int )Math.ceil((Math.log(n)/Math.log( 2 )));
return ( int )(- 1 * Math.pow( 2 , pos));
}
static void highestPowerOf2( int n)
{
if (n > 0 )
{
System.out.println(powOfPositive(n));
}
else
{
n = -n;
System.out.println(powOfNegative(n));
}
}
public static void main (String[] args)
{
int n = - 24 ;
highestPowerOf2(n);
}
}
|
C#
using System;
class GFG
{
static int powOfPositive( int n)
{
int pos = ( int )Math.Floor((Math.Log(n)/Math.Log(2)));
return ( int )Math.Pow(2, pos);
}
static int powOfNegative( int n)
{
int pos = ( int )Math.Ceiling((Math.Log(n)/Math.Log(2)));
return ( int )(-1 * Math.Pow(2, pos));
}
static void highestPowerOf2( int n)
{
if (n > 0)
{
Console.WriteLine(powOfPositive(n));
}
else
{
n = -n;
Console.WriteLine(powOfNegative(n));
}
}
public static void Main()
{
int n = -24;
highestPowerOf2(n);
}
}
|
Python3
from math import floor,ceil,log2
def powOfPositive(n) :
pos = floor(log2(n));
return 2 * * pos;
def powOfNegative(n) :
pos = ceil(log2(n));
return ( - 1 * pow ( 2 , pos));
def highestPowerOf2(n) :
if (n > 0 ) :
print (powOfPositive(n));
else :
n = - n;
print (powOfNegative(n));
if __name__ = = "__main__" :
n = - 24 ;
highestPowerOf2(n);
|
Javascript
<script>
function powOfPositive(n)
{
let pos = Math.floor(Math.log2(n));
return Math.pow(2, pos);
}
function powOfNegative(n)
{
let pos = Math.ceil(Math.log2(n));
return (-1 * Math.pow(2, pos));
}
function highestPowerOf2(n)
{
if (n > 0)
{
document.write(powOfPositive(n));
}
else
{
n = -n;
document.write(powOfNegative(n));
}
}
let n = -24;
highestPowerOf2(n);
</script>
|
Time Complexity: O(log2(log2n))
Auxiliary Space: O(1)
Another Approach: we can use Bits manipulation in this way :
1.If number is positive- Then our answer will be pow(2,i) where i is leftmost set bit . Because if number is equal to pow(2,i), Then number is already power of 2, and if number > pow(2,i) , any power of 2 can not be greater than pow(2,i) as in bit concepts.
2.If number is negative- Let i is leftmost set bit .Then our answer will be pow(2,i) if(pow(2,i)==num). else our answer will be pow(2,i+1) because we are taking in negative.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void highestPowerOf2( int n)
{
int set_bits = 0, leftmost, highest;
for ( int i = 0; i <= 32; i++) {
int val = pow (2, i);
int Bitwise_AND = val & abs (n);
if (Bitwise_AND
> 0)
{
leftmost = i;
highest = pow (
2, leftmost);
set_bits++;
}
if (val >= abs (n)) {
break ;
}
}
if (n < 0)
{
if (set_bits
> 1)
{
highest = pow (2, leftmost + 1);
}
highest = -1 * highest;
}
cout << highest;
}
int main()
{
int n = -24;
highestPowerOf2(n);
return 0;
}
|
Java
import java.lang.Math;
class Main {
static void highestPowerOf2( int n) {
int set_bits = 0 , leftmost = 0 , highest = 0 ;
for ( int i = 0 ; i <= 31 ; i++) {
int val = ( int ) Math.pow( 2 , i);
int Bitwise_AND = val & Math.abs(n);
if (Bitwise_AND > 0 ) {
leftmost = i;
highest = ( int ) Math.pow( 2 , leftmost);
set_bits++;
}
if (val >= Math.abs(n)) {
break ;
}
}
if (n < 0 ) {
if (set_bits > 1 ) {
highest = ( int ) Math.pow( 2 , leftmost + 1 );
}
highest = - 1 * highest;
}
System.out.println(highest);
}
public static void main(String[] args) {
int n = - 24 ;
highestPowerOf2(n);
}
}
|
Python3
import math
def highestPowerOf2(n):
set_bits = 0
leftmost = 0
highest = 0
for i in range ( 32 ):
val = int (math. pow ( 2 , i))
Bitwise_AND = val & abs (n)
if (Bitwise_AND > 0 ):
leftmost = i
highest = int (math. pow ( 2 , leftmost))
set_bits + = 1
if (val > = abs (n)):
break
if (n < 0 ):
if (set_bits > 1 ):
highest = int (math. pow ( 2 , leftmost + 1 ))
highest = - 1 * highest
print (highest)
n = - 24
highestPowerOf2(n)
|
C#
using System;
class GFG {
static void highestPowerOf2( int n)
{
int set_bits = 0, leftmost = 0, highest = 0;
for ( int i = 0; i <= 32; i++) {
int val = ( int )Math.Pow(2, i);
int bitwiseAnd
= val & Math.Abs(n);
if (bitwiseAnd > 0) {
leftmost = i;
highest = ( int )Math.Pow(2, leftmost);
set_bits++;
}
if (val >= Math.Abs(n)) {
break ;
}
}
if (n < 0) {
if (set_bits > 1) {
highest = ( int )Math.Pow(
2,
leftmost
+ 1);
}
highest = -1 * highest;
}
Console.WriteLine(highest);
}
public static void Main()
{
int n = -24;
highestPowerOf2(n);
}
}
|
Javascript
function highestPowerOf2(n)
{
let set_bits = 0, leftmost, highest;
for (let i = 0; i <= 32; i++) {
let val = Math.pow(2, i);
let Bitwise_AND
= val & Math.abs(n);
if (Bitwise_AND > 0) {
leftmost = i;
highest = Math.pow(
2, leftmost);
set_bits++;
}
if (val >= Math.abs(n)) {
break ;
}
}
if (n < 0) {
if (set_bits > 1) {
highest = Math.pow(2, leftmost + 1);
}
highest = -1 * highest;
}
console.log(highest);
}
let n = -24;
highestPowerOf2(n);
|
Time Complexity: O(log2n) , Because we are breaking loop , if leftmost set bit is found
Auxiliary Space: O(1)