hi, let Avik connect you.
** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.
** You can check all the posts on the Posts page.
** More about me in the About Me section.
Confusing Number
Problem: 1056. Confusing Number
Problem Link: leetcode.com/problems/confusing-number/ [ Premium ]
Alternative Link: luminouscl.github.io/_posts/2020-10-06-Leetcode-1056-Confusing-Number/
Companies Aksed: Google
Approach:
Reverse the number.
If any of 2, 3, 4, 5, or 7 are encountered then return False.
If 6 is encountered make the reversed digit 9.
If 9 is encountered make the reversed digit 6.
Return true if the original number and the reversed number are not equal. Else return False.
Time and Space Complexity:
Time Complexity: O(length of digit in N)
Space Complexity: O(1)
Code [C++]
bool confusingNumber(int N){
int reversed = 0, tempN = N;
while(N){
int rem = N % 10;
if(rem == 2 || rem==3 || rem==4 || rem==5 || rem == 7) return false;
if(rem == 9) rem = 6;
if(rem == 6) rem = 9;
reversed *= 10;
reversed += rem;
N /= 10;
}
return (reversed != tempN);
}