countingBits.md
August 14, 2024 ยท View on GitHub
Description:
Given a positive integer num, return a list ans of length n + 1 where each element ans[i] represents the count of 1 bits in the binary representation of i(0<=i<=n).
Example:
Example 1: Input: num = 3 Output: [0, 1, 1, 2]
Example 2: Input: num = 6 Output: [0, 1, 1, 2, 1, 2, 2]
Algorithmic Steps: This problem is solved with the help of dynamic programming technique where previous results going to be reused to calculate the current results. The algorithmic approach can be summarized as follows:
-
Get the number
numas input parameter. -
Initialize the
ansarray with a length ofnum+1where each element assigned to zero by default. This array indicates the number of1s in each element from0tonum. -
Initialize the offset(
offset) variable to 1. This is because the first offset with power of 2 is 1(i.e, 2 power 0). In this problem, you can find pattern that offset is multiplied by 2 when the index value is power of 2. -
Iterate over each element upto
numto find the counting bits. -
The offset should be updated to the index variable(
i) once the offset multiplied by 2 is equal to index variable. -
If the condition in step5 fails, the number of 1's for current number is calculated by adding the number of 1's in previous power of 2 index variable with 1.
-
Repeat 5-6 steps to calculate the number of set bits for each element.
-
Return the
ansarray which holds number of set bits upto input number.
Time and Space complexity:
This algorithm takes a time complexity of O(n). This is because we are iterating elements upto n.
Here, we need to use array to hold the number of set bits for each element. Hence, the space complexity will be O(n).