Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Build a DP table where ans[i] reuses the already-computed count for i >> 1 (integer division by 2) and adds 1 if i is odd (i & 1). This avoids recomputing bit counts from scratch for each number.
Right-shifting by 1 removes the least significant bit, and that bit count is already stored in the table. Adding back whether the removed bit was 1 completes the count.
Updated Feb 2026