Skip to main content

Number of 1 Bits - Video Solutions

EasyDivide and ConquerBit Manipulation

Number of 1 Bits - Leetcode 191 - Python

NeetCode
11:59197,107 views
10 video solutions available

Number of 1 Bits - Video Solution

Watch 10 video solutions for Number of 1 Bits, a easy level problem involving Divide and Conquer, Bit Manipulation. This walkthrough by NeetCode has 197,107 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

Given a positive integer n, write a function that returns the number of set bits in its binary representation (also known as the Hamming weight).

 

Example 1:

Input: n = 11

Output: 3

Explanation:

The input binary string 1011 has a total of three set bits.

Example 2:

Input: n = 128

Output: 1

Explanation:

The input binary string 10000000 has a total of one set bit.

Example 3:

Input: n = 2147483645

Output: 30

Explanation:

The input binary string 1111111111111111111111111111101 has a total of thirty set bits.

 

Constraints:

  • 1 <= n <= 231 - 1

 

Follow up: If this function is called many times, how would you optimize it?
Read full problem with examples

Approach Overview

Problem Overview: Given a 32-bit unsigned integer n, count how many bits are set to 1 in its binary representation. This count is often called the Hamming weight. The task focuses on efficient bit-level operations rather than converting the number to a string or using high-level utilities.

Approach 1: Iterative Bit Check (Time: O(32) ~ O(1), Space: O(1))

This method inspects each bit of the integer one by one. Use a loop while n > 0. Check the least significant bit with n & 1. If the result is 1, increment the counter. Then right-shift the number using n >> 1 to move the next bit into position. The algorithm performs at most 32 iterations for a 32-bit integer, so the runtime is effectively constant. This approach is straightforward and easy to implement during interviews when demonstrating basic bit manipulation skills.

Approach 2: Clear the Least Significant Set Bit using n & (n - 1) (Time: O(k), Space: O(1))

The expression n & (n - 1) removes the lowest set bit from a number. Each operation reduces the number of 1 bits by exactly one. Repeatedly apply this operation until n becomes 0, counting how many times it runs. If the integer contains k set bits, the loop runs exactly k times. This makes the algorithm faster when the number contains only a few set bits. The trick relies on binary arithmetic properties and is a classic technique in bit manipulation and divide and conquer-style bit reduction problems.

Recommended for interviews: Interviewers usually expect the n & (n - 1) solution. It demonstrates deeper understanding of binary representations and efficient bit tricks. The iterative bit check still shows solid fundamentals and is often a good first explanation. Moving from the basic bit scan to the optimized clearing trick signals strong problem-solving ability with low-level operations.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Bit CheckO(32) ≈ O(1)O(1)Simple implementation when scanning each bit is acceptable
n & (n - 1) Bit Clearing TrickO(k) where k = number of set bitsO(1)Preferred in interviews and optimized cases with few set bits