Target sum pair problem

pls tell the error int he problem

Hey @kirat.singh8417


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin>>n;
    int *arr=new int[n];
	int target;
	cin>>target;
	for(int i=0;i<n;i++){
		cin>>arr[i];
	}

	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			if(arr[i]+arr[j]==target)
			cout<<min(arr[i],arr[j])<<" and "<<max(arr[i],arr[j]);
		}
	}
	return 0;
}

First input all array elements and after that target value, you are taking target value then array elements.
Complexity of this code in best case is O(n^2) , instead do what, sort all the elements of the array using sort function which is O(nlogn) and then use 2 pointer approach to find sum. Whereas your code for test case
5
4 2 0 1 3
5
Will give output
1 and 3
Whereas expected output is
1 and 4
2 and 3

pls someone tell this problem

Hey @kirat.singh8417 i have responded to your doubt.
See this

now whats the problem??

You are taking target value first and then array elements, first take all array elements then target value

int target;
int *arr=new int[n];
for(int i=0;i<n;i++){
	cin>>arr[i];
}
cin>>target;

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.