Not getting appropriate result for the test case A={1,1,1}, B=2;

interview-questions
Tags: #<Tag:0x00007f24286cc0c0>

#1

vector Solution::twoSum(const vector &A, int B) {
map<int,int>m;
vector res;
int index1=-1, index2=-1;
int n=A.size();
int sum=0, target=-1;
for(int i=0; i<n; i++)
{
target=B-A[i];
if(m.find(target)==m.end() && target!=A[i])
{
m[target]=i;
}
else
continue;
if(m.find(target)==m.end() && target==A[i])
{
m[target]=i;
index1=i;
}
else if(m.find(target)!=m.end() && target==A[i])
{
index2=i;

    }
    if(m.find(A[m[target]])!=m.end() && target!=A[i])
    {
        if(index2==-1&& index1==-1)
        {
            index2=m[target];
            index1=m[A[m[target]]];
        }
        else if(index2>m[target])
        {
            index2=m[target];
            index1=m[A[m[target]]];
        }
        else if(index2==m[target] && index1>m[A[m[target]]])
        {
            index2=m[target];
            index1=m[A[m[target]]];   
        }
    }
}

res.push_back(index1+1);
res.push_back(index2+1);
return res;

}