Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Number of Digit One
Bookmark
Input
n = 13
n = 1024 (all 3 cases)
n = 100
n = 1
Custom
n
=
13
counting 1s in
1…
13
1
3
count the 1s at each place value, then add them up
count =
0
counting 1s in
1…
13
1
3
count the 1s at each place value, then add them up
count =
0
counting 1s in
1…
13
1
3
start with
count = 0
count =
0
counting 1s in
1…
13
place =
units
(×1)
1
3
higher
1
· current
3
· lower
0
ones contributed by each place value
count =
0
1
|
3
|
0
← how many 1s land in this place?
counting 1s in
1…
13
place =
units
(×1)
1
3
higher
1
· current
3
· lower
0
ones contributed by each place value
2
units
count =
2
current ≥ 2 →
(1 + 1) × 1
=
2
counting 1s in
1…
13
place =
tens
(×10)
1
3
higher
0
· current
1
· lower
3
ones contributed by each place value
2
units
count =
2
0
|
1
|
3
← how many 1s land in this place?
counting 1s in
1…
13
place =
tens
(×10)
1
3
higher
0
· current
1
· lower
3
ones contributed by each place value
2
units
+
4
tens
count =
6
current 1 →
0 × 10 + 3 + 1
=
4
counting 1s in
1…
13
1
3
ones contributed by each place value
2
units
+
4
tens
count = 6
algo
master
.
io
Step:
Count how many times the digit 1 appears across every number from 1 to 13.
0 / 6
Input
n = 13
n = 1024 (all 3 cases)
n = 100
n = 1
Custom
n
=
13
0 / 6
algo
master
.
io
Step:
Count how many times the digit 1 appears across every number from 1 to 13.