-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOfTwo.java
More file actions
36 lines (29 loc) · 778 Bytes
/
PowerOfTwo.java
File metadata and controls
36 lines (29 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public boolean isPowerOfTwo(int n) {
//edge case
if(n<=0)
return false;
// bitwise soln the bit representation of n and n-1 for power of two number will be always zero;
return (n & (n-1)) ==0;
}
}
//class Solution {
// public boolean isPowerOfTwo(int n) {
// return ((n & (n-1)) == 0 && n>0);
// }
// }
//Brtureforce soln
//class Solution {
// private int pow2(int i){
// return (int)Math.pow(2,i);
// }
// public boolean isPowerOfTwo(int n) {
// for(int i=0;i<32;i++){
// if(n==pow2(i))
// return true;
// if(pow2(i)>n+1)
// return false;
// }
// return false;
// }
// }