Skip to content
Related Articles

Related Articles

Check if a given number is Fancy
  • Difficulty Level : Easy
  • Last Updated : 18 Sep, 2019

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 the implementation of above idea.

C++




// 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;
}

Java




// Java program to find if a given number 
// is fancy or not
import java.util.*;
  
class GFG 
{
static boolean isFancy(String num) 
    // To store mappings of fancy pair characters. 
    // For example 6 is paired with 9 and 9 is paired with 6. 
    Map<Character, 
        Character> fp = new HashMap<Character, 
                                    Character>(); 
                                      
    fp. put('0', '0'); 
    fp. put('1', '1'); 
    fp. put('6', '9'); 
    fp. put('8', '8'); 
    fp. put('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.containsKey(num.charAt(l)) || 
             fp.get(num.charAt(l)) != num.charAt(r)) 
            return false
        l++; 
        r--; 
    
    return true
  
// Driver Code
public static void main(String[] args) 
{
    String str = "9088806"
    if(isFancy(str))
        System.out.println("Yes");
    else
        System.out.println("No");
}
  
// This code is contributed by PrinciRaj1992

C#




// C# program to find if a given number 
// is fancy or not
using System;
using System.Collections.Generic;
  
class GFG 
{
static bool isFancy(String num) 
    // To store mappings of fancy pair characters. 
    // For example 6 is paired with 9 and 9 is paired with 6. 
    Dictionary<char, char> fp = new Dictionary<char, char>();
                                      
    fp.Add('0', '0'); 
    fp.Add('1', '1'); 
    fp.Add('6', '9'); 
    fp.Add('8', '8'); 
    fp.Add('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.ContainsKey(num[l]) || 
             fp[num[l]] != num[r]) 
            return false
        l++; 
        r--; 
    
    return true
  
// Driver Code
public static void Main(String[] args) 
{
    String str = "9088806"
    if(isFancy(str))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
  
// This code is contributed by Rajput-Ji

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.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :