📌  相关文章
📜  可以通过给定运算将数字乘以2的数量的计数

📅  最后修改于: 2021-05-25 02:48:50             🧑  作者: Mango

给定一个数组arr [] ,任务是对可以通过以下操作将其乘以2的幂进行计数:
如果1的乘方尚未为2的幂,则可以最多将其添加一次。

例子:

方法:遍历数组并检查当前元素是否为2的幂,如果是,则更新count = count + 1 。如果不是2的幂,则检查一个大的元素,即arr [i] + 1 。要检查元素是否为2的幂:

  • 天真的方法元素重复除以2,直到得到01作为余数。如果余数是1,则其2的幂,否则不是2的幂。
  • 高效的方法:如果X&(X – 1)= 0,X为2的幂。
    假设X = 16 = 10000,并且X – 1 = 15 = 01111,那么X&(X – 1)= 10000&01111 = 0,X = 16的2的幂。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if x is a power of 2
bool isPowerOfTwo(int x)
{
    if (x == 0)
        return false;
 
    // If x & (x-1) = 0 then x is a power of 2
    if (!(x & (x - 1)))
        return true;
    else
        return false;
}
 
// Function to return the required count
int countNum(int a[], int n)
{
    int count = 0;
 
    for (int i = 0; i < n; i++) {
 
        // If a[i] or (a[i]+1) is a power of 2
        if (isPowerOfTwo(a[i]) || isPowerOfTwo(a[i] + 1))
            count++;
    }
 
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 5, 6, 9, 3, 1 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << countNum(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function that returns true if x is a power of 2
static boolean isPowerOfTwo(int x)
{
    if (x == 0)
        return false;
 
    // If x & (x-1) = 0 then x is a power of 2
    if ((x & (x - 1)) == 0)
        return true;
    else
        return false;
}
 
// Function to return the required count
static int countNum(int a[], int n)
{
    int count = 0;
 
    for (int i = 0; i < n; i++) {
 
        // If a[i] or (a[i]+1) is a power of 2
        if (isPowerOfTwo(a[i]) || isPowerOfTwo(a[i] + 1))
            count++;
    }
 
    return count;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 5, 6, 9, 3, 1 };
 
    int n = arr.length;
 
    System.out.println(countNum(arr, n));
 
}
}
 
// This code is contributed by
// Sahil_Shelangia


Python3
# Python 3 implementation of the approach
 
# Function that returns true if x
# is a power of 2
def isPowerOfTwo(x):
    if (x == 0):
        return False
 
    # If x & (x-1) = 0 then x is a power of 2
    if ((x & (x - 1)) == 0):
        return True
    else:
        return False
 
# Function to return the required count
def countNum(a, n):
    count = 0
 
    for i in range(0, n, 1):
         
        # If a[i] or (a[i]+1) is a power of 2
        if (isPowerOfTwo(a[i]) or
            isPowerOfTwo(a[i] + 1)):
            count += 1
 
    return count
 
# Driver code
if __name__ == '__main__':
    arr = [5, 6, 9, 3, 1]
 
    n = len(arr)
 
    print(countNum(arr, n))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function that returns true if x is a power of 2
static bool isPowerOfTwo(int x)
{
    if (x == 0)
        return false;
 
    // If x & (x-1) = 0 then x is a power of 2
    if ((x & (x - 1)) == 0)
        return true;
    else
        return false;
}
 
// Function to return the required count
static int countNum(int[] a, int n)
{
    int count = 0;
 
    for (int i = 0; i < n; i++)
    {
        // If a[i] or (a[i]+1) is a power of 2
        if (isPowerOfTwo(a[i]) || isPowerOfTwo(a[i] + 1))
            count++;
    }
 
    return count;
}
 
// Driver code
public static void Main()
{
    int[] arr = { 5, 6, 9, 3, 1 };
    int n = arr.Length;
    Console.WriteLine(countNum(arr, n));
 
}
}
 
// This code is contributed by
// Mukul Singh


PHP


Javascript


输出:
2