Count of Right-Angled Triangle formed from given N points whose base or perpendicular are parallel to X or Y axis
Given an array arr[] of N distinct integers points on the 2D Plane. The task is to count the number of Right-Angled Triangle from N points such that the base or perpendicular is parallel to the X or Y-axis.
Examples:
Input: arr[][] = {{4, 2}, {2, 1}, {1, 3}}
Output: 0
Explanation:
In the above image there is no right-angled triangle formed.
Input: arr[][] = {{1, 2}, {2, 1}, {2, 2}, {2, 3}, {3, 2}}
Output: 4
Explanation:
In the above image there are 4 right-angled triangles formed by triangles ACB, ACD, DCE, BCE.
Approach: The idea is to store the count of each co-ordinate’s having the same X and Y co-ordinates respectively. Now traverse each given points and the count of a right-angled triangle formed by each coordinate (X, Y) is given by:
Count of right-angled triangles = (frequencies of X coordinates – 1) * (frequencies of Y coordinates – 1)
Below are the steps:
- Create two maps to store the count of points, one for having the same X-coordinate and another for having the same Y-coordinate.
- For each value in the map of x-coordinate and in the map of y-coordinate choose that pair of points as pivot elements and find the frequency of that pivot element.
- For each pivot element(say pivot) in the above step, the count of right-angled is given by:
(m1[pivot].second-1)*(m2[pivot].second-1)
- Similarly, calculate the total possible right-angled triangle for other N points given.
- Finally, sum all the possible triangle obtained that is the final answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int RightAngled( int a[][2], int n)
{
unordered_map< int , int > xpoints;
unordered_map< int , int > ypoints;
for ( int i = 0; i < n; i++) {
xpoints[a[i][0]]++;
ypoints[a[i][1]]++;
}
int count = 0;
for ( int i = 0; i < n; i++) {
if (xpoints[a[i][0]] >= 1
&& ypoints[a[i][1]] >= 1) {
count += (xpoints[a[i][0]] - 1)
* (ypoints[a[i][1]] - 1);
}
}
return count;
}
int main()
{
int N = 5;
int arr[][2] = { { 1, 2 }, { 2, 1 },
{ 2, 2 }, { 2, 3 },
{ 3, 2 } };
cout << RightAngled(arr, N);
return 0;
}
|
Python3
from collections import defaultdict
def RightAngled(a, n):
xpoints = defaultdict( lambda : 0 )
ypoints = defaultdict( lambda : 0 )
for i in range (n):
xpoints[a[i][ 0 ]] + = 1
ypoints[a[i][ 1 ]] + = 1
count = 0
for i in range (n):
if (xpoints[a[i][ 0 ]] > = 1 and
ypoints[a[i][ 1 ]] > = 1 ):
count + = ((xpoints[a[i][ 0 ]] - 1 ) *
(ypoints[a[i][ 1 ]] - 1 ))
return count
N = 5
arr = [ [ 1 , 2 ], [ 2 , 1 ],
[ 2 , 2 ], [ 2 , 3 ],
[ 3 , 2 ] ]
print (RightAngled(arr, N))
|
Java
import java.util.*;
class GFG{
static int RightAngled( int a[][], int n)
{
HashMap<Integer,
Integer> xpoints = new HashMap<Integer,
Integer>();
HashMap<Integer,
Integer> ypoints = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++)
{
if (xpoints.containsKey(a[i][ 0 ]))
{
xpoints.put(a[i][ 0 ], xpoints.get(a[i][ 0 ]) + 1 );
}
else
{
xpoints.put(a[i][ 0 ], 1 );
}
if (ypoints.containsKey(a[i][ 1 ]))
{
ypoints.put(a[i][ 1 ], ypoints.get(a[i][ 1 ]) + 1 );
}
else
{
ypoints.put(a[i][ 1 ], 1 );
}
}
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (xpoints.get(a[i][ 0 ]) >= 1 &&
ypoints.get(a[i][ 1 ]) >= 1 )
{
count += (xpoints.get(a[i][ 0 ]) - 1 ) *
(ypoints.get(a[i][ 1 ]) - 1 );
}
}
return count;
}
public static void main(String[] args)
{
int N = 5 ;
int arr[][] = { { 1 , 2 }, { 2 , 1 },
{ 2 , 2 }, { 2 , 3 },
{ 3 , 2 } };
System.out.print(RightAngled(arr, N));
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int RightAngled( int [, ] a, int n)
{
Dictionary< int , int > xpoints = new Dictionary< int , int >();
Dictionary< int , int > ypoints = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (xpoints.ContainsKey(a[i, 0]))
{
xpoints[a[i, 0]] = xpoints[a[i, 0]] + 1;
}
else
{
xpoints.Add(a[i, 0], 1);
}
if (ypoints.ContainsKey(a[i, 1]))
{
ypoints[a[i, 1]] = ypoints[a[i, 1]] + 1;
}
else
{
ypoints.Add(a[i, 1], 1);
}
}
int count = 0;
for ( int i = 0; i < n; i++)
{
if (xpoints[a[i, 0]] >= 1 &&
ypoints[a[i, 1]] >= 1)
{
count += (xpoints[a[i, 0]] - 1) *
(ypoints[a[i, 1]] - 1);
}
}
return count;
}
public static void Main(String[] args)
{
int N = 5;
int [, ] arr = {{1, 2}, {2, 1},
{2, 2}, {2, 3}, {3, 2}};
Console.Write(RightAngled(arr, N));
}
}
|
Javascript
<script>
function RightAngled(a, n) {
var xpoints = {};
var ypoints = {};
for ( var i = 0; i < n; i++) {
if (xpoints.hasOwnProperty(a[i][0])) {
xpoints[a[i][0]] = xpoints[a[i][0]] + 1;
} else {
xpoints[a[i][0]] = 1;
}
if (ypoints.hasOwnProperty(a[i][1])) {
ypoints[a[i][1]] = ypoints[a[i][1]] + 1;
} else {
ypoints[a[i][1]] = 1;
}
}
var count = 0;
for ( var i = 0; i < n; i++) {
if (xpoints[a[i][0]] >= 1 && ypoints[a[i][1]] >= 1) {
count += (xpoints[a[i][0]] - 1) * (ypoints[a[i][1]] - 1);
}
}
return count;
}
var N = 5;
var arr = [
[1, 2],
[2, 1],
[2, 2],
[2, 3],
[3, 2],
];
document.write(RightAngled(arr, N));
</script>
|
Time Complexity: O(N+N) i.e O(N)
Auxiliary Space: O(N+N) i.e O(N)
Last Updated :
11 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...