Could anyone help me out with this code....its failing for larger test cases


#1

Comment body goes here.int Solution::seats(string A)
{
vector v;
int n=A.length();
for(int i=0;i<n;i++)
{
if(A[i]==‘x’)
v.push_back(i);
}
int size=v.size();
int mid=size>>1;
int k=1;
long long int res=0;
for(int i=mid-1;i>=0;i–)
{
res+=(v[mid]-v[i]-k)%100000003;
k++;
}
k=1;
for(int i=mid+1;i<size;i++)
{
res+=(v[i]-v[mid]-k)%100000003;
k++;
}
return (res%100000003);
}


#2

Use Mod condition, answer must be overflowing if mod not used.


#3

For cases where it shows “Your code might be failing for large test cases” :

  1. ensure you are returning the mod value
  2. use ‘long long’ instead of ‘int’ (for variable result here)
    also while taking mod, you can use " result % static_cast(std::pow(10,7)+3); "

#4

return using long long works!


#5

Using res+=(v[mid]-v[i]-k)%100000003; might cause overflow. Instead use this res=(res+(v[mid]-v[i]-k))%100000003;

And also use long long int instead of int


#6

Values of mod is wrong … u have used 7 zeros in mod but there are 6.