Target Sum Pairs Problem

#include<bits/stdc++.h>
#include
using namespace std;
void targetSum(int a[], int n, int target)
{ pair <int,int> p1;
pair <int,int> p2;
int count=2;
int currentSum=0;
int i=0;
int j=n-1;
while(count!=0){
currentSum=a[i]+a[j];
if(currentSum==target)
{
if(count==2)
{
p1.first=a[i];
p1.second=a[j];
i++;
j–;
}
else{
p2.first=a[i];
p2.second=a[j];
i++;
j–;
}
}
else if(currentSum>target)
{
j–;
}
else{
i++;
}
}
cout<<p1.first<<“and”<<p1.second<<endl;
cout<<p2.first<<“and”<<p2.second<<endl;
}
int main() {
int n;
cin>>n;
int arr[n];
for(int i=0;i<=n;i++){
cin>>arr[i];
}
int x;
cin>>x;
targetSum(arr,n,x);
return 0;
}

Can you please tell me the mistake I am doing?

/bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe

hi @vswapnil2212,
please share the link of code by saving at ide.codingblocks.com
the code shared here is not readable

hi @vswapnil2212,
please refer here https://ide.codingblocks.com/s/660593 ive commented in detailed
(you dont have to over complicate the code try writing it simple :slight_smile:)

Okay. Your approach is much simpler to understand. But, can you please tell me the mistakes I have done in my code?

@vswapnil2212, sure…just comment down t code with logic and save it