<p>An integer <code>x</code> is a <strong>good</strong> if after rotating each digit individually by 180 degrees, we get a valid number that is different from <code>x</code>. Each digit must be rotated - we cannot choose to leave it alone.</p>

<p>A number is valid if each digit remains a digit after rotation. For example:</p>

<ul>
	<li><code>0</code>, <code>1</code>, and <code>8</code> rotate to themselves,</li>
	<li><code>2</code> and <code>5</code> rotate to each other (in this case they are rotated in a different direction, in other words, <code>2</code> or <code>5</code> gets mirrored),</li>
	<li><code>6</code> and <code>9</code> rotate to each other, and</li>
	<li>the rest of the numbers do not rotate to any other number and become invalid.</li>
</ul>

<p>Given an integer <code>n</code>, return <em>the number of <strong>good</strong> integers in the range </em><code>[1, n]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 0
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
    map<int,int> store = {{0,0}, {1,1}, {2, 5}, {5, 2},{6,9}, {8,8}, {9,6}};
    int checkForNumbersInSingleDigit(int n)
    {
        int count = 0;
        for(int idx = 1;idx <= n; idx++)
        {
             if(store.contains(idx) && store[idx] != idx)
               count++;
        }
        return count;
    }
    
    int checkForRemainingNumbers(int n)
    {
        int count = 0;
        int new_number = 0;
        int copy_n = n;
        int idx = 0;
        while(n > 0)
        {
            int remainder = n%10;
            cout << "rem is" << remainder << std::endl;
            if(store.contains(remainder))
                new_number = (new_number + store[remainder]*pow(10,idx));
            else
               return 0;
            
            n = n/10;
            idx++;
        }
        cout << "new_number is" << new_number << std::endl;
        return(new_number == copy_n)?0:1;
    }
    
public:
    int rotatedDigits(int n) {
       int count = 0;
        
       if(n <= 9)
         return checkForNumbersInSingleDigit(n);
       else
       {
           count += checkForNumbersInSingleDigit(9);
           for(int idx = 10; idx <= n; idx++)
           count += checkForRemainingNumbers(idx);
       }
        return count;
       
    }
};
10000