Check if a given number is Fancy - GeeksforGeeks
Check if a given number is Fancy
A fancy number is one which when rotated 180 degrees is the same. Given a number, find whether it is fancy or not.
180 degree rotations of 6, 9, 1, 0 and 8 are 9, 6, 1, 0 and 8 respectively
Examples:
Input: num = 96 Output: Yes If we rotate given number by 180, we get same number Input: num = 916 Output: Yes If we rotate given number by 180, we get same number Input: num = 996 Output: No Input: num = 121 Output: No
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to create a map to store fancy pair mappings. After creating map, traverse given number from both ends and if at any point characters at current ends are not fancy pairs, return false. This algorithm is similar to palindrome check algorithm.
Read full article from Check if a given number is Fancy - GeeksforGeeks
No comments:
Post a Comment