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 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
๐Ÿ• Digital Clock Rotation Factory ๐Ÿ”„89Normal Clockโ†’ 180ยฐ โ†’68Rotated Clockโœ“ CONFUSING!Shows different time11Normal Clockโ†’ 180ยฐ โ†’11Rotated Clockโœ— NOT CONFUSINGShows same time25Normal Clockโ†’ 180ยฐ โ†’??Invalid Displayโœ— INVALIDContains non-rotatable digits๐Ÿ”„ Rotation RulesValid Rotations:0 โ†’ 0 (unchanged)1 โ†’ 1 (unchanged)6 โ†’ 9 (changes!)8 โ†’ 8 (unchanged)9 โ†’ 6 (changes!)Invalid Digits:2, 3, 4, 5, 7๐ŸŽฏ Factory Quality Control Process1. Screen Valid Clocks: Only process clocks with digits [0,1,6,8,9]2. Rotation Test: Virtually rotate each clock 180 degrees3. Confusion Check: Mark as 'confusing' if rotated time โ‰  original time4. Count Defects: Total confusing clocks = quality control report
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Recursion depth is at most k digits, where k=logโ‚โ‚€(n)

n
2n
โœ“ 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)
Asked in
Google 15 Amazon 8 Meta 6 Microsoft 4
23.5K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen