Getting TimeLimit on Second Test Case Don’t Know Why???
Arrays-Target Sum Pairs
#include #include using namespace std; int main() { int l,r; int arr[1000]; int n; int target; int sum =0; cin>>n; l=0; r=n-1; for(int i=0;i<n;i++){ cin>>arr[i]; } sort(arr,arr+n); cin>>target; while(l<r){ sum = arr[l]+arr[r]; if(sum == target){ cout<<arr[l]<<" and "<<arr[r]<<endl; l++; r–; } else{ r–; } } return 0; }
Hi @Madhukar
In the while loop you are not considering if sum is less or greater than target. You are just reducing r when target is not equal to sum. Instead you should consider if sum is less than target then do l++ and if sum is greater than target then do r–.
Here is your corrected code :