Talentd
FleetCode
Make and follow sheets.Sign in

Confusing Number solution and explanation

EasyMath4 min read49.8% acceptanceUpdated 23 Mar 2026Premium elsewhere, free here
Confusing Number I & II: Leetcode 1056/1088Tony Teaches · 15:41 · 3K viewsWatch on YouTube
7 more videos on this problem
  1. Confusing Number I & II: Leetcode 1056/1088Tony Teaches · 15:41 · 3K views
  2. 1056. Confusing Number - Week 1/5 Leetcode January ChallengeProgramming Live with Larry · 7:23 · 253 views
  3. Google Interview Question - Confusing Number- Leetcode 1056- PythonSephorus · 5:20 · 193 views
  4. LeetCode Python: Confusing Number IThe Bro Codes · 6:02 · 155 views
  5. LeetCode Premium: 1056. Confusing Number #leetcode #coding #interview #code #codinginterviewSuper Lazy Coder · 10:39 · 64 views
  6. LeetCode 1056 | Confusing Number | HashMap | JavaSleepy Cracker · 5:13 · 52 views
  7. No Cuts: LeetCode 1056 Confusing Number Mock Coding Interview in PythonHollany · 14:25 · 25 views
  8. LeetCode 1056: Confusing NumberAlitaCode · 26:38 · 8 views

The 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, 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

class Solution:
    def confusingNumber(self, n: int) -> bool:
        x, y = n, 0
        d = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6]
        while x:
            x, v = divmod(x, 10)
            if d[v] < 0:
                return False
            y = y * 10 + d[v]
        return y != n
class Solution {
    public boolean confusingNumber(int n) {
        int[] d = new int[] {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
        int x = n, y = 0;
        while (x > 0) {
            int v = x % 10;
            if (d[v] < 0) {
                return false;
            }
            y = y * 10 + d[v];
            x /= 10;
        }
        return y != n;
    }
}
class Solution {
public:
    bool confusingNumber(int n) {
        vector<int> d = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
        int x = n, y = 0;
        while (x) {
            int v = x % 10;
            if (d[v] < 0) {
                return false;
            }
            y = y * 10 + d[v];
            x /= 10;
        }
        return y != n;
    }
};
func confusingNumber(n int) bool {
	d := []int{0, 1, -1, -1, -1, -1, 9, -1, 8, 6}
	x, y := n, 0
	for x > 0 {
		v := x % 10
		if d[v] < 0 {
			return false
		}
		y = y*10 + d[v]
		x /= 10
	}
	return y != n
}
class Solution {
    /**
     * @param Integer $n
     * @return Boolean
     */
    function confusingNumber($n) {
        $d = [0, 1, -1, -1, -1, -1, 9, -1, 8, 6];
        $x = $n;
        $y = 0;
        while ($x > 0) {
            $v = $x % 10;
            if ($d[$v] < 0) {
                return false;
            }
            $y = $y * 10 + $d[$v];
            $x = intval($x / 10);
        }
        return $y != $n;
    }
}
Try this approach in the editor

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

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.

Comments

Now write it yourselfClose this page and solve Confusing Number in the editor, without looking.
Practise Confusing Number