Fastest Solution is wrong for square


#1

The fastest solution is given as
int Solution::solve(int A, int B, int C, int D) {
map<int,int> m;
m[A]++;
m[B]++;
m[C]++;
m[D]++;

if(m.size()==2)
{
    for(auto it: m)
    {
        if(it.second!=2){return 0;}
    }
    return 1;
}
else
{
    return 0;
}

}

Which return “0” on sqaure input like for example “4 4 4 4” which must return “1”.
This is because problem solver is only checking if map siize is 2 while in case of sqaure i can be also 1.

My approach to solve this problem was to neglect the wrong outcomes, which shrinnks the problem, checkout:-
int Solution::solve(int A, int B, int C, int D) {
map<int,int> m;
m[A]++;
m[B]++;
m[C]++;
m[D]++;

if(m.size()==2)
{
    for(auto it: m)
    {
        if(it.second!=2){return 0;}
    }
    return 1;
}
else
{
    return 0;
}

}


#2

int Solution::solve(int A, int B, int C, int D) {
map<int ,int >m;
m[A]++;
m[B]++;
m[C]++;
m[D]++;
for(auto i : m){
if(i.second%2!=0){
return 0;
}
}
return 1;
}


#3

yes it is wrong.They didn’t even include a testcase for square


#4

A rectangle is a square when both pairs of opposite sides are the same length. This means that a square is a specialized case of the rectangle and is indeed a rectangle .