Confusing Number - Problem

Ever wondered what happens when you flip a digital clock upside down? Some numbers look completely different, while others become invalid!

A confusing number is a number that transforms into a different valid number when rotated 180 degrees. Here's the magic behind the rotation:

  • 0 rotates to 0
  • 1 rotates to 1
  • 6 rotates to 9
  • 8 rotates to 8
  • 9 rotates to 6
  • 2, 3, 4, 5, 7 become invalid when rotated

Key Rules:

  • After rotation, leading zeros are ignored (e.g., 8000 โ†’ 0008 โ†’ 8)
  • The rotated number must be different from the original to be confusing
  • All digits must be valid after rotation

Goal: Given an integer n, return true if it's a confusing number, false otherwise.

Input & Output

example_1.py โ€” Simple Confusing Number
$ Input: n = 6
โ€บ Output: true
๐Ÿ’ก Note: When 6 is rotated 180ยฐ, it becomes 9, which is different from the original 6, making it confusing.
example_2.py โ€” Non-Confusing Number
$ Input: n = 89
โ€บ Output: true
๐Ÿ’ก Note: 8 rotates to 8, and 9 rotates to 6. So 89 becomes 68 when rotated, which is different from 89.
example_3.py โ€” Invalid Digit
$ Input: n = 11
โ€บ Output: false
๐Ÿ’ก Note: Both 1s rotate to 1s, so 11 becomes 11 when rotated. Since it's the same as the original, it's not confusing.

Visualization

Tap to expand
Digital Clock Rotation: 89 โ†’ 6889Original8 โ†’ 8Valid!9 โ†’ 6Valid!Rotated: 86โ†“ Reverse โ†“68Final Resultโœ… 68 โ‰  89Result: CONFUSING NUMBER!
Understanding the Visualization
1
Original Number
Start with the input number displayed on a digital screen
2
Digit Validation
Check if each digit can be validly rotated (only 0,1,6,8,9 are valid)
3
Apply Rotation
Transform each digit: 0โ†’0, 1โ†’1, 6โ†’9, 8โ†’8, 9โ†’6
4
Flip Display
Reverse the entire number (simulating 180-degree rotation)
5
Compare Results
Check if the rotated number differs from the original
Key Takeaway
๐ŸŽฏ Key Insight: Mathematical processing naturally handles the reversal while being more efficient than string manipulation!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(log n)

We process each digit once, and there are logโ‚โ‚€(n) digits in the number

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using a fixed-size rotation array and a few variables

n
2n
โœ“ Linear Space

Constraints

  • 0 โ‰ค n โ‰ค 109
  • The input is guaranteed to be a valid integer
  • Edge case: Single digit numbers like 0, 1, 8 are not confusing as they remain the same
Asked in
Google 25 Facebook 18 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~12 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