Arrays- target sum Pairs

i am getting the last test case wrong can you please help me correct it
here is the code
#include
#include

using namespace std;

int main() {
int n,target;
cin>>n;
int a[n+1];
for(int i=1;i<=n;i++){
cin>>a[i];
}

cin>>target;
sort(a, a+n);
for(int i=0;i<=n;i++){
	for(int j=i+1;j<=n;j++){
		
		if((a[i]+a[j])==target){
			cout<<a[i]<<" and "<<a[j]<<endl;
		}
		
	}
}

return 0;

}