top of page

Looking to master object-oriented and system design for tech interviews or career growth?

  • Improve your system design and machine coding skills.

  • Study with our helpful resources.

  • Prepare for technical interviews and advance your career.

**We're in beta mode and would love to hear your feedback.

Writer's pictureilluminati

Number of 1 Bits - InterviewBit Solution


Problem Description:

Given a number, count the number of set bits.


Example:

The 32-bit integer 11 has binary representation

00000000000000000000000000001011 

so the function should return 3


Solution Approach:

To solve this problem we can keep dividing it by 2, whenever the remainder is 1, that means there is a set bit, so increment the count.




Space & Time Complexity:

Time Complexity: O(logN)

- Since each time we are diving the number by 2.


Space Complexity: O(1)


Solution:

Code in C++


Comments


bottom of page