rsudhanshu138's blog

By rsudhanshu138, history, 3 years ago, In English

I am facing a runtime issue in my code.Can anyone help me with my code: My code link: https://pastebin.com/qCC4GsPS. Just check the merge and mergesort function in this link.

Full text and comments »

  • Vote: I like it
  • -6
  • Vote: I do not like it

By rsudhanshu138, history, 4 years ago, In English

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 <vector>
  #include <algorithm>
 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 &mdash; 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 and;
  }

 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';

   }
   }

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it