Numbers At Most N Given Digit Set

Revision en1, by rsudhanshu138, 2020-08-18 11:21:05

How to approach this question when 0 is also included in the digit set. My solution is giving the wrong answer for this.

problem link;https://leetcode.com/problems/numbers-at-most-n-given-digit-set/

include<bits/stdc++.h>

#include #include using namespace std; #define ll long long int

ll s1(vector &v, ll N) {

ll i, j, n = v.size(), ans = 0; string ss = "", s = "";

for (i = 0; i < n; i++) { s += v[i]; } ss = to_string(N);

ll d = ss.length(), f = 0;

// for numbers with atmost (d-1) digits we have n choices for each position for (i = 1; i < d; i++) { if (i == 1) { ans += pow(n — 1, i); continue; } ans += pow(n, i); } ll z = 0;

for (j = 0; j < d; j++) { f = 0; //to check whether that particular digit of N exists in the given string or not

for (i = 0; i < s.length(); i++)
{
    if (z == 0)
    {
       z++;
       continue;
    }
    //if a digit less than the current digit is encountered, we have 'n' possibilites for each digit to the right of 'i'
    if (s[i] < ss[j])
    {

       ans += pow(n, d - (j + 1));
       z++;
    }
    else if (s[i] == ss[j])
    {
       z++;
       f = 1;
       break;
    }
}
if (!f)
{
    break;
} //if we do not have jth digit in 's' , no need to continue

} ans += f;

return ans; }

int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);

ll l, r;
ll k, i, g;

cin >> l ;
vector<ll> v1;

ll x = 0;
while (x <= 9)
{
    v1.push_back(x);
    x = x + k;
}

vector<string> s;

for (i = 0; i < v1.size(); i++)
{
    g = v1[i];
    char c = g + '0';
    string d(1, c);
    s.push_back(d);
    // cout << D[i] << " ";
}

cout << s1(l) << '\n';

} }

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English rsudhanshu138 2020-08-18 11:25:36 1 Tiny change: 'blem link;https://le' -> 'blem link; https://le'
en3 English rsudhanshu138 2020-08-18 11:23:50 66
en2 English rsudhanshu138 2020-08-18 11:22:21 74
en1 English rsudhanshu138 2020-08-18 11:21:05 1753 Initial revision (published)