|
| 1 | +--- |
| 2 | +id: counting-bits |
| 3 | +title: Counting Bits |
| 4 | +sidebar_label: 338-Counting Bits |
| 5 | +tags: |
| 6 | + - Arrays |
| 7 | + - Bit Manipulation |
| 8 | + - LeetCode |
| 9 | + - Java |
| 10 | + - Python |
| 11 | + - C++ |
| 12 | +description: "This is a solution to the Counting Bits problem on LeetCode." |
| 13 | +sidebar_position: 5 |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +Given an integer `n`, return an array `ans` of length `n + 1` such that for each `i` (0 <= `i` <= `n`), `ans[i]` is the number of `1`s in the binary representation of `i`. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +``` |
| 25 | +Input: n = 2 |
| 26 | +Output: [0, 1, 1] |
| 27 | +Explanation: |
| 28 | +0 --> 0 |
| 29 | +1 --> 1 |
| 30 | +2 --> 10 |
| 31 | +``` |
| 32 | + |
| 33 | +**Example 2:** |
| 34 | + |
| 35 | +``` |
| 36 | +Input: n = 5 |
| 37 | +Output: [0, 1, 1, 2, 1, 2] |
| 38 | +Explanation: |
| 39 | +0 --> 0 |
| 40 | +1 --> 1 |
| 41 | +2 --> 10 |
| 42 | +3 --> 11 |
| 43 | +4 --> 100 |
| 44 | +5 --> 101 |
| 45 | +``` |
| 46 | + |
| 47 | +### Constraints |
| 48 | + |
| 49 | +- `0 <= n <= 10^5` |
| 50 | + |
| 51 | +### Follow-up |
| 52 | + |
| 53 | +- Can you solve it in linear time `O(n)` and possibly in a single pass? |
| 54 | +- Can you solve it without using any built-in function (i.e., like `__builtin_popcount` in C++)? |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Solution for Counting Bits Problem |
| 59 | + |
| 60 | +### Approach 1: Brute Force (Naive) |
| 61 | + |
| 62 | +The brute force approach involves iterating through each number from `0` to `n`, converting each number to its binary form, and counting the number of `1`s in that binary representation. |
| 63 | + |
| 64 | +#### Code in Different Languages |
| 65 | + |
| 66 | +<Tabs> |
| 67 | +<TabItem value="C++" label="C++" default> |
| 68 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 69 | + |
| 70 | +```cpp |
| 71 | +class Solution { |
| 72 | +public: |
| 73 | + vector<int> countBits(int n) { |
| 74 | + vector<int> ans(n + 1); |
| 75 | + for (int i = 0; i <= n; ++i) { |
| 76 | + ans[i] = __builtin_popcount(i); |
| 77 | + } |
| 78 | + return ans; |
| 79 | + } |
| 80 | +}; |
| 81 | +``` |
| 82 | +
|
| 83 | +</TabItem> |
| 84 | +<TabItem value="Java" label="Java"> |
| 85 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 86 | +
|
| 87 | +```java |
| 88 | +class Solution { |
| 89 | + public int[] countBits(int n) { |
| 90 | + int[] ans = new int[n + 1]; |
| 91 | + for (int i = 0; i <= n; ++i) { |
| 92 | + ans[i] = Integer.bitCount(i); |
| 93 | + } |
| 94 | + return ans; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +</TabItem> |
| 100 | +<TabItem value="Python" label="Python"> |
| 101 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 102 | + |
| 103 | +```python |
| 104 | +class Solution: |
| 105 | + def countBits(self, n: int) -> List[int]: |
| 106 | + return [bin(i).count('1') for i in range(n + 1)] |
| 107 | +``` |
| 108 | + |
| 109 | +</TabItem> |
| 110 | +</Tabs> |
| 111 | + |
| 112 | +#### Complexity Analysis |
| 113 | + |
| 114 | +- **Time Complexity**: $O(nlogn)$, as converting a number to binary and counting bits takes $O(\log n)$ time, and we do this for each number from `0` to `n`. |
| 115 | +- **Space Complexity**: $O(n)$, for storing the result array. |
| 116 | + |
| 117 | +### Approach 2: Optimized Dynamic Programming |
| 118 | + |
| 119 | +To achieve a linear time solution, we can use a dynamic programming approach. We use the property that the number of `1`s in `i` can be derived from the number of `1`s in `i >> 1` (i.e., `i` divided by `2`) plus `1` if the last bit of `i` is `1`. |
| 120 | + |
| 121 | +#### Code in Different Languages |
| 122 | + |
| 123 | +<Tabs> |
| 124 | +<TabItem value="C++" label="C++" default> |
| 125 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 126 | + |
| 127 | +```cpp |
| 128 | +class Solution { |
| 129 | +public: |
| 130 | + vector<int> countBits(int n) { |
| 131 | + vector<int> ans(n + 1); |
| 132 | + for (int i = 1; i <= n; ++i) { |
| 133 | + ans[i] = ans[i >> 1] + (i & 1); |
| 134 | + } |
| 135 | + return ans; |
| 136 | + } |
| 137 | +}; |
| 138 | +``` |
| 139 | +
|
| 140 | +</TabItem> |
| 141 | +<TabItem value="Java" label="Java"> |
| 142 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 143 | +
|
| 144 | +```java |
| 145 | +class Solution { |
| 146 | + public int[] countBits(int n) { |
| 147 | + int[] ans = new int[n + 1]; |
| 148 | + for (int i = 1; i <= n; ++i) { |
| 149 | + ans[i] = ans[i >> 1] + (i & 1); |
| 150 | + } |
| 151 | + return ans; |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +</TabItem> |
| 157 | +<TabItem value="Python" label="Python"> |
| 158 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 159 | + |
| 160 | +```python |
| 161 | +class Solution: |
| 162 | + def countBits(self, n: int) -> List[int]: |
| 163 | + ans = [0] * (n + 1) |
| 164 | + for i in range(1, n + 1): |
| 165 | + ans[i] = ans[i >> 1] + (i & 1) |
| 166 | + return ans |
| 167 | +``` |
| 168 | + |
| 169 | +</TabItem> |
| 170 | +</Tabs> |
| 171 | + |
| 172 | +#### Complexity Analysis |
| 173 | + |
| 174 | +- **Time Complexity**: $O(n)$, as we calculate the number of bits for each number from `0` to `n` in constant time. |
| 175 | +- **Space Complexity**: $O(n)$, for storing the result array. |
| 176 | + |
| 177 | +--- |
| 178 | + |
| 179 | +<h2>Authors:</h2> |
| 180 | + |
| 181 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 182 | +{['ImmidiSivani'].map(username => ( |
| 183 | + <Author key={username} username={username} /> |
| 184 | +))} |
| 185 | +</div> |
0 commit comments