How can i make this code more faster plz help

this problem is from ZCO13003

#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);

vector v;
vector ars;
int a,b,c;
cin>>a;
cin>>c;

while(a–){
cin>>b;
v.push_back(b);
}
int sum,fake=0;
for (int i=0;i<v.size()-1;i++){
if (v[i]>=c) continue;
for (int j=i+1;j<v.size();j++){
sum=v[i]+v[j];
ars.push_back(sum);
}}
for (int x:ars){
if (x<c){
fake+=1;
}
}
cout<<fake;
return 0;}

hey is your q is. you need to find number of pairs having sum less than c?if this is the case i think you can do it in a single loop.

Plz tell how I am begainer :blush::slight_smile:

see first sort your array now take 2 pointers i and j, point both to (n-1), now decrease j while (array[j] +array[i]) >c.
now your answer will be (j-i)(i+1)+((i)(i+1))/2.
i think you can figure out the formula by your own, its not tough if you are using pen and paper. Good luck :slight_smile:

1 Like

Can you tell what the question?