A confusing number is a number that when rotated 180 degrees becomes a different number with each digit valid.
We can rotate digits as follows:
0
, 1
, 8
rotate to themselves,2
and 5
rotate to each other (in this case they are different),6
and 9
rotate to each other (in this case they are different),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 different than 6.
Example 2:
Input: n = 89 Output: true Explanation: We get 68 after rotating 89, 68 is different than 89.
Example 3:
Input: n = 11 Output: false Explanation: We get 11 after rotating 11, 11 is the same as 11.
Constraints:
0 <= n <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To solve this 'Confusing Number' problem the hard way, we'll check every single number from 1 up to the given number. For each number, we'll see if it's a 'confusing number' based on the rules.
Here's how the algorithm would work step-by-step:
def confusing_number_brute_force(number_limit):
confusing_numbers_count = 0
for current_number in range(1, number_limit + 1):
rotated_number = rotate_number(current_number)
# Check if the rotated number is different from the original
if rotated_number != current_number:
confusing_numbers_count += 1
return confusing_numbers_count
def rotate_number(number):
number_string = str(number)
rotated_string = ''
valid_rotations = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}
for digit in reversed(number_string):
# Invalid numbers like 2, 3, 4, 5, 7 are not confusing.
if digit not in valid_rotations:
return -1
rotated_string += valid_rotations[digit]
return int(rotated_string)
The task is to determine if a number is confusing. A confusing number is one that, when rotated 180 degrees, becomes a different valid number. Instead of checking every number up to the input, we'll focus on figuring out which numbers can be rotated to create a new valid number.
Here's how the algorithm would work step-by-step:
def is_confusing_number(number):
rotation_map = {
'0': '0',
'1': '1',
'6': '9',
'8': '8',
'9': '6'
}
number_string = str(number)
rotated_string = ''
# Iterating in reverse to simulate 180-degree rotation
for digit in reversed(number_string):
if digit not in rotation_map:
return False
rotated_string += rotation_map[digit]
rotated_number = int(rotated_string)
# Numbers must be different after rotation to be considered 'confusing'
if rotated_number != number:
return True
else:
return False
Case | How to Handle |
---|---|
Input N is zero | Return False as 0 is not a confusing number as it becomes 0 when rotated. |
Input N is a negative number | Return False since the problem deals with positive integers and rotation does not apply to negative integers. |
Input N contains digits other than 0, 1, 6, 8, and 9 | Return False immediately since any other digits are invalid. |
Input N is a single digit number (e.g., 0, 1, 6, 8, 9) | Return True only if the rotated number is different; 0, 1, and 8 will return False and 6/9 will return True. |
Input N is a large number that may cause integer overflow during rotation | Use appropriate data types (e.g., long) to prevent potential integer overflows during the rotation process. |
Input N has leading zeros, potentially altering the confusing number result. | Treat the number as an integer, which implicitly ignores the leading zeros, before processing. |
All digits of N are the same, resulting in the same number after rotation. | If all digits are 0, 1, or 8, then it is not a confusing number, and should return false. |
Input N is the maximum integer value. | Handle the rotation of each digit carefully to prevent overflow or incorrect mapping. |