给定正整数N ,任务是检查给定整数是否为2的偶数幂。
例子:
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.
天真的方法:最简单的方法是迭代所有2的指数值,并检查相应的值是否为N以及指数是否为偶数。如果两个都成立,则打印“是” 。否则,如果当前指数2超过N ,则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N can be
// expressed as an even power of 2
bool checkEvenPower(int n)
{
int x = 0;
// Iterate till x is N
while (x < n) {
int value = pow(2, x);
if (value == n) {
// if x is even then
// return true
if (x % 2 == 0)
return true;
else
return false;
}
// Increment x
x++;
}
// If N is not a power of 2
return false;
}
// Driver Code
int main()
{
int N = 4;
cout << (checkEvenPower(N) ? "Yes" : "No");
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to check if N can be
// expressed as an even power of 2
static boolean checkEvenPower(int n)
{
int x = 0;
// Iterate till x is N
while (x < n)
{
int value = (int)Math.pow(2, x);
if (value == n)
{
// if x is even then
// return true
if (x % 2 == 0)
return true;
else
return false;
}
// Increment x
x++;
}
// If N is not a power of 2
return false;
}
// Driver Code
public static void main (String[] args)
{
int N = 4;
System.out.println((checkEvenPower(N) ? "Yes" : "No"));
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# Python3 program for the above approach
# Function to check if N can be
# expressed as an even power of 2
def checkEvenPower(n):
x = 0
# Iterate till x is N
while (x < n):
value = pow(2, x)
if (value == n):
# If x is even then
# return true
if (x % 2 == 0):
return True
else:
return False
# Increment x
x += 1
# If N is not a power of 2
return False
# Driver Code
if __name__ == '__main__':
N = 4
if checkEvenPower(N):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if N can be
// expressed as an even power of 2
static bool checkEvenPower(int n)
{
int x = 0;
// Iterate till x is N
while (x < n)
{
int value = (int)Math.Pow(2, x);
if (value == n)
{
// if x is even then
// return true
if (x % 2 == 0)
return true;
else
return false;
}
// Increment x
x++;
}
// If N is not a power of 2
return false;
}
// Driver Code
static public void Main()
{
int N = 4;
Console.Write((checkEvenPower(N) ? "Yes" : "No"));
}
}
// This code is contributed by avijitmondal1998
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N can be
// expressed as an even power of 2 or not
string checkEvenPower(int n)
{
int low = 0, high = n;
// Iterate until low > high
while (low <= high) {
// Calculate mid
int mid = low + (high - low) / 2;
int value = pow(2, mid);
// If 2 ^ mid is equal to n
if (value == n) {
// If mid is odd
if (mid % 2 == 1)
return "No";
else
return "Yes";
}
// Update the value of low
else if (value < n)
low = mid + 1;
// Update the value of high
else
high = mid - 1;
}
// Otherwise
return "No";
}
// Driver Code
int main()
{
int N = 4;
cout << checkEvenPower(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if N can be
// expressed as an even power of 2 or not
static String checkEvenPower(int n)
{
int low = 0, high = n;
// Iterate until low > high
while (low <= high)
{
// Calculate mid
int mid = low + (high - low) / 2;
int value = (int) Math.pow(2, mid);
// If 2 ^ mid is equal to n
if (value == n)
{
// If mid is odd
if (mid % 2 == 1)
return "No";
else
return "Yes";
}
// Update the value of low
else if (value < n)
low = mid + 1;
// Update the value of high
else
high = mid - 1;
}
// Otherwise
return "No";
}
// Driver code
public static void main(String[] args)
{
int N = 4;
System.out.println(checkEvenPower(N));
}
}
// This code is contributed by offbeat
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N can be
// expressed as an even power of 2 or not
bool checkEvenPower(long long int N)
{
// If N is not a power of 2
if ((N & (N - 1)) != 0)
return false;
// Bitwise AND operation
N = N & 0x55555555;
return (N > 0);
}
// Driver Code
int main()
{
int N = 4;
cout << checkEvenPower(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to check if N can be
// expressed as an even power of 2 or not
static boolean checkEvenPower(long N)
{
// If N is not a power of 2
if ((N & (N - 1)) != 0)
return false;
// Bitwise AND operation
N = N & 0x55555555;
return (N > 0);
}
// Driver Code
public static void main (String[] args)
{
long N = 4;
System.out.println(checkEvenPower(N)? 1 : 0);
}
}
// This code is contributed by rag2127
输出:
Yes
时间复杂度: O(log N)
辅助空间: O(1)
另一种方法:上述方法也可以使用二进制搜索来实现。请按照以下步骤解决问题:
- 初始化两个变量,比如低到0和高到N。
- 迭代直到低点超过高点:
- 找到中间低至+的值(高-低)/ 2。
- 如果2 mid的值是N ,并且mid的值是偶数,则打印“是”并跳出循环。
- 如果2 mid
的值,将low的值更新为(mid + 1) 。 - 否则,将high的值更新为(mid – 1) 。
- 完成上述步骤后,如果循环没有中断,请打印“ No” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N can be
// expressed as an even power of 2 or not
string checkEvenPower(int n)
{
int low = 0, high = n;
// Iterate until low > high
while (low <= high) {
// Calculate mid
int mid = low + (high - low) / 2;
int value = pow(2, mid);
// If 2 ^ mid is equal to n
if (value == n) {
// If mid is odd
if (mid % 2 == 1)
return "No";
else
return "Yes";
}
// Update the value of low
else if (value < n)
low = mid + 1;
// Update the value of high
else
high = mid - 1;
}
// Otherwise
return "No";
}
// Driver Code
int main()
{
int N = 4;
cout << checkEvenPower(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if N can be
// expressed as an even power of 2 or not
static String checkEvenPower(int n)
{
int low = 0, high = n;
// Iterate until low > high
while (low <= high)
{
// Calculate mid
int mid = low + (high - low) / 2;
int value = (int) Math.pow(2, mid);
// If 2 ^ mid is equal to n
if (value == n)
{
// If mid is odd
if (mid % 2 == 1)
return "No";
else
return "Yes";
}
// Update the value of low
else if (value < n)
low = mid + 1;
// Update the value of high
else
high = mid - 1;
}
// Otherwise
return "No";
}
// Driver code
public static void main(String[] args)
{
int N = 4;
System.out.println(checkEvenPower(N));
}
}
// This code is contributed by offbeat
Java脚本
输出:
Yes
时间复杂度: O(log N)
辅助空间: O(1)
高效的方法:还可以基于以下观察来优化上述方法:
- 2 s的幂的二进制表示形式如下:
20 = 00….0001
21 = 00….0010
22 = 00….0100
23 = 00….1000
- 从上面的二进制表示中可以看出,对于一个数字为2的幂,它必须仅具有1个置位,并且必须为2的偶数次幂,则唯一的置位应位于奇数位置。
- 因此,可以将这些偶数和奇数功率彼此区分开的数字是5(0101) 。假定该值将适合一个64位长的整数,则当且仅当设置了奇数位(即,具有2的偶数次幂)时, 0x55555555与N的按位与是正数。
因此,其想法是检查0x55555555与N的按位与运算是否为正,然后打印“是”。否则为“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N can be
// expressed as an even power of 2 or not
bool checkEvenPower(long long int N)
{
// If N is not a power of 2
if ((N & (N - 1)) != 0)
return false;
// Bitwise AND operation
N = N & 0x55555555;
return (N > 0);
}
// Driver Code
int main()
{
int N = 4;
cout << checkEvenPower(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to check if N can be
// expressed as an even power of 2 or not
static boolean checkEvenPower(long N)
{
// If N is not a power of 2
if ((N & (N - 1)) != 0)
return false;
// Bitwise AND operation
N = N & 0x55555555;
return (N > 0);
}
// Driver Code
public static void main (String[] args)
{
long N = 4;
System.out.println(checkEvenPower(N)? 1 : 0);
}
}
// This code is contributed by rag2127
输出:
1
时间复杂度: O(1)
辅助空间: O(1)