Total Accepted:18290Total Submissions:32984Difficulty:Medium
Given a non negative integer numbernum. For every numbersiin the range0 ≤ i ≤ numcalculate the number of 1's
in their binary representation and return them as an array.
num = 5you should return
- It is very easy to come up with a solution with run timeO(n*sizeof(integer)). But can you do it in linear timeO(n)/possibly in a single pass
- Space complexity should beO(n).
- Can you do it like a boss Do it without using any builtin function like__builtin_popcountin c++ or in any other language.
- You should make use of what you have produced already.
- Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
- Or does the odd/even status of the number help you in calculating the number of 1s
Special thanks to@ syedeefor adding this problem and creating all test cases.
Subscribeto see which companies asked this question