244B. Undoubtedly Lucky Numbers - Codeforces Solution C++

  Problem Link : 244B. Undoubtedly Lucky Numbers 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int
ll n;
ll ans;
void func(ll v, ll one, ll two)
{
	if(v>n)
		return;
	//cout<<v<<" ";
	ans++;
	ll t;
	if(two==-1)
    {
        for(ll i=0;i<=9;i++)
        {
            t=v*10+i;
                if(i==one)
                    func(t,one,-1);
                else
                    func(t,one,i);

        }
    }
    else{

            t=v*10+one;
                func(t,one,two);
            t=v*10+two;
                func(t,one,two);

    }
}

int main()
{

	ans=0;
	cin>>n;
	if(n<=10)
	{
		cout<<n;
		exit(0);
	}

	for(ll i=1;i<=9;i++)
		func(i,i,-1);



	cout<<ans;


}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!