Confusing Number
Understanding the Problem
A number is considered confusing if after rotating its digits by 180 degrees, we get a valid number that is different from the original number.
Steps to Solve
- Create a Mapping: First, we need to create a mapping for valid rotations. This mapping will contain which digit can be converted to which digit after rotation.
- Rotate the Number: We will read the digits of the number from right to left and apply the rotation according to our mapping. If we find an invalid digit that cannot be rotated, we return
False
. - Compare with the Original: Finally, we will compare the rotated number with the original number. If they are the same, return
False
. If they are different, returnTrue
.
Code Implementation
|
|
Examples
- For
n = 6
, the rotated number is9
, so the result isTrue
. - For
n = 89
, the rotated number is68
, so the result isTrue
. - For
n = 11
, the rotated number is11
, so the result isFalse
.
Complexity
The time complexity of this solution is O(log n)
, where n
is the input number, and the space complexity is O(1)
as we are using a constant amount of extra space.