K33

Counting Bits (Number of 1 Bits)

Junior Level Candidate
Target Time: 15 - 20 Minutes
Type: ARRAY

Problem Statement & Objective

Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.

Function Signature (Python)

counting_bits_number_of_1(n: int) -> list[int]

Input & Output Contract

Output Rule:0'dan n'e kadar her sayının 1 bit sayısı listesi.

System Constraints

0 <= n <= 10^5

Example Execution Walkthrough

n = 2 -> 0: '0' (0), 1: '1' (1), 2: '10' (1) -> cevap: [0, 1, 1]
n = 5 -> 0(0), 1(1), 2(1), 3(2), 4(1), 5(2) -> cevap: [0, 1, 1, 2, 1, 2]

Example Test Scenarios (3)

Example 1: n=2
Input:[2]
Expected Output:[0,1,1]
Example 2: n=5
Input:[5]
Expected Output:[0,1,1,2,1,2]
Example 3: n=0
Input:[0]
Expected Output:[0]
Loading Python Editor...
Input Parameters
[
  2
]
Expected Output
[
  0,
  1,
  1
]