Target sum problem

#include
#include<unordered_map>
#include
using namespace std;

unordered_map<int ,int> mp;
int t;
void checktarget(int* arr,int n)
{
for(int i=0;i<n;i++)
{
if(mp.find(t-arr[i])==mp.end())
{
mp.insert({arr[i],i});
}
else
{
cout<<mp.at(t-arr[i])<<" and "<<i<<endl;
}

}

}
int main() {
int n;
int target;
int arr[n];
cin>>n;
for(int i=0;i<=n;i++){
cin>>arr[i];
}
cin>>target;
cout<<target<<"…"<<endl;
t=target;
sort(arr,arr+n);
checktarget(arr,n);
return 0;
}

could not print target value.

hi @sharmasrishti891
u can simply use 2 pointer method for this ques…
refer this–>

Sir,Please help me to figure out error in the code which I posted.

hi @sharmasrishti891
considering the logic u used(ie maps) and the way o/p order is expected refer this code

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.