# 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.

Below is C++ implementation of above idea.

`// C++ program to find if a given number is fancy or not. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `bool` `isFancy(string& num) ` `{ ` ` ` `// To store mappings of fancy pair characters. For example ` ` ` `// 6 is paired with 9 and 9 is paired with 6. ` ` ` `map<` `char` `, ` `char` `> fp; ` ` ` `fp[` `'0'` `] = ` `'0'` `; ` ` ` `fp[` `'1'` `] = ` `'1'` `; ` ` ` `fp[` `'6'` `] = ` `'9'` `; ` ` ` `fp[` `'8'` `] = ` `'8'` `; ` ` ` `fp[` `'9'` `] = ` `'6'` `; ` ` ` ` ` `// Find number of digits in given number ` ` ` `int` `n = num.length(); ` ` ` ` ` `// Traverse from both ends, and compare characters one by one ` ` ` `int` `l = 0, r = n-1; ` ` ` `while` `(l<=r) ` ` ` `{ ` ` ` `// If current characters at both ends are not fancy pairs ` ` ` `if` `(fp.find(num[l]) == fp.end() || fp[num[l]] != num[r]) ` ` ` `return` `false` `; ` ` ` `l++; ` ` ` `r--; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `string str = ` `"9088806"` `; ` ` ` `isFancy(str)? cout << ` `"Yes"` `: cout << ` `"No"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Yes

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.

## Recommended Posts:

- 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