#include<bits/stdc++.h>
using namespace std;
void targetSum(int *arr, int n, int target)
{
int left = 0;
int right = n - 1;
while (left <= right)
{
int sum = arr[left] + arr[right];
if (sum > target)
{
right–;
}
else if (sum < target)
{
left++;
}
else
{
cout << arr[left] << " and " << arr[right] << endl;
left++;
right–;
}
}
}
int main() {
int n;
cin>>n;
if(n > 1 && n < 1000)
{
int arr[n];
for(int i=0;i<=n;i++){
cin>>arr[i];
}
int x;
cin>>x;
sort(arr, arr + n);
targetSum(arr,n,x);
}
return 0;
}
Code is not passing testcases
hi @cs17b010_60782e7cd0491d73 write left < right not <= else it will count same index twice
say 15 9 is there and u r looking for 10 then left and right will both point to 5 sum will be 10 but they should be 2 diff nos
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.