Watch 8 video solutions for Confusing Number, a easy level problem involving Math. This walkthrough by Tony Teaches has 3,068 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
0, 1, 6, 8, and 9 are rotated 180 degrees, they become 0, 1, 9, 8, and 6 respectively.2, 3, 4, 5, and 7 are rotated 180 degrees, they become invalid.Note that after rotating a number, we can ignore leading zeros.
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.
Example 1:
Input: n = 6 Output: true Explanation: We get 9 after rotating 6, 9 is a valid number, and 9 != 6.
Example 2:
Input: n = 89 Output: true Explanation: We get 68 after rotating 89, 68 is a valid number and 68 != 89.
Example 3:
Input: n = 11 Output: false Explanation: We get 11 after rotating 11, 11 is a valid number but the value remains the same, thus 11 is not a confusing number
Constraints:
0 <= n <= 109Problem Overview: A number is confusing if rotating each of its digits by 180 degrees forms a valid number that is different from the original. Only digits 0, 1, 6, 8, 9 remain valid after rotation, where 6 ↔ 9 and 0, 1, 8 stay the same.
Approach 1: Digit Rotation with Math (O(d) time, O(1) space)
Iterate through the digits of the number from right to left using modulo and division. For each digit, look up its rotated value using a small mapping: {0→0, 1→1, 6→9, 8→8, 9→6}. Build the rotated number by multiplying the current result by 10 and adding the mapped digit. If you encounter a digit not in the mapping (like 2,3,4,5,7), the number cannot form a valid rotation, so return false immediately. After processing all digits, compare the rotated number with the original. If they are different, the number is confusing.
This approach works well because you never need extra storage beyond a few integers. It relies purely on arithmetic operations, making it efficient and straightforward. Problems like this commonly appear in math and simulation categories where you emulate transformations digit by digit.
Approach 2: String Build with Rotation Map (O(d) time, O(d) space)
Convert the number into a string and iterate from the last character to the first. Use a rotation map to translate each digit and append it to a new string representing the rotated number. If any character is not present in the rotation map, the number cannot be rotated and the answer is false. After building the rotated string, compare it with the original string representation.
This approach is easier to reason about during implementation since string operations make reversing and mapping digits more explicit. However, it uses extra memory proportional to the number of digits. The digit mapping structure also resembles patterns used in hash map lookups.
Recommended for interviews: The math-based digit rotation approach is what interviewers usually expect. It demonstrates comfort with integer manipulation, modulo/division operations, and constant-space thinking. The string approach still works and is often easier to code quickly, but the arithmetic solution shows stronger control over low-level number processing.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Digit Rotation with Math | O(d) | O(1) | Best general solution; efficient constant-space digit processing |
| String Build with Rotation Map | O(d) | O(d) | Useful when implementing quickly or when string manipulation is preferred |