Code Showing TLE

The code I have submitted is the same as the one provided in the solution and the one taught to us, still, it is showing that there is a time error. Please help.

@Parikalp10
a) u need to sort ur array before applying this algorithm
b) u need to print array values in place of their indices.
c) in this if statement increment i and decrement j.
image
i.e i++;
and j–;

I did that, but it is still showing TLE

image

in this else statement u need to decrement j and increment i.

did that too, still showing the same error

#include
#include
using namespace std;
void sum(int n, int a[], int sum)
{
int i=0,j=n-1,csum=0;
while(i<j){
csum=a[i]+a[j];
if(csum>sum){
j–;
}
else if(csum<sum){
i++;
}
else{
cout<<a[i]<<“and”<<a[j]<<endl;
i++;
j–;
}
}
}
int main() {
int a[1000],n,target,p;
cin>>n;
for(p=0;p<n;p++){
cin>>a[p];
}
cin>>target;
sort(a,a+n);
sum(n,a,target);
return 0;
}

please save it here ->https://ide.codingblocks.com/
and share the link

image
it is working fine.
pls submit again

okay thank you so much

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.