|
| 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