-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPower_of_Two.java
More file actions
98 lines (68 loc) · 1.82 KB
/
Power_of_Two.java
File metadata and controls
98 lines (68 loc) · 1.82 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Explaination: If number equals or less than 0 then it cant be any power of 2. Now for positive numbers,
it should have only one binary bit as 1 (becaus power of 2 will come as 2^0,2^1,2^2.......).
for example,
1 -> 2^0 -> 0000001
2 -> 2^1 -> 0000010
4 -> 2^2 -> 0000100
8 -> 2^3 -> 0001000
16-> 2^4 -> 0010000
....... and so on.
So here we can take a bitwise and (&) operation between the number and a number less than it,
if it equal to 0 then it means that no other position has high bit, except at one place.
Thus return true, else false.
for ex: take the number, n=16
n (in bits) -> 1 0 0 0 0 (16 is a power of 2 and thus has only one high bit)
n-1 (in bits) -> 0 1 1 1 1 ( n-1 i.e. 15 will make all bits high excpet the 5th bit)
n & n-1 -> 0 0 0 0 0 (& operation will make all the bits to 0, thus its power of 2)
*/
class Solution {
public boolean isPowerOfTwo(int n) {
if(n<=0)
return false;
return ((n&(n-1))==0);
}
}
// Iterative
class Solution {
public boolean isPowerOfTwo(int n) {
if(n==0)
return false;
while(n!=1)
{
if(n%2!=0)
return false;
n = n/2;
}
return true;
}
}
// Recursive
class Solution {
public boolean isPowerOfTwo(int n) {
if(n==1)
return true;
if(n%2!=0 || n==0)
return false;
return isPowerOfTwo(n/2);
}
}
// Bitwise Solution
class Solution {
public boolean isPowerOfTwo(int n) {
int count=0;
while(n>0)
{
if((n & 1) == 1)
count++;
n = n >> 1;
}
return count == 1 ? true : false;
}
}
// One Liner
public class Solution {
public boolean isPowerOfTwo(int n) {
return ((n & n - 1) == 0 && n > 0);
}
}