forked from Thelalitagarwal/GFG_Daily_Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSatisfy the equation.cpp
More file actions
31 lines (31 loc) · 890 Bytes
/
Satisfy the equation.cpp
File metadata and controls
31 lines (31 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
vector<int> satisfyEqn(int A[], int N) {
// code here
unordered_map<int,pair<int,int>>mp;
vector<int>ans;
for(int i=0;i<N-1;i++)
{
for(int j=i+1;j<N;j++)
{
int sum=A[i]+A[j];
if(mp.find(sum)!=mp.end())
{
if(mp[sum].first!=i && mp[sum].second!=i && mp[sum].first!=j && mp[sum].second!=j)
{
vector<int>v={mp[sum].first,mp[sum].second,i,j};
if(ans.size()==0 || ans>v)
ans=v;
}
}
else
mp[sum]={i,j};
}
}
if(ans.empty()){
vector<int>v={-1,-1,-1,-1};
return v;
}
return ans;
}
};