Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6361727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:48:53+00:00 2026-05-24T23:48:53+00:00

I was asked this Interview Question (C++,algos)and had no idea how to solve it.

  • 0

I was asked this Interview Question (C++,algos)and had no idea how to solve it.

Given an array say Arr[N] containing Cartesian coordinates of N distinct points count the number of triples (Arr[P], Arr[Q], Arr[R]) such that P < Q < R < N and the points Arr[P], Arr[Q], Arr[R] are collinear (i.e lie on the same straight line).

Any ideas? What algorithm can I use for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:48:54+00:00Added an answer on May 24, 2026 at 11:48 pm

    The following is probably not optimized, but its complexity is the one your interviewer requested.

    First create a list of (a,b,c) values for each couple of points (N² complexity)
    –> (a,b,c) stands for the cartesian equation of a straight line a*x+b*y+c=0
    Given two points and their coordinates (xa, ya) and (xb, yb), computing (a,b,c) is simple.
    Either you can find a solution to

    ya=alpha*xa+beta  
    yb=alpha*xb+beta
    
    (if (xb-xa) != 0)
    alpha = (yb-ya)/(xb-xa)
    beta = ya - alpha*xa
    a = alpha
    b = -1
    c = beta
    

    or to

    xa = gamma*ya+delta
    xb = gamma*yb+delta
    (you get the point)
    

    The solvable set of equations can then be rewritten in the more general form

    a*x+b*y+c = 0
    

    Then sort the list (N² log(N²) complexity therefore N²log(N) complexity).

    Iterate over elements of the list. If two sequential elements are equal, corresponding points are collinear. N² complexity.

    You might want to add a last operation to filter duplicate results, but you should be fine, complexity-wise.

    EDIT : i updated a bit the algorithm while coding it to make it more simple and optimal. Here it goes.

    #include <map>
    #include <set>
    #include <vector>
    #include <iostream>
    
    struct StraightLine
    {
        double a,b,c;
        StraightLine() : a(0.),b(0.),c(0.){}
        bool isValid() { return a!=0. || b!= 0.; }
        bool operator<(StraightLine const& other) const
        {
            if( a < other.a ) return true;
            if( a > other.a ) return false;
            if( b < other.b ) return true;
            if( b > other.b ) return false;
            if( c < other.c ) return true;
            return false;
        }
    };
    
    struct Point { 
        double x, y; 
        Point() : x(0.), y(0.){}
        Point(double p_x, double p_y) : x(p_x), y(p_y){}
    };
    
    StraightLine computeLine(Point const& p1, Point const& p2)
    {
        StraightLine line;
        if( p2.x-p1.x != 0.)
        {
            line.b = -1;
            line.a = (p2.y - p1.y)/(p2.x - p1.x);
        }
        else if( p2.y - p1.y != 0. )
        {
            line.a = -1;
            line.b = (p2.x-p1.x)/(p2.y-p1.y);
        }
        line.c = - line.a * p1.x - line.b * p1.y;
        return line;
    }
    
    int main()
    {
        std::vector<Point> points(9);
        for( int i = 0 ; i < 3 ; ++i )
        {
            for( int j = 0; j < 3 ; ++j )
            {
                points[i*3+j] = Point((double)i, (double)j);
            }
        }
    
    
        size_t nbPoints = points.size();
        typedef std::set<size_t> CollinearPoints;
        typedef std::map<StraightLine, CollinearPoints> Result;
        Result result;
    
        for( int i = 0 ; i < nbPoints ; ++i )
        {
            for( int j = i + 1 ; j < nbPoints ; ++j )
            {
                StraightLine line = computeLine(points[i], points[j]);
                if( line.isValid() )
                {
                    result[line].insert(i);
                    result[line].insert(j);
                }
            }
        }
    
        for( Result::iterator currentLine = result.begin() ; currentLine != result.end(); ++currentLine )
        {
            if( currentLine->second.size() <= 2 )
            {
                continue;
            }
            std::cout << "Line";
            for( CollinearPoints::iterator currentPoint = currentLine->second.begin() ; currentPoint != currentLine->second.end() ; ++currentPoint )
            {
                std::cout << " ( " << points[*currentPoint].x << ", " << points[*currentPoint].y << ")";
            }
            std::cout << std::endl;
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asked this question in a job interview. The interviewer and I disagreed
I was asked this question during an interview. They're both O(nlogn) and yet most
I was recently asked this question in interview How many photographs are taken in
I have a question. I was once asked this in the interview -- Suppose
I was asked this question in an interview, but I couldn't come up with
I was asked this question in an interview and I gave various solutions but
i was asked this question in my interview that how to make a class
I was recently asked this question in an interview: There are two arrays of
I went to an interview today and was asked this question: Suppose you have
Recently I was asked this during a job interview. I was honest and said

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.