Confusing Number - Problem

A confusing number is a number that when rotated 180 degrees becomes a different number with each digit valid.

We can rotate digits of a number by 180 degrees to form new digits:

  • When 0, 1, 6, 8, 9 are rotated 180 degrees, they become 0, 1, 9, 8, 6 respectively
  • When 2, 3, 4, 5, 7 are rotated 180 degrees, they become invalid

Note: After rotating a number, we can ignore leading zeros. For example, after rotating 8000, we have 0008 which is considered as just 8.

Given an integer n, return true if it is a confusing number, or false otherwise.

Input & Output

Example 1 — Single Confusing Digit
$ Input: n = 6
Output: true
💡 Note: When rotated 180°, digit 6 becomes 9. Since 6 ≠ 9, it's a confusing number.
Example 2 — Two-Digit Confusing Number
$ Input: n = 89
Output: true
💡 Note: 8 rotates to 8, 9 rotates to 6. Rotated number is 68. Since 89 ≠ 68, it's confusing.
Example 3 — Invalid Digit
$ Input: n = 11
Output: false
💡 Note: Both 1s rotate to 1s, giving 11. Since 11 = 11, it's not confusing (same number).

Constraints

  • 0 ≤ n ≤ 109

Visualization

Tap to expand
Confusing Number - Single Pass with Direct Mapping INPUT 6 Valid Digits for Rotation: 0 1 6 8 9 Rotation Mapping: 0 --> 0 1 --> 1 6 --> 9 8 --> 8 9 --> 6 Invalid: 2, 3, 4, 5, 7 n = 6 ALGORITHM STEPS 1 Initialize Store original, rotated = 0 2 Extract Digit digit = n % 10 = 6 3 Check Valid + Map 6 is valid, maps to 9 4 Build Rotated rotated = 0*10 + 9 = 9 6 --> 9 180° rotation Compare: original(6) != rotated(9) Different! Return true FINAL RESULT Original vs Rotated 6 Original != 9 Rotated Output: true 6 rotated 180° becomes 9 which is different from 6 OK - Confusing Number! Key Insight: A confusing number must: (1) contain only valid rotatable digits {0,1,6,8,9}, and (2) produce a DIFFERENT number after 180° rotation. Use a single pass to build the rotated number while validating digits. Time: O(log n), Space: O(1). TutorialsPoint - Confusing Number | Single Pass with Direct Mapping
Asked in
Google 15 Facebook 8
12.0K Views
Medium Frequency
~15 min Avg. Time
430 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