Power of Two

Posted: 22 Nov, 2020
Difficulty: Easy


Try Problem

You have been given an integer 'N'. Your task is to return true if it is a power of two. Otherwise, return false.

An integer 'N' is a power of two, if it can be expressed as 2 ^ 'K' where 'K' is an integer.

Input Format:
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.

The only line of each test case contains an integer 'N'.
Output Format:
For each test case, the only line of output prints whether the integer ‘N’ is a power of 2 or not.

The output for each test case is in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 100
-2^31 <= N <= 2^31 - 1

Time Limit: 1sec
Approach 1

Recursive approach is to keep dividing the number by two, i.e. do ‘N’ = ‘N’/2 recursively. In any case, if ‘N’%2 becomes non-zero and ‘N’ is not 1 then ‘N’ is not a power of 2. If ‘N' becomes 1 then it is a power of 2.

Try Problem