Hi👋SpeedAlgo • Deliberate Practice & Cognitive Ergonomics for Software Engineers

An interactive algorithm mastery and technical interview preparation platform published by Hi👋WebEnterprise. Built for senior and staff software engineers preparing for rigorous coding screens at top tech companies (FAANG/MAMAA).

13 Core Algorithmic Patterns & 225 Practice Problems

  • 1. Two Pointers (16 Paradigms, 43 Problems): Opposite ends, sorted pair sums, palindrome verification, trapping rain water, 3Sum, container with most water — includes the Sliding Window and Fast & Slow Pointers paradigms (contiguous subarray invariants, longest substrings, Floyd's cycle detection, linked list middle).
  • 2. Binary Search (9 Paradigms, 14 Problems): Monotonic predicate partitioning (feasible(x)), boundary search, rotated sorted arrays, square roots, minimum ship capacity, median of two sorted arrays, matrix median on value range.
  • 3. Bit Manipulation (5 Paradigms, 11 Problems): Counting set bits, XOR cancellation, bit reversal, bitmask pairing, maximum XOR with a bitwise trie.
  • 4. Math & Geometry (8 Paradigms, 14 Problems): Sieve of Eratosthenes, integer/roman conversions, modular arithmetic, geometric simulation.
  • 5. Tree/Graph Depth-First Search (15 Paradigms, 24 Problems): Path sums, lowest common ancestor, tree diameter, validating BSTs, tree DP (House Robber III, Binary Tree Cameras).
  • 6. Tree/Graph Breadth-First Search (5 Paradigms, 12 Problems): Level-order traversals, shortest path in unweighted graphs, zig-zag traversals, rotting oranges, word ladders.
  • 7. Graphs (12 Paradigms, 22 Problems): Topological sort, cycle detection, Dijkstra's algorithm, bipartite graph validation, network delay.
  • 8. Backtracking (6 Paradigms, 14 Problems): Subsets, permutations, combinations, constraint satisfaction, pruning, N-Queens, Sudoku solver.
  • 9. Dynamic Programming (13 Paradigms, 28 Problems): Memoization vs tabulation, 0/1 knapsack, unbounded knapsack, coin change, edit distance, longest common subsequence.
  • 10. Heap / Priority Queue (9 Paradigms, 11 Problems): Running medians, top-k elements, interval scheduling, IPO, k-way merges.
  • 11. Advanced Data Structures (7 Paradigms, 17 Problems): Trie (prefix tree), Union-Find (Disjoint Set Union), LRU Cache, LFU Cache, range-sum queries with updates.
  • 12. Stack (5 Paradigms, 9 Problems): Next greater element, largest rectangle in histogram, remove k digits, decode string, min stack.
  • 13. Intervals & Greedy (2 Paradigms, 6 Problems): Merge intervals, interval scheduling, meeting rooms by sweep line, jump game.

4-Stage Deliberate Practice Framework

  1. Stage 1 (Compare & Learn): Read the pattern's mental model, loop invariant and Big-O proof, compare the pattern's template with the problem's solution side by side, and use the step-by-step debugger. The solution can also be shown in C#, Java, TypeScript, C++, Go or Rust for almost every problem.
  2. Stage 2 (Active Recall): Rebuild the solution from memory in a code editor, with an invariant checklist and optional progressive hints, and check it against a judged test suite.
  3. Stage 3 (AI Interview Coach, coming soon): A mock-interview stage still in development; for now, students go straight from Stage 2 to Stage 4.
  4. Stage 4 (Solve on Your Own): Solve a new problem on your own against a 15-minute timer; your code is graded against a test suite. Python and TypeScript run in your browser and C# runs on our server (design problems grade in Python and TypeScript).

Equipped with a spaced-repetition review hub on a fixed 7-step schedule (1 day up to 90 days), a two-pane Studio workspace, and study notes.

Pricing, Access & Commercial Terms

  • Core Curriculum: 100% Free. No credit card required.
  • Coins: New accounts get 40 free coins once, the daily claim adds +20 coins once a day, and inviting a friend earns you +25 coins when they complete their first practice step. These are the only ways to earn coins.
  • AI Coach: Each AI coach question costs 1-5 coins, priced by the server according to its size.
  • BYOK (Bring Your Own Key): AI coaching is free with your own Google Gemini, OpenAI or OpenRouter API key (BYOK).
  • Extras: Coins also unlock extras for 24 hours: a Recall hint level (1 coin), a Solve solution reveal (2), a LeetCode problem card's solutions (1) and a pattern's extra canonical problems (1).
  • Payments & Refunds: Nothing on the site is sold for money: coins can only be earned, never bought. There are no subscriptions or charges, so there is nothing to refund.
  • Platform Operator: Hi👋WebEnterprise. Support & policies at hispeedalgo.com.
Skip to main content
Hi👋SpeedAlgo

Invariant-First Algorithmic Mastery

Arena
238Items
Theory Context•Bit Manipulation
EasyLC 338

Counting Bits (LeetCode 338)

Target Frequency:MetaAppleAmazon

Given an integer n, build the array ans of length n + 1 where each ans[i] (for 0 <= i <= n) holds the number of 1-bits in the binary form of i.

Worked Examples

Example 1
Input:n = 2
Output:[0,1,1]
Explanation: 0 --> 0 1 --> 1 2 --> 10
Example 2
Input:n = 5
Output:[0,1,1,2,1,2]
Explanation: 0 --> 0 1 --> 1 2 --> 10 3 --> 11 4 --> 100 5 --> 101

⚖️Formal Constraints & Bounds

  • 0 <= n <= 105

Deep-Dive & Conceptual Insights

Core Insight

Right shifting i >> 1 removes the least significant bit. Therefore, the number of set bits in i is ans[i >> 1] + (i & 1). Tabulating from 1 to n achieves strict O(N) linear time.

Real-World Scenario & Production Applications

Hamming distance calculation tables, high-speed population count index precomputation in search engines, cryptographic weight generation.

Step-by-Step Execution Trace Table

Number iBinary RepresentationRight Shift i >> 1Lowest Bit i & 1Recurrence: ans[i >> 1] + (i & 1)Result ans[i]
00_200Base case0
11_201ans[0] + 1 = 0 + 11
210_210ans[1] + 0 = 1 + 01
311_211ans[1] + 1 = 1 + 12
4100_220ans[2] + 0 = 1 + 01
5101_221ans[2] + 1 = 1 + 12
Worked on Paper

The Math, Step by Step

1. The number stored for each iii

ans[i] must be the number of 1 bits in the binary form of iii, its popcount. The code fills ans from i=0i = 0i=0 up to nnn, and it computes each entry from an entry it has already filled.

2. Splitting iii into its last bit and the rest

Every whole number iii splits into its bits without the last one, and its last bit:

i=2⋅⌊i2⌋+(i mod 2)i = 2 \cdot \left\lfloor \frac{i}{2} \right\rfloor + (i \bmod 2)i=2⋅⌊2i​⌋+(imod2)

In code, the rest is i >> 1, which shifts the bits right by one place and drops the last bit. The last bit is i & 1, which keeps only bit 0. For i=5i = 5i=5, which is 101 in binary, i >> 1 is 10, which is 2, and i & 1 is 1.

Last bit of iiii & 11 bits of iii compared with i >> 1
0 (iii is even)0the same
1 (iii is odd)1one more
3. Why the recurrence is right, and why the order works

The 1 bits of iii are the 1 bits of i >> 1 plus its last bit. So

ans[i]=ans[i≫1]+(i&1)\text{ans}[i] = \text{ans}[i \gg 1] + (i \mathbin{\&} 1)ans[i]=ans[i≫1]+(i&1)

This is a dynamic programming recurrence: each answer is built from the answer to a smaller input. For i≥1i \ge 1i≥1, i >> 1 is smaller than iii. The loop goes from i=1i = 1i=1 upward, so ans[i >> 1] is always filled before it is read.

4. The start value

ans[0] is 0, because 0 has no 1 bits. The code sets it with ans = [0] * (n + 1), and the loop starts at i=1i = 1i=1. For i=1i = 1i=1, the rule reads ans[0]: ans[1]=ans[0]+1=1\text{ans}[1] = \text{ans}[0] + 1 = 1ans[1]=ans[0]+1=1. For n=0n = 0n=0, the loop does not run, and the code returns [0].

5. Example 2 by hand

n = 5.

iiiiii in binaryi >> 1i & 1ans[i] = ans[i >> 1] + (i & 1)
000 (start value)
11010+1=10 + 1 = 10+1=1
210101+0=11 + 0 = 11+0=1
311111+1=21 + 1 = 21+1=2
4100201+0=11 + 0 = 11+0=1
5101211+1=21 + 1 = 21+1=2

The result [0,1,1,2,1,2] is the expected output. For example, 4 is 100 and 2 is 10: dropping the last 0 of 4 gives 2, and both have one 1 bit.

6. Example 1 by hand

n = 2. The table above gives ans[1]=1\text{ans}[1] = 1ans[1]=1 and ans[2]=1\text{ans}[2] = 1ans[2]=1. The code returns [0,1,1], the expected output.

7. From the math to the code
MathIn countBits
ans[0]=0\text{ans}[0] = 0ans[0]=0ans = [0] * (n + 1)
fill i=1…ni = 1 \ldots ni=1…n in increasing orderfor i in range(1, n + 1):
the rest of iii, ⌊i/2⌋\lfloor i / 2 \rfloor⌊i/2⌋i >> 1
the last bit, i mod 2i \bmod 2imod2i & 1
ans[i]=ans[i≫1]+(i&1)\text{ans}[i] = \text{ans}[i \gg 1] + (i \mathbin{\&} 1)ans[i]=ans[i≫1]+(i&1)ans[i] = ans[i >> 1] + (i & 1)

Each entry takes one shift, one & and one addition. So the code takes O(n)O(n)O(n) time, and the output list takes O(n)O(n)O(n) space.

Code Shape
Archetype Code Shape
Python
1ans = [0] * (n + 1)
2for i in range(1, n + 1):
3 ans[i] = ans[i >> 1] + (i & 1)
4return ans

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

dp[i >> 1] reaching backward into an array you're still building looks strange until you notice that every integer's binary form is literally built out of a smaller integer's binary form, plus one extra digit.

📋 The Analogy: Reusing the Answer for a Shorter Number

i >> 1 is i with its last binary digit chopped off — in other words, it's exactly the room number you get by dropping the ones-digit. Chopping off a trailing digit never changes how many 1s are in the rest of the number, so whatever answer you already computed for that shorter number is still valid; you just need to add back the one digit you chopped off, which is either 0 or 1.

You never recompute a bit count from scratch. Every number reuses the answer already stored for the number half its size.

🔑 The Key Idea in the Code
  • Trick 1 (dp[i >> 1]): The set-bit count of everything except the last binary digit — already sitting in the array because i >> 1 < i, so it was computed on an earlier loop iteration.
  • Trick 2 (i & 1): The one digit that the shift removed, added back with one AND.
  • Calling popcount on every number 0…N0 \dots N0…N in O(Nlog⁡N)O(N \log N)O(NlogN): Computing popcount independently for each number takes O(N⋅32)O(N \cdot 32)O(N⋅32). Using dynamic programming dp[i] = dp[i >> 1] + (i & 1) computes all bit counts in strict O(N)O(N)O(N) time.

  • Off-by-one allocating array of size NNN instead of N+1N + 1N+1: The result array must hold bit counts from 0 through NNN inclusive, requiring length N+1N + 1N+1.

  • Operator precedence omitting parentheses: In dp[i >> 1] + (i & 1), omitting parentheses around i & 1 results in (dp[i >> 1] + i) & 1 because + has higher precedence than &.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Counting set bits for all integers in range [0, n], linear O(N) time requirement without O(N log N) per-number bit loops.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: Any integer i has the exact same number of set bits as i >> 1 (integer division by 2), plus 1 if i is odd (i & 1).

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Running Kernighan's trick or bin(i).count('1') in a loop: Takes O(N · 32) = O(N log N) time, missing the linear dynamic programming recurrence.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I allocate a dp array of size n + 1 initialized to 0. For i from 1 to n, I compute dp[i] = dp[i >> 1] + (i & 1). Since i >> 1 < i, the subproblem dp[i >> 1] is already memoized. This computes bit counts for all numbers in a single pass in O(N) time and O(1) auxiliary space (excluding output).

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the execution steps: dp[i] = dp[i >> 1] + (i & 1). Number of set bits in i equals set bits in i // 2 plus last bit. Single pass filling array from 0 to N.

SPACE COMPLEXITY

O(1) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Output array excluded from auxiliary space per problem spec. Uses O(1) auxiliary registers.

Formal Recurrence Relation

T(N) = N · O(1) = O(N)

Derivation Progression

Right-Shift Induction

dp[i] = dp[i >> 1] + (i \& 1)

Shifting right removes LSB; remaining bits already computed at index i >> 1.

Constant-Time Fill

O(1) bitwise shift and AND per index

Zero loops or bit iterations.

Linear Array Build

N+1 elements = O(N)

Computes all bit counts in strict linear time.

Variable Definitions

NNN

Upper integer limit (computing bit counts for integers 0 to N)

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(1) Zero heap memory (output array excluded)

🟢 Output Space

O(N) Result array of size N + 1

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N)

Average Case

O(N)O(N)O(N)

Worst Case

O(N)O(N)O(N)

Recurrence Tree Topology

Recurrence Tree Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

The problem asks for an array ans of n + 1 entries, where ans[i] counts the 1-bits in i written in binary. Evaluating popcount(i) for every number independently would cost O(Nlog⁡N)O(N \log N)O(NlogN) or O(32N)O(32N)O(32N). The phrase "for every number from 0 to n" is a classic structural trigger for Dynamic Programming over Bit Transitions. Specifically, right-shifting an integer i >> 1 removes its least significant bit, whose popcount was already computed!

CONSTRAINTS & BOUNDS

N≤105N \le 10^5N≤105. An O(N)O(N)O(N) linear pass where each answer is computed in O(1)O(1)O(1) time is required. The output array has length N+1N + 1N+1, initialized with dp[0] = 0.

Theory Context•Bit Manipulation
EasyLC 338

Counting Bits (LeetCode 338)

Target Frequency:MetaAppleAmazon

Given an integer n, build the array ans of length n + 1 where each ans[i] (for 0 <= i <= n) holds the number of 1-bits in the binary form of i.

Worked Examples

Example 1
Input:n = 2
Output:[0,1,1]
Explanation: 0 --> 0 1 --> 1 2 --> 10
Example 2
Input:n = 5
Output:[0,1,1,2,1,2]
Explanation: 0 --> 0 1 --> 1 2 --> 10 3 --> 11 4 --> 100 5 --> 101

⚖️Formal Constraints & Bounds

  • 0 <= n <= 105

Deep-Dive & Conceptual Insights

Core Insight

Right shifting i >> 1 removes the least significant bit. Therefore, the number of set bits in i is ans[i >> 1] + (i & 1). Tabulating from 1 to n achieves strict O(N) linear time.

Real-World Scenario & Production Applications

Hamming distance calculation tables, high-speed population count index precomputation in search engines, cryptographic weight generation.

Step-by-Step Execution Trace Table

Number iBinary RepresentationRight Shift i >> 1Lowest Bit i & 1Recurrence: ans[i >> 1] + (i & 1)Result ans[i]
00_200Base case0
11_201ans[0] + 1 = 0 + 11
210_210ans[1] + 0 = 1 + 01
311_211ans[1] + 1 = 1 + 12
4100_220ans[2] + 0 = 1 + 01
5101_221ans[2] + 1 = 1 + 12
Worked on Paper

The Math, Step by Step

1. The number stored for each iii

ans[i] must be the number of 1 bits in the binary form of iii, its popcount. The code fills ans from i=0i = 0i=0 up to nnn, and it computes each entry from an entry it has already filled.

2. Splitting iii into its last bit and the rest

Every whole number iii splits into its bits without the last one, and its last bit:

i=2⋅⌊i2⌋+(i mod 2)i = 2 \cdot \left\lfloor \frac{i}{2} \right\rfloor + (i \bmod 2)i=2⋅⌊2i​⌋+(imod2)

In code, the rest is i >> 1, which shifts the bits right by one place and drops the last bit. The last bit is i & 1, which keeps only bit 0. For i=5i = 5i=5, which is 101 in binary, i >> 1 is 10, which is 2, and i & 1 is 1.

Last bit of iiii & 11 bits of iii compared with i >> 1
0 (iii is even)0the same
1 (iii is odd)1one more
3. Why the recurrence is right, and why the order works

The 1 bits of iii are the 1 bits of i >> 1 plus its last bit. So

ans[i]=ans[i≫1]+(i&1)\text{ans}[i] = \text{ans}[i \gg 1] + (i \mathbin{\&} 1)ans[i]=ans[i≫1]+(i&1)

This is a dynamic programming recurrence: each answer is built from the answer to a smaller input. For i≥1i \ge 1i≥1, i >> 1 is smaller than iii. The loop goes from i=1i = 1i=1 upward, so ans[i >> 1] is always filled before it is read.

4. The start value

ans[0] is 0, because 0 has no 1 bits. The code sets it with ans = [0] * (n + 1), and the loop starts at i=1i = 1i=1. For i=1i = 1i=1, the rule reads ans[0]: ans[1]=ans[0]+1=1\text{ans}[1] = \text{ans}[0] + 1 = 1ans[1]=ans[0]+1=1. For n=0n = 0n=0, the loop does not run, and the code returns [0].

5. Example 2 by hand

n = 5.

iiiiii in binaryi >> 1i & 1ans[i] = ans[i >> 1] + (i & 1)
000 (start value)
11010+1=10 + 1 = 10+1=1
210101+0=11 + 0 = 11+0=1
311111+1=21 + 1 = 21+1=2
4100201+0=11 + 0 = 11+0=1
5101211+1=21 + 1 = 21+1=2

The result [0,1,1,2,1,2] is the expected output. For example, 4 is 100 and 2 is 10: dropping the last 0 of 4 gives 2, and both have one 1 bit.

6. Example 1 by hand

n = 2. The table above gives ans[1]=1\text{ans}[1] = 1ans[1]=1 and ans[2]=1\text{ans}[2] = 1ans[2]=1. The code returns [0,1,1], the expected output.

7. From the math to the code
MathIn countBits
ans[0]=0\text{ans}[0] = 0ans[0]=0ans = [0] * (n + 1)
fill i=1…ni = 1 \ldots ni=1…n in increasing orderfor i in range(1, n + 1):
the rest of iii, ⌊i/2⌋\lfloor i / 2 \rfloor⌊i/2⌋i >> 1
the last bit, i mod 2i \bmod 2imod2i & 1
ans[i]=ans[i≫1]+(i&1)\text{ans}[i] = \text{ans}[i \gg 1] + (i \mathbin{\&} 1)ans[i]=ans[i≫1]+(i&1)ans[i] = ans[i >> 1] + (i & 1)

Each entry takes one shift, one & and one addition. So the code takes O(n)O(n)O(n) time, and the output list takes O(n)O(n)O(n) space.

Code Shape
Archetype Code Shape
Python
1ans = [0] * (n + 1)
2for i in range(1, n + 1):
3 ans[i] = ans[i >> 1] + (i & 1)
4return ans

Conceptual Narrative

🧭 Conceptual Foundation & Pattern Intuition

dp[i >> 1] reaching backward into an array you're still building looks strange until you notice that every integer's binary form is literally built out of a smaller integer's binary form, plus one extra digit.

📋 The Analogy: Reusing the Answer for a Shorter Number

i >> 1 is i with its last binary digit chopped off — in other words, it's exactly the room number you get by dropping the ones-digit. Chopping off a trailing digit never changes how many 1s are in the rest of the number, so whatever answer you already computed for that shorter number is still valid; you just need to add back the one digit you chopped off, which is either 0 or 1.

You never recompute a bit count from scratch. Every number reuses the answer already stored for the number half its size.

🔑 The Key Idea in the Code
  • Trick 1 (dp[i >> 1]): The set-bit count of everything except the last binary digit — already sitting in the array because i >> 1 < i, so it was computed on an earlier loop iteration.
  • Trick 2 (i & 1): The one digit that the shift removed, added back with one AND.
  • Calling popcount on every number 0…N0 \dots N0…N in O(Nlog⁡N)O(N \log N)O(NlogN): Computing popcount independently for each number takes O(N⋅32)O(N \cdot 32)O(N⋅32). Using dynamic programming dp[i] = dp[i >> 1] + (i & 1) computes all bit counts in strict O(N)O(N)O(N) time.

  • Off-by-one allocating array of size NNN instead of N+1N + 1N+1: The result array must hold bit counts from 0 through NNN inclusive, requiring length N+1N + 1N+1.

  • Operator precedence omitting parentheses: In dp[i >> 1] + (i & 1), omitting parentheses around i & 1 results in (dp[i >> 1] + i) & 1 because + has higher precedence than &.

Senior SWE Reasoning Architecture

4-Phase Thought Process Model

Pattern Recognition Signals

The 10-second spot

Counting set bits for all integers in range [0, n], linear O(N) time requirement without O(N log N) per-number bit loops.

Formulating the Predicate & Invariants

Turning intuition into a boolean rule

Invariant: Any integer i has the exact same number of set bits as i >> 1 (integer division by 2), plus 1 if i is odd (i & 1).

Silent Failure Traps & Edge Cases

Where confident candidates still lose points

  • Running Kernighan's trick or bin(i).count('1') in a loop: Takes O(N · 32) = O(N log N) time, missing the linear dynamic programming recurrence.

The 60-Second Interview Pitch

Say this out loud before you type a single line

I allocate a dp array of size n + 1 initialized to 0. For i from 1 to n, I compute dp[i] = dp[i >> 1] + (i & 1). Since i >> 1 < i, the subproblem dp[i >> 1] is already memoized. This computes bit counts for all numbers in a single pass in O(N) time and O(1) auxiliary space (excluding output).

Big-O Invariant Derivation

Complexity & Mathematical Proof

TIME COMPLEXITY

O(N)

Look at the execution steps: dp[i] = dp[i >> 1] + (i & 1). Number of set bits in i equals set bits in i // 2 plus last bit. Single pass filling array from 0 to N.

SPACE COMPLEXITY

O(1) Auxiliary

Look at memory allocations: Call stack uses O(1) Iterative. Output array excluded from auxiliary space per problem spec. Uses O(1) auxiliary registers.

Formal Recurrence Relation

T(N) = N · O(1) = O(N)

Derivation Progression

Right-Shift Induction

dp[i] = dp[i >> 1] + (i \& 1)

Shifting right removes LSB; remaining bits already computed at index i >> 1.

Constant-Time Fill

O(1) bitwise shift and AND per index

Zero loops or bit iterations.

Linear Array Build

N+1 elements = O(N)

Computes all bit counts in strict linear time.

Variable Definitions

NNN

Upper integer limit (computing bit counts for integers 0 to N)

Memory Architecture & Bounds

🟣 Call Stack

O(1) Iterative

🔵 Auxiliary Heap

O(1) Zero heap memory (output array excluded)

🟢 Output Space

O(N) Result array of size N + 1

Boundary Best / Worst Cases

Best Case

O(N)O(N)O(N)

Average Case

O(N)O(N)O(N)

Worst Case

O(N)O(N)O(N)

Recurrence Tree Topology

Recurrence Tree Topology
Synthesizing vector architecture diagram...
Staff+ Engineering Perspective

Senior SWE Deconstruction & Hardware Caveats

ARCHITECTURAL SIGNALS & INTERVIEW TRIGGERS

The problem asks for an array ans of n + 1 entries, where ans[i] counts the 1-bits in i written in binary. Evaluating popcount(i) for every number independently would cost O(Nlog⁡N)O(N \log N)O(NlogN) or O(32N)O(32N)O(32N). The phrase "for every number from 0 to n" is a classic structural trigger for Dynamic Programming over Bit Transitions. Specifically, right-shifting an integer i >> 1 removes its least significant bit, whose popcount was already computed!

CONSTRAINTS & BOUNDS

N≤105N \le 10^5N≤105. An O(N)O(N)O(N) linear pass where each answer is computed in O(1)O(1)O(1) time is required. The output array has length N+1N + 1N+1, initialized with dp[0] = 0.

© 2026 Hi👋WebEnterprise. All rights reserved.
Sitemap•llms.txt•