CLPNT EDITORIAL

I used a similar approach but got a wrong answer, here’s my solution CodeChef: Practical coding for everyone

please help

most welcome !

1 Like

I am finding lower bound for each x+y on walls and if x+y is equal to value if wall it is -1 else it is the index , my solution is giving WA can somebody help
my solution

https://www.codechef.com/viewsolution/35672769
This is my Code I am new cannot understand whats wrong.
Also I am getting TLE error
can someone correct

same logic but used lower_bound stl (even tried upper_bound) but got WA?
any idea how??

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin>>t;
while(t–)
{
long long int n,a;
cin>>n;
vector arr;
for(long long i=0;i<n;i++)
{
cin>>a;
arr.push_back(a);
}
long long int q;
cin>>q;
while(q–)
{
long long int x,y,res=0,flag=0;
cin>>x>>y;
if(x==0 && y==0)
res=0;
else
{
auto itr=lower_bound(arr.begin(),arr.end(),(x+y))-arr.begin();
//cout<<“itr=”<<itr<<endl;
if(arr[itr]==(x+y))
flag=1;
else
res=itr;
}
if(flag==1)
cout<<"-1\n";
else
cout<<res<<"\n";
}
}
return 0;
}

https://www.codechef.com/viewsolution/35700782
Can anyone see why my B Search approach is getting TLE?

1 Like

Can someone tell me why am i getting WA.
https://www.codechef.com/viewsolution/35658188

https://www.codechef.com/viewsolution/35660483
@souradeep1999
Can you provide an example for the above solution, so i can understand why it was failing? Thanks!

Same with me I applied leastgreatest

hey fixed ur code a , commented for better understanding
link
u committed ; ss=x+y not max(x,y)
let me know if u have querry @soumya_2882

hello brother,
fixed error modified code with comments
let me know if anything is unclear @anon56550829

1 Like

I used a similar way but got a wrong answer, please help here’s my solution.
https://www.codechef.com/viewsolution/35669865
@souradeep1999 @faisal1947 @jyoti369

thank you so much,
just one question that is why do i get O[n*2] when I used elif when checking x+y in wall

im glad it helped @tn6541
x+y in wall: is O(N) time (linear search in list internally)
and this is inside loop which runs q times hence it becomes O(q*N)
thats why i made a set of all a’s elements ,x+y in set is O(1)

1 Like

when you declare a local variable it is stored in the stack memory
Global variables are stored in heap memory.Online compilers mai often there is a limit of stack memory.Heap memory limit is large.

So to search an elements in a set is faster than in a list?

yup… @anon56550829 its O(1) unlike list which is O(N)

1 Like

https://www.codechef.com/viewsolution/35674052

This is my solution approach. Please help to find error in the code, I am getting wrong answer.

bro @pranava23 could not understand ur approach,
will u plz let me know how did u approached because its certainly not similar to anyone approach i have seen and debug so far.
thank you.

brother @tech_29
i don’t know java much but i tried to debug:
portion of your code:

     while(q>0){
    		        int x = sc.nextInt();
    		        int y = sc.nextInt();
    		        
    		        int result = 0;
    		        for(int i=0;i<n;i++){
    		            if((x+y)==a[i]){
    		                result = -1;
    		                break;
    		            }      
    		        }
    		        if(result==0){
    		            for(int i=0;i<n;i++){
    		            if(a[i]>x && a[i]>y)
    		                break;
    		            else 
    		                result++;
    		            }      
    		        }

error in line 32(of ur submitted code)
if(a[i]>x && a[i]>y) should be if (a[i]>x+y):
after also after loop check
if a[i]>x+y:
print i+1
else:
print(i)

anyways:
no need two separated for loops both can be done in single loop
moreover:
even if u rectify mistakes u would get TLE since for overall complexity would become (q*N) use BS instead of for loop

please let me know if anything is unclear
hope this helps