Rotated Digits - Problem
An integer x is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from x.
Each digit must be rotated - we cannot choose to leave it alone. A number is valid if each digit remains a digit after rotation:
0,1, and8rotate to themselves2and5rotate to each other6and9rotate to each other- The rest of the numbers (
3,4,7) do not rotate to any other number and become invalid
Given an integer n, return the number of good integers in the range [1, n].
Input & Output
Example 1 — Basic Case
$
Input:
n = 10
›
Output:
4
💡 Note:
Numbers 1-10: Good numbers are 2 (→5), 5 (→2), 6 (→9), 9 (→6). Numbers like 1 (→1) and 8 (→8) are same after rotation, so not good.
Example 2 — Smaller Range
$
Input:
n = 1
›
Output:
0
💡 Note:
Only number 1, which rotates to itself (1→1), so it's not different. No good numbers.
Example 3 — Larger Range
$
Input:
n = 25
›
Output:
9
💡 Note:
Good numbers are: 2, 5, 6, 9, 12, 15, 16, 19, 20. Numbers like 25 (→52) are good, but 22 (→55) contains invalid digits after 10.
Constraints
- 1 ≤ n ≤ 104
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code