ByteByteGo logo
menuProblems List

Hamming Weights of Integers

Easy

The Hamming weight of a number is the number of set bits (1-bits) in its binary representation. Given a positive integer n, return an array where the ith element is the Hamming weight of integer i for all integers from 0 to n.

Example:

Input: n = 7
Output: [0, 1, 1, 2, 1, 2, 2, 3]

Explanation:

NumberBinary representationNumber of set bits
000
111
2101
3112
41001
51012
61102
71113

You can practice coding exercises online by logging into bytebytego.com on your laptop.