Confusing Number II - Problem
Confusing Number II is a fascinating problem about rotational symmetry in digits! ๐
Imagine you have a digital clock that's been rotated 180 degrees - some numbers still look valid, but they might represent different values! Only the digits
A confusing number is one that:
1. Contains only rotatable digits (
2. When rotated 180ยฐ, becomes a different valid number
3. Leading zeros after rotation are ignored
Examples:
โข
โข
โข
โข
Goal: Count how many confusing numbers exist in the range
Imagine you have a digital clock that's been rotated 180 degrees - some numbers still look valid, but they might represent different values! Only the digits
0, 1, 6, 8, 9 remain valid when rotated: they become 0, 1, 9, 8, 6 respectively. Digits 2, 3, 4, 5, 7 become unreadable gibberish.A confusing number is one that:
1. Contains only rotatable digits (
0, 1, 6, 8, 9)2. When rotated 180ยฐ, becomes a different valid number
3. Leading zeros after rotation are ignored
Examples:
โข
6 โ rotates to 9 (confusing! โ
)โข
89 โ rotates to 68 (confusing! โ
)โข
11 โ rotates to 11 (same number, not confusing โ)โข
25 โ contains invalid digits (not confusing โ)Goal: Count how many confusing numbers exist in the range
[1, n]. Input & Output
example_1.py โ Basic Case
$
Input:
n = 20
โบ
Output:
6
๐ก Note:
The confusing numbers in [1,20] are: 6โ9, 9โ6, 10โ01=1, 16โ91, 18โ81, 19โ61. Each transforms to a different valid number when rotated 180ยฐ.
example_2.py โ Small Range
$
Input:
n = 100
โบ
Output:
19
๐ก Note:
All single-digit confusing numbers (6,9) plus valid two-digit confusing numbers like 10,16,18,19,60,61,66,68,69,80,81,86,89,90,91,96,98,99 but excluding those that rotate to themselves.
example_3.py โ Edge Case
$
Input:
n = 1
โบ
Output:
0
๐ก Note:
No confusing numbers exist in [1,1] since 1 rotates to itself (1โ1), which makes it not confusing by definition.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Rotatable Digits
Only 0,1,6,8,9 remain valid when rotated: 0โ0, 1โ1, 6โ9, 8โ8, 9โ6
2
Check Each Number
For numbers 1 to n, verify they contain only rotatable digits
3
Rotate and Compare
Rotate each valid number 180ยฐ and check if it becomes different
4
Count Confusing Numbers
Sum up all numbers that change when rotated (these confuse customers!)
Key Takeaway
๐ฏ Key Insight: Use backtracking to generate only potentially confusing numbers (containing digits 0,1,6,8,9) rather than checking every number up to n - this dramatically improves efficiency from O(n) to O(5^k) where k is the number of digits!
Time & Space Complexity
Time Complexity
O(5^k)
We have 5 valid digits and at most k=logโโ(n) digit positions, but pruning makes it much faster in practice
โ Linear Growth
Space Complexity
O(k)
Recursion depth is at most k digits, where k=logโโ(n)
โ Linear Space
Constraints
- 1 โค n โค 109
- Only digits 0, 1, 6, 8, 9 are valid when rotated 180ยฐ
- Leading zeros are ignored after rotation (e.g., 8000 โ 0008 = 8)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code