Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 844 Bytes

File metadata and controls

51 lines (38 loc) · 844 Bytes

342 Power of Four

Description

link


Solution

Valid Number:

1
100
10000
1000000
100000000
10000000000
1000000000000
100000000000000
10000000000000000
1000000000000000000
100000000000000000000
10000000000000000000000
1000000000000000000000000
100000000000000000000000000
10000000000000000000000000000
1000000000000000000000000000000

Any other number not it the list should be considered as invalid. So if you XOR them altogether, you will get a mask value, which is:

1010101010101010101010101010101 (1431655765)

num &(num-1) == 0 is making sure that 2 - based is not included in ans


Code

O(1)

class Solution:
    def isPowerOfFour(self, num: int) -> bool:
        return num != 0 and num &(num-1) == 0 and num & 1431655765 == num