Skip to main content

Confusing Number - Solution & Explanation

EasyPremiumFree on FleetCodeMath5 min readAsked at: Google
Practice this problem

Problem Statement

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, and 9 are rotated 180 degrees, they become 0, 1, 9, 8, and 6 respectively.
  • When 2, 3, 4, 5, and 7 are rotated 180 degrees, they become invalid.

Note that 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.

 

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 <= 109

Approach Overview

Problem 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.

Solution

Code

Python

Java

C++

Go

PHP

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Digit Rotation with MathO(d)O(1)Best general solution; efficient constant-space digit processing
String Build with Rotation MapO(d)O(d)Useful when implementing quickly or when string manipulation is preferred

Video Solution

Confusing Number I & II: Leetcode 1056/1088Tony Teaches3,111 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Confusing Number easy or hard?
Confusing Number is classified as an Easy problem on LeetCode with an acceptance rate around 49%. The challenge mainly involves recognizing valid digit rotations and constructing the rotated number correctly.
Confusing Number Python/Java solution
Implement a mapping for valid rotations and iterate through the digits using modulo and division. Build the rotated number and compare it with the original. The same logic works across Python, Java, C++, Go, and PHP with O(d) time and constant extra space.
How to solve Confusing Number in O(n)?
Treat n as the number of digits. Iterate through the digits from right to left, map each digit to its rotated equivalent, and construct the new number. If any digit is invalid (2,3,4,5,7), return false. After building the rotated value, check if it differs from the original number.
What is the best approach for Confusing Number?
The best approach rotates digits using arithmetic operations and a mapping of valid rotations (0→0, 1→1, 6→9, 8→8, 9→6). Extract digits with modulo, build the rotated number in reverse order, and compare it with the original. This runs in O(d) time with O(1) space where d is the number of digits.
Is Confusing Number asked at Google/Amazon/Meta?
Confusing Number appears in interview preparation sets related to number manipulation and digit transformations. Variants such as Confusing Number II are more frequently reported in big tech interview discussions, especially for companies like Google and Amazon.
What data structure is used in Confusing Number?
Most solutions use a small constant mapping structure that maps digits to their rotated equivalents. This can be implemented with a hash map, array, or switch statement since there are only five valid digits (0,1,6,8,9).
What is the time complexity of Confusing Number?
The time complexity is O(d) because each digit of the number is processed once. The algorithm extracts digits using modulo and division operations and constructs the rotated value step by step. Space complexity can be O(1) when using pure arithmetic.

Ready to solve this problem?

Practice Confusing Number with our built-in code editor and test cases.

Practice on FleetCode