<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 {
public:
int f(int i,int flag)
{
    if(i==0)
    {
        if(flag==1)
        return(1);
    return(0);
    }
    if(i%10==2||i%10==5||i%10==6||i%10==9)
    {
        return(f(i/10,1));
    }
    
    if(i%10==3||i%10==4||i%10==7)
    return(0);
    return(f(i/10,flag));
}
    int rotatedDigits(int n) {
        vector<int>adj[10];
        adj[0].push_back(0);
        adj[1].push_back(1);
        adj[8].push_back(8);
        adj[2].push_back(5);
        adj[5].push_back(2);
        adj[6].push_back(9);
        adj[9].push_back(6);
        adj[3].push_back(-1);
        adj[4].push_back(-1);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=f(i,0);
        }
        return(ans);
        
    }
};
7500