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 3802720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:15:07+00:00 2026-05-19T14:15:07+00:00

Given a list of coordinates in the first quadrant , calculate how many right

  • 0

Given a list of coordinates in the first quadrant, calculate how many right triangles can be formed from these which have one side parallel to x-axis and one side parallel to y-axis.

Recently I took part in a programming competition, more specifically INOI(Indian National Olympiad n Informatics) and this was the first out of two questions in the paper.

Basically I figured any 3 points of type (a,y) (x,y) (x,b) would form such a triangle but couldn’t manage anything better, and in the end just wrote a naive O(n^3) solution (and so did all my friends).

Can anyone suggest a better way?

And please, THIS IS NOT HOMEWORK.

  • 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-19T14:15:08+00:00Added an answer on May 19, 2026 at 2:15 pm

    Lets numX[i] = how many points have i as their X coordinate and numY[i] = how many points have i as their Y coordinate.

    We will count how many triangles with the required property exist for a certain point p. Without loss of generality, we can assume that p is the point where the triangles make their right angle.

    For this to happen, we need a point with the same Y coordinate and one with the same X coordinate. So how about this algorithm:

    compute numX and numY in O(n).
    num = 0
    for each point p in the given list of points
        num += (numX[p.X] - 1)*(numY[p.Y] - 1)
    
    output num
    

    Basically, we can combine each point with the same X coordinate with each point with the same Y coordinate to get the required triangle. We subtract 1 so as not to count p itself.

    This will run in O(n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of more than 15 thousand latitude and longitude coordinates. Given
Given a list of coordinates, how do I calculate the minimum bounding rectangle (MBR),
Given a List<int> myValues which I know to be ordered, what is the quickest
given a list of module names (e.g. mymods = ['numpy', 'scipy', ...]) how can
The polygon is given as a list of Vector2I objects (2 dimensional, integer coordinates).
I am learning python and panda3d currently. I have a nested list which i
I'am tring to retrieve from my database (using JPA) for a given list of
I have a matrix, i.e., a non-ragged list of lists, and given a list
Given an R list, I wish to find the index of a given list
Given a list like this: num = [1, 2, 3, 4, 5] There are

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.