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
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.
Below is the implementation of above idea.
Thanks to Gaurav Ahirwar for suggesting above solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Write a program to reverse digits of a number
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Check if a number is multiple of 5 without using / and % operators
- Check whether a given point lies inside a triangle or not
- Given a number, find the next smallest palindrome
- Check divisibility by 7
- Count the number of possible triangles
- Select a random number from stream, with O(1) space
- Program to convert a given number to words
- Efficient program to print all prime factors of a given number