Program to check if a number can be expressed as an even power of 2 or not
Last Updated :
18 Jun, 2021
Given a positive integer N, the task is to check whether the given integer is an even power of 2 or not.
Examples:
Input: N = 4
Output: Yes
Explanation: 4 can be expressed as 22 = 4, which is an even number power of 2.
Input: N = 8
Output: No
Explanation: 8 can be expressed as 23 = 8, which is an odd power of 2.
Naive Approach: The simplest approach is to iterate over all exponent values of 2 and check if the corresponding value is N and the exponent is even or not. If both are found to be true, then print “Yes”. Otherwise, if current exponent of 2 exceeds N, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkEvenPower( int n)
{
int x = 0;
while (x < n) {
int value = pow (2, x);
if (value == n) {
if (x % 2 == 0)
return true ;
else
return false ;
}
x++;
}
return false ;
}
int main()
{
int N = 4;
cout << (checkEvenPower(N) ? "Yes" : "No" );
}
|
Java
import java.io.*;
class GFG{
static boolean checkEvenPower( int n)
{
int x = 0 ;
while (x < n)
{
int value = ( int )Math.pow( 2 , x);
if (value == n)
{
if (x % 2 == 0 )
return true ;
else
return false ;
}
x++;
}
return false ;
}
public static void main (String[] args)
{
int N = 4 ;
System.out.println((checkEvenPower(N) ? "Yes" : "No" ));
}
}
|
Python3
def checkEvenPower(n):
x = 0
while (x < n):
value = pow ( 2 , x)
if (value = = n):
if (x % 2 = = 0 ):
return True
else :
return False
x + = 1
return False
if __name__ = = '__main__' :
N = 4
if checkEvenPower(N):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool checkEvenPower( int n)
{
int x = 0;
while (x < n)
{
int value = ( int )Math.Pow(2, x);
if (value == n)
{
if (x % 2 == 0)
return true ;
else
return false ;
}
x++;
}
return false ;
}
static public void Main()
{
int N = 4;
Console.Write((checkEvenPower(N) ? "Yes" : "No" ));
}
}
|
Javascript
<script>
function checkEvenPower(n)
{
var x = 0;
while (x < n)
{
var value = Math.pow(2, x);
if (value == n)
{
if (x % 2 == 0)
return true ;
else
return false ;
}
x++;
}
return false ;
}
var N = 4;
document.write((checkEvenPower(N) ? "Yes" : "No" ));
</script>
|
Time Complexity: O(log N)
Auxiliary Space : O(1)
Another Approach: The above approach can also be implemented using Binary Search. Follow the steps below to solve the problem:
- Initialize two variables, say low as 0 and high as N.
- Iterate until low exceeds high:
- Find the value of mid as low + (high – low)/2.
- If the value of 2mid is N, and the value of mid is even, then print “Yes” and break out of the loop.
- If the value of 2mid < N, update the value of low as (mid + 1).
- Otherwise, update the value of high as (mid – 1).
- After completing the above steps, if the loop doesn’t break, then print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string checkEvenPower( int n)
{
int low = 0, high = n;
while (low <= high) {
int mid = low + (high - low) / 2;
int value = pow (2, mid);
if (value == n) {
if (mid % 2 == 1)
return "No" ;
else
return "Yes" ;
}
else if (value < n)
low = mid + 1;
else
high = mid - 1;
}
return "No" ;
}
int main()
{
int N = 4;
cout << checkEvenPower(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String checkEvenPower( int n)
{
int low = 0 , high = n;
while (low <= high)
{
int mid = low + (high - low) / 2 ;
int value = ( int ) Math.pow( 2 , mid);
if (value == n)
{
if (mid % 2 == 1 )
return "No" ;
else
return "Yes" ;
}
else if (value < n)
low = mid + 1 ;
else
high = mid - 1 ;
}
return "No" ;
}
public static void main(String[] args)
{
int N = 4 ;
System.out.println(checkEvenPower(N));
}
}
|
Python3
def checkEvenPower(n):
low, high = 0 , n
while (low < = high):
mid = low + (high - low) / 2
value = pow ( 2 , mid)
if (value = = n):
if (mid % 2 = = 1 ):
return "No"
else :
return "Yes"
elif (value < n):
low = mid + 1
else :
high = mid - 1
return "No"
N = 4
print (checkEvenPower(N))
|
C#
using System;
public class GFG
{
static String checkEvenPower( int n)
{
int low = 0, high = n;
while (low <= high)
{
int mid = low + (high - low) / 2;
int value = ( int ) Math.Pow(2, mid);
if (value == n)
{
if (mid % 2 == 1)
return "No" ;
else
return "Yes" ;
}
else if (value < n)
low = mid + 1;
else
high = mid - 1;
}
return "No" ;
}
public static void Main(String[] args)
{
int N = 4;
Console.WriteLine(checkEvenPower(N));
}
}
|
Javascript
<script>
function checkEvenPower(n)
{
var low = 0, high = n;
while (low <= high) {
var mid = low + (high - low) / 2;
var value = parseInt( Math.pow(2, mid));
if (value == n)
{
if (mid % 2 == 1)
return "No" ;
else
return "Yes" ;
}
else if (value < n)
low = mid + 1;
else
high = mid - 1;
}
return "No" ;
}
var N = 4;
document.write(checkEvenPower(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized based on the following observations:
- Binary representation of power of 2s are of the following form:
20 = 00….0001
21 = 00….0010
22 = 00….0100
23 = 00….1000
- The observation from the above binary representations is that for a number to be the power of 2, it must have only 1 set bit and to be an even power of 2, then the only set bit should be at the odd position.
- Therefore, the number that can differentiate these even and odd powers from each other is 5 (0101). Assuming that the value is going to fit in a 64-bit long integer, the Bitwise AND of 0x55555555 with N, is a positive number if and only if an odd bit is set, i.e., having even power of 2.
Therefore, the idea is to check if Bitwise AND of 0x55555555 and N is positive, then print “Yes”. Otherwise “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkEvenPower( long long int N)
{
if ((N & (N - 1)) != 0)
return false ;
N = N & 0x55555555;
return (N > 0);
}
int main()
{
int N = 4;
cout << checkEvenPower(N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static boolean checkEvenPower( long N)
{
if ((N & (N - 1 )) != 0 )
return false ;
N = N & 0x55555555 ;
return (N > 0 );
}
public static void main (String[] args)
{
long N = 4 ;
System.out.println(checkEvenPower(N)? 1 : 0 );
}
}
|
Python3
def checkEvenPower(N):
if ((N & (N - 1 )) ! = 0 ):
return false
N = N & 0x55555555
return (N > 0 )
N = 4
print ( 1 if checkEvenPower(N) else 0 )
|
C#
using System;
public class GFG {
static bool checkEvenPower( long N) {
if ((N & (N - 1)) != 0)
return false ;
N = N & 0x55555555;
return (N > 0);
}
public static void Main(String[] args) {
long N = 4;
Console.WriteLine(checkEvenPower(N) ? 1 : 0);
}
}
|
Javascript
<script>
function checkEvenPower(N)
{
if ((N & (N - 1)) != 0)
return false ;
N = N & 0x55555555;
return (N > 0);
}
let N = 4;
document.write(checkEvenPower(N)? 1 : 0);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...