Ya it should ,
this thing messed my whole contest.
Two exactly same code.
1st got WA but 2nd got AC. How is this possible?
I used lower bound and similar solution was uploaded by someone else and i was getting WA but the other solution got AC, can you please help me figure out the difference.
Inside the testcases loop-
// ll is long long.
ll n, i, x;
cin >> n;
vector a;
for (i = 0; i < n; i++)
{
cin >> x;
a.push_back(x);
}
ll q;
cin >> q;
for (i = 0; i < q; i++)
{
ll p, z;
cin >> p >> z;
p += z;
vector::iterator lower;
lower = lower_bound (a.begin(), a.end(), p);
ll cv = lower - a.begin();
if (a[cv] == p)
cout << “-1” << endl;
else
cout << cv << endl;
}
Other Soln-
long long int n;
cin>>n;
long long int q[n];
for(long long int i=0;i<n;i++)
{
cin>>q[i];
}
long long int t;
cin>>t;
while(t–)
{
long long int x,y;
cin>>x>>y;
long long int s=x+y;
long long int d = lower_bound(q, q+n, s)-q;
if(q[d]==s)
{
cout<<"-1"<<endl;
continue;
}
else{
cout<<d<<endl;
}
}
I also faced the same issue, I tried both lowerbound as well as Binary search but was getting WA and a very similar soln was accepted
Can you please explain how you solved it with PBDS? I solved it with binary search.
I have used two approach and i think both are correct and same but the first one got WA while the latter one got AC. Here are the links:-
- wrong code- CodeChef: Practical coding for everyone
- correct code -
CodeChef: Practical coding for everyone
Can someone explain why its happening so?
This should not get accepted because lower bound can give n as return value but max array index is n-1 and in the next if statement you are trying to access that element. Which should give runtime error.
Line 27
The same case is with… exactly the same approach… one got WA other got AC
ohh right, Thankyou for helping me, btw this means that both solns shoud not get accepted right??
yes.
can u explain whats wrong in line 27 with the help of a test case
look at my reply above
I tried a lot using binary search but it give WA , can anyone provide me some testcase where my solution will give WA , I will be highly grateful and obliged to the one who answers my query satisfactorily .
Thanks a lot in advance !
My submission link : CodeChef: Practical coding for everyone
When we pass an array to a function, a pointer is actually passed but when a vector is passed to a function, a copy of the vector is created, the changes made inside the function are not reflected outside because function has a copy.It probably has something to do with your issue
Line 116.
binary_search() returns a bool, not int.
did you open those two links? They are exactly same. Exact copy of each other.
@sampras123 here is my solution link CodeChef: Practical coding for everyone now it is showing TLE but during contest it was giving WA and i wasted my whole time on this question, if still there is any bug in my code please point it.
Here is my code with binary Search : CodeChef: Practical coding for everyone
Hope this helps. Thanks !!!
I have made some changes to your code by removing functions, it worked!
(though I am not able to understand the reason for it).
Submission - CodeChef: Practical coding for everyone