|
1 |
| ---- |
2 |
| -id: power-of-2 |
3 |
| -title: Power of 2 |
4 |
| -tags: [Easy, Bit Manipulation, GeeksforGeeks, CPP, Python, DSA] |
5 |
| -description: "This tutorial covers the solution to the Power of 2 problem from the GeeksforGeeks website, featuring implementations in Python and C++." |
6 |
| ---- |
7 |
| - |
8 |
| -## Problem Description |
9 |
| - |
10 |
| -Given a non-negative integer `n`, the task is to check if it is a power of 2. |
11 |
| - |
12 |
| -## Examples |
13 |
| - |
14 |
| -**Example 1:** |
15 |
| - |
16 |
| -``` |
17 |
| -Input: n = 1 |
18 |
| -Output: true |
19 |
| -Explanation: 2^0 = 1 |
20 |
| -``` |
21 |
| - |
22 |
| -**Example 2:** |
23 |
| - |
24 |
| -``` |
25 |
| -Input: n = 16 |
26 |
| -Output: true |
27 |
| -Explanation: 2^4 = 16 |
28 |
| -``` |
29 |
| - |
30 |
| -**Example 3:** |
31 |
| - |
32 |
| -``` |
33 |
| -Input: n = 3 |
34 |
| -Output: false |
35 |
| -Explanation: 3 is not a power of 2 |
36 |
| -``` |
37 |
| - |
38 |
| -## Your Task |
39 |
| - |
40 |
| -You don't need to read input or print anything. Your task is to complete the function `isPowerofTwo()` which takes an integer `n` as input and returns `true` if `n` is a power of 2, otherwise `false`. |
41 |
| - |
42 |
| -Expected Time Complexity: $O(1)$ |
43 |
| - |
44 |
| -Expected Auxiliary Space: $O(1)$ |
45 |
| - |
46 |
| -## Constraints |
47 |
| - |
48 |
| -- `0 ≤ n ≤ 10^18` |
49 |
| - |
50 |
| -## Problem Explanation |
51 |
| - |
52 |
| -A number is a power of 2 if there exists an integer `x` such that `n = 2^x`. |
53 |
| - |
54 |
| -## Code Implementation |
55 |
| - |
56 |
| -<Tabs> |
57 |
| - <TabItem value="Python" label="Python" default> |
58 |
| - <SolutionAuthor name="@YourUsername"/> |
59 |
| - |
60 |
| - ```python |
61 |
| - class Solution: |
62 |
| - def isPowerofTwo(self, n: int) -> bool: |
63 |
| - if n <= 0: |
64 |
| - return False |
65 |
| - # A power of two has exactly one bit set in binary representation |
66 |
| - return (n & (n - 1)) == 0 |
67 |
| - |
68 |
| - # Example usage |
69 |
| - if __name__ == "__main__": |
70 |
| - solution = Solution() |
71 |
| - print(solution.isPowerofTwo(16)) # Expected output: True |
72 |
| - print(solution.isPowerofTwo(3)) # Expected output: False |
73 |
| - ``` |
74 |
| - |
75 |
| - </TabItem> |
76 |
| - <TabItem value="C++" label="C++"> |
77 |
| - <SolutionAuthor name="@YourUsername"/> |
78 |
| - |
79 |
| - ```cpp |
80 |
| - #include <bits/stdc++.h> |
81 |
| - using namespace std; |
82 |
| - |
83 |
| - class Solution { |
84 |
| - public: |
85 |
| - // Function to check if given number n is a power of two. |
86 |
| - bool isPowerofTwo(long long n) { |
87 |
| - if (n <= 0) return false; |
88 |
| - // A power of two has exactly one bit set in binary representation |
89 |
| - return (n & (n - 1)) == 0; |
90 |
| - } |
91 |
| - }; |
92 |
| - |
93 |
| - int main() { |
94 |
| - int t; |
95 |
| - cin >> t; // testcases |
96 |
| - |
97 |
| - for (int i = 0; i < t; i++) { |
98 |
| - long long n; // input a number n |
99 |
| - cin >> n; |
100 |
| - Solution ob; |
101 |
| - if (ob.isPowerofTwo(n)) |
102 |
| - cout << "true" << endl; |
103 |
| - else |
104 |
| - cout << "false" << endl; |
105 |
| - } |
106 |
| - return 0; |
107 |
| - } |
108 |
| - ``` |
109 |
| - |
110 |
| - </TabItem> |
111 |
| -</Tabs> |
112 |
| - |
113 |
| -## Solution Logic: |
114 |
| - |
115 |
| -1. A number `n` is a power of 2 if it has exactly one bit set in its binary representation. |
116 |
| -2. To check this, you can use the property: `n & (n - 1) == 0`. |
117 |
| -3. This expression is true only for powers of 2. |
118 |
| - |
119 |
| -## Time Complexity |
120 |
| - |
121 |
| -- The function operates in constant time, $O(1)$. |
122 |
| - |
123 |
| -## Space Complexity |
124 |
| - |
125 |
| -- The function uses constant space, $O(1)$. |
126 |
| - |
127 |
| -## References |
128 |
| - |
129 |
| -- **GeeksforGeeks Problem:** [Power of 2](https://www.geeksforgeeks.org/problems/power-of-2-1587115620/1) |
130 |
| -- **Solution Author:** [arunimad6yuq](https://www.geeksforgeeks.org/user/arunimad6yuq/) |
| 1 | +--- |
| 2 | +id: power-of-2 |
| 3 | +title: Power of 2 |
| 4 | +tags: |
| 5 | + - Easy |
| 6 | + - Bit Manipulation |
| 7 | + - GeeksforGeeks |
| 8 | + - CPP |
| 9 | + - Python |
| 10 | + - DSA |
| 11 | +description: "This tutorial covers the solution to the Power of 2 problem from the GeeksforGeeks." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +Given a non-negative integer `n`, the task is to check if it is a power of 2. |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | + |
| 22 | +``` |
| 23 | +Input: n = 1 |
| 24 | +Output: true |
| 25 | +Explanation: 2^0 = 1 |
| 26 | +``` |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +``` |
| 31 | +Input: n = 16 |
| 32 | +Output: true |
| 33 | +Explanation: 2^4 = 16 |
| 34 | +``` |
| 35 | + |
| 36 | +**Example 3:** |
| 37 | + |
| 38 | +``` |
| 39 | +Input: n = 3 |
| 40 | +Output: false |
| 41 | +Explanation: 3 is not a power of 2 |
| 42 | +``` |
| 43 | + |
| 44 | +## Your Task |
| 45 | + |
| 46 | +You don't need to read input or print anything. Your task is to complete the function `isPowerofTwo()` which takes an integer `n` as input and returns `true` if `n` is a power of 2, otherwise `false`. |
| 47 | + |
| 48 | +Expected Time Complexity: $O(1)$ |
| 49 | + |
| 50 | +Expected Auxiliary Space: $O(1)$ |
| 51 | + |
| 52 | +## Constraints |
| 53 | + |
| 54 | +- `0 ≤ n ≤ 10^18` |
| 55 | + |
| 56 | +## Problem Explanation |
| 57 | + |
| 58 | +A number is a power of 2 if there exists an integer `x` such that `n = 2^x`. |
| 59 | + |
| 60 | +## Code Implementation |
| 61 | + |
| 62 | +<Tabs> |
| 63 | + <TabItem value="Python" label="Python" default> |
| 64 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 65 | + |
| 66 | + ```python |
| 67 | + class Solution: |
| 68 | + def isPowerofTwo(self, n: int) -> bool: |
| 69 | + if n <= 0: |
| 70 | + return False |
| 71 | + # A power of two has exactly one bit set in binary representation |
| 72 | + return (n & (n - 1)) == 0 |
| 73 | + |
| 74 | + # Example usage |
| 75 | + if __name__ == "__main__": |
| 76 | + solution = Solution() |
| 77 | + print(solution.isPowerofTwo(16)) # Expected output: True |
| 78 | + print(solution.isPowerofTwo(3)) # Expected output: False |
| 79 | + ``` |
| 80 | + |
| 81 | + </TabItem> |
| 82 | + <TabItem value="C++" label="C++"> |
| 83 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 84 | + |
| 85 | + ```cpp |
| 86 | + #include <bits/stdc++.h> |
| 87 | + using namespace std; |
| 88 | + |
| 89 | + class Solution { |
| 90 | + public: |
| 91 | + // Function to check if given number n is a power of two. |
| 92 | + bool isPowerofTwo(long long n) { |
| 93 | + if (n <= 0) return false; |
| 94 | + // A power of two has exactly one bit set in binary representation |
| 95 | + return (n & (n - 1)) == 0; |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + int main() { |
| 100 | + int t; |
| 101 | + cin >> t; // testcases |
| 102 | + |
| 103 | + for (int i = 0; i < t; i++) { |
| 104 | + long long n; // input a number n |
| 105 | + cin >> n; |
| 106 | + Solution ob; |
| 107 | + if (ob.isPowerofTwo(n)) |
| 108 | + cout << "true" << endl; |
| 109 | + else |
| 110 | + cout << "false" << endl; |
| 111 | + } |
| 112 | + return 0; |
| 113 | + } |
| 114 | + ``` |
| 115 | + |
| 116 | + </TabItem> |
| 117 | + |
| 118 | + <TabItem value="Javascript" label="Javascript"> |
| 119 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 120 | + |
| 121 | + ```javascript |
| 122 | +class Solution { |
| 123 | + isPowerofTwo(n) { |
| 124 | + if (n <= 0) return false; |
| 125 | + return (n & (n - 1)) == 0; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +let t = parseInt(prompt("Enter number of testcases")); |
| 130 | +for (let i = 0; i < t; i++) { |
| 131 | + let n = parseInt(prompt("Enter a number")); |
| 132 | + let sol = new Solution(); |
| 133 | + console.log(sol.isPowerofTwo(n)); |
| 134 | +} |
| 135 | + |
| 136 | + ``` |
| 137 | + |
| 138 | + </TabItem> |
| 139 | + <TabItem value="Typescript" label="Typescript"> |
| 140 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 141 | + |
| 142 | + ```typescript |
| 143 | +class Solution { |
| 144 | + isPowerofTwo(n: number): boolean { |
| 145 | + if (n <= 0) return false; |
| 146 | + return (n & (n - 1)) == 0; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +let t: number = parseInt(prompt("Enter number of testcases")); |
| 151 | +for (let i: number = 0; i < t; i++) { |
| 152 | + let n: number = parseInt(prompt("Enter a number")); |
| 153 | + let sol: Solution = new Solution(); |
| 154 | + console.log(sol.isPowerofTwo(n)); |
| 155 | +} |
| 156 | + |
| 157 | + ``` |
| 158 | + |
| 159 | + </TabItem> |
| 160 | + |
| 161 | + |
| 162 | + <TabItem value="Java" label="Java"> |
| 163 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 164 | + |
| 165 | + ```java |
| 166 | +import java.util.Scanner; |
| 167 | + |
| 168 | +class Solution { |
| 169 | + boolean isPowerofTwo(long n) { |
| 170 | + if (n <= 0) return false; |
| 171 | + return (n & (n - 1)) == 0; |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +public class Main { |
| 176 | + public static void main(String[] args) { |
| 177 | + Scanner sc = new Scanner(System.in); |
| 178 | + int t = sc.nextInt(); |
| 179 | + for (int i = 0; i < t; i++) { |
| 180 | + long n = sc.nextLong(); |
| 181 | + Solution sol = new Solution(); |
| 182 | + System.out.println(sol.isPowerofTwo(n)); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | + ``` |
| 188 | + |
| 189 | + </TabItem> |
| 190 | +</Tabs> |
| 191 | + |
| 192 | +## Solution Logic: |
| 193 | + |
| 194 | +1. A number `n` is a power of 2 if it has exactly one bit set in its binary representation. |
| 195 | +2. To check this, you can use the property: `n & (n - 1) == 0`. |
| 196 | +3. This expression is true only for powers of 2. |
| 197 | + |
| 198 | +## Time Complexity |
| 199 | + |
| 200 | +- The function operates in constant time, $O(1)$. |
| 201 | + |
| 202 | +## Space Complexity |
| 203 | + |
| 204 | +- The function uses constant space, $O(1)$. |
0 commit comments