Chef and sum

I am getting TLE in a problems submission
the link to the problem is
https://www.codechef.com/problems/CSUM

My solution to this problem

Blockquote
#include<bits/stdc++.h>
using namespace std;
/*
do not voilate rules
*/
void fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}

int main()
{
fastio();
long long int t;//number of testcase
cin>>t;
while(t–)
{
long long int n,k,x;
cin>>n>>k;
vectorvec;
for(long long int i=0;i<n;i++)
{
cin>>x;
vec.push_back(x);
}
bool print=false;
for(long long int i=0;i<n;i++)
{
auto it=find(vec.begin(),vec.end(),k-vec[i]);
if(it!=vec.end()&&abs((it-vec.begin())!=i))
{
cout<<β€œYes”<<endl;
print=true;
break;
}
}

  if(!print)
  cout<<"No"<<endl;
 
}

}

please help me . I am getting this question as partially correct.

@tmwilliamlin @df

find function in vector takes O(N). So overall your complexity is O(N^2).

Hint

Use Two Pointers

1 Like

okay i thought it takes O(LOGN) thanks a lot for help

1 Like