Power of 2

Amy has written N numbers in her maths notebook. She is interested in knowing how many of them can be represented in the form of 2^x (^ means power).

Example:

Input:  n = 5, arr = [1,2,3,4,5]
Output: 3

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long powerOfTwo(long long nlong long arr[])
{
    long long cnt = 0;
    for (long long i = 0i < ni++)
    {
        long long x = arr[i];

        long long flag = 0;
        while (x != 1)
        {
            if (x & 1)
            {
                flag = 1;
                break;
            }
            x = x / 2;
        }
        if (flag == 0)
            cnt++;
    }
    return cnt;
}
int main()
{
    long long n = 5;

    long long arr[] = {12345};

    cout << powerOfTwo(narr<< "\n";

    return 0;
}


No comments:

Post a Comment