---
title: "Discussion on \"LeetCode 338: Counting Bits\" | Hashnode"
description: "Discussion on \"LeetCode 338: Counting Bits\". Problem Overview\nGiven an integer n, return an array ans of length n + 1 where ans[i] is the number of set bits in the binary representation of i.\n\nExample: n = 5 → Output: [0, 1, 1, 2, 1, 2]\n\nConstraints: 0 <= n <= 10^5\n\n\nSolution 1: Brute Force\nThi..."
url: "https://hashnode.com/posts/leetcode-338-counting-bits/685cb3b564a9eda5ecd8603f"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [LeetCode 338: Counting Bits](https://blog.brucelinweb.com/leetcode-338-counting-bits?utm_source=hashnode&utm_medium=feed)

Problem Overview Given an integer n, return an array ans of length n + 1 where ans\[i\] is the number of set bits in the binary representation of i. Example: n = 5 → Output: \[0, 1, 1, 2, 1, 2\] Constraints: 0 <= n <= 10^5 Solution 1: Brute Force Thi...

Jun 26, 2025 · 2 min read · blog.brucelinweb.com  
Read the article

-   [#leetcode](https://hashnode.com/tag/leetcode)
-   [#coding](https://hashnode.com/tag/coding)
-   [#cpp](https://hashnode.com/tag/cpp)

## Written by

[Bruce Lin](https://hashnode.com/@lalalala0620)

@lalalala0620

  
[View profile](https://hashnode.com/@lalalala0620)

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
