Quoestion: ClickHere
Example1:
Input: 1
Output: true
Explanation: 2^0 = 1
Example2:
Input:4
Output:true
Explanation:2^2
Example3:
Input:1000
Output:False
Explanation:1000 cannot be expressed as power of two
Algorithm:
step1:check if n is 1 or not if yes return True
step2:check if n is odd if yes return False(This because every number that is power of 2 should be even)
step3:if all the above conditions are false then update n = n/2 and repeat above steps
Python Code:
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if n==1: #Check If n==1 or Not
return True
while n >= 2:
if n % 2 != 0: #Check n is Even or Odd
return False
if n == 2:
return True
else:
n = n/2
if n < 2:
return False
0 Comments