Getting wrong ans

#include <bits/stdc++.h>
using namespace std;
/store the prices in a vector then sort the vector
iterate the vector and finding out pairs where first+second = money
it is given that i<=j therefore this condition if(x<=(money/2)) is written
this means discarding 8,2 and keeping 2,8 when money is 10.
if the element in vector is 2 then 10-2 is searched in vector and if it is found
then map with key equal to diff is stored with the pair<x,y> where x+y=money and x<=y
since the map is sorted, the first element of map is printed as the answer
/
int main() {
int t,n;
cin>>t;
long long int price,money;
while(t–){
vector v;
map<long long int,pair<long long int,long long int>> m;
cin>>n;
while(n–){
cin>>price;
v.push_back(price);
}
cin>>money;
sort(v.begin(),v.end());
for(auto x:v){
long long int first_val,second_val,diff;
if(x<=(money-1)){
first_val = x;
second_val = money - x;
if(find(v.begin(),v.end(),second_val)!=v.end()){
diff = second_val - first_val;
if(first_val<=second_val){
m[diff] = make_pair(first_val, second_val);}
}
}
else
break;
}
cout<<β€œDeepak should buy roses whose prices are β€œ<<(*m.begin()).second.first<<” and β€œ<<(*m.begin()).second.second<<”.”<<endl;
}
return 0;
}

hello @mansi25

u r considering same element twice.

for example for this test case->
1
3
20 40 80
80
ur solution is picking 40 two times , which is wrong.

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.