I am getting output as 0 and 5.
where as in my array there is not 0.
MY CODE
#include
#include
using namespace std;
int compare(int a,int b){
return a<b;
}
void targetsum(int *arr,int n,int target){
sort(arr,arr+n,compare);
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;
right--;
left++;
}
}
}
int main()
{
int arr[100];
int target;
cin>>target;
int n=sizeof(arr)/sizeof(arr[0]);
for (int i = 0; i < n;i++)
{
cin>>arr[i];
}
targetsum(arr,n,target);
return 0;
}