Can someone please tell me what is wrong in my code?


#1

I have taken two parameters for a line, slope and y-intercept. I took all the pairs of point, found the slope and y intercept of the line from each pair, and inserted these points in a map with key as a pair of slope and y intercept. Also, I took a ‘visited’ map to keep track of the points that I have inserted in our main map.

The code I wrote is giving wrong answer on submission, Can someone please check my code?

bool findint(vector a, int x)
{

for(int i=0;i<a.size();i++)
{
    if(a[i]==x)
    {
        return true;
    }
}

return false;

}

int Solution::maxPoints(vector &a, vector &b) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

map<pair<float,float>, vector<pair<int,int> > > m1;

map<pair<float,float>, vector<int> > visited;

if(a.size()==0 || a.size()==1)
{
    return a.size();
}



for(int i=0;i<a.size()-1;i++)
{
    for(int j=i+1;j<a.size();j++)
    {
        float m;
        
        if(a[j]-a[i]==0)
        {
            m=INT_MAX;
        }
        
        else{
            m=(1.000000*(b[j]-b[i]))/(a[j]-a[i]);
        }
        
        float c;
        
        if(m==INT_MAX)
        {
            c=a[i];
        }
        
        else{
            c=b[i]-(m*a[i]);
        }
        
        
        if(!findint(visited[make_pair(m,c)],i))
        {
            m1[make_pair(m,c)].push_back(make_pair(a[i],b[i]));
            
            visited[make_pair(m,c)].push_back(i);
        }
        
        if(!findint(visited[make_pair(m,c)],j))
        {
            m1[make_pair(m,c)].push_back(make_pair(a[j],b[j]));
            visited[make_pair(m,c)].push_back(j);
        }
        
        
        // if(!findpair(m1[make_pair(m,c)],make_pair(a[i],b[i])))
        // {
        //     m1[make_pair(m,c)].push_back(make_pair(a[i],b[i]));
            
        // }
        
        // if(!findpair(m1[make_pair(m,c)],make_pair(a[j],b[j])))
        // {
        //     m1[make_pair(m,c)].push_back(make_pair(a[j],b[j]));
            
        // }
        
        
    }
}


int ans=0;

for(auto i:m1)
{
    
    int pt=m1[i.first].size();
    ans=max(ans,pt);
}

return ans;

}