📌  相关文章
📜  由给定的N个点组成的直角三角形的计数,这些点的底面或垂直方向平行于X或Y轴

📅  最后修改于: 2021-04-29 15:31:08             🧑  作者: Mango

给定二维平面上N个不同整数点的数组arr [] 。任务是从N个点开始计算直角三角形的数量,以使底角或垂线平行于XY轴

例子:

方法:想法是存储分别具有相同XY坐标的每个坐标的计数。现在遍历每个给定的点,并且由每个坐标(X,Y)形成的直角三角形的计数由下式给出:

步骤如下:

  • 创建两个地图以存储点数,一个用于具有相同的X坐标,另一个用于具有相同的Y坐标
  • 对于x坐标图中和y坐标图中的每个值,请选择该对点作为枢轴元素,并找到该枢轴元素的频率。
  • 对于上述步骤中的每个枢轴元素(例如ivot ),直角的计数由下式给出:
  • 同样,为给定的其他N个点计算的直角三角形
  • 最后,求和所有可能的三角形,将其作为最终答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include 
using namespace std;
  
// Function to find the number of right
// angled triangle that are formed from
// given N points whose perpendicular or
// base is parallel to X or Y axis
int RightAngled(int a[][2], int n)
{
  
    // To store the number of points
    // has same x or y coordinates
    unordered_map xpoints;
    unordered_map ypoints;
  
    for (int i = 0; i < n; i++) {
        xpoints[a[i][0]]++;
        ypoints[a[i][1]]++;
    }
  
    // Store the total count of triangle
    int count = 0;
  
    // Iterate to check for total number
    // of possible triangle
    for (int i = 0; i < n; i++) {
  
        if (xpoints[a[i][0]] >= 1
            && ypoints[a[i][1]] >= 1) {
  
            // Add the count of triangles
            // formed
            count += (xpoints[a[i][0]] - 1)
                     * (ypoints[a[i][1]] - 1);
        }
    }
  
    // Total possible triangle
    return count;
}
  
// Driver Code
int main()
{
    int N = 5;
  
    // Given N points
    int arr[][2] = { { 1, 2 }, { 2, 1 },
                     { 2, 2 }, { 2, 3 },
                     { 3, 2 } };
  
    // Function Call
    cout << RightAngled(arr, N);
  
    return 0;
}


Python3
# Python3 program for the above approach 
from collections import defaultdict
  
# Function to find the number of right 
# angled triangle that are formed from 
# given N points whose perpendicular or 
# base is parallel to X or Y axis 
def RightAngled(a, n):
      
    # To store the number of points 
    # has same x or y coordinates 
    xpoints = defaultdict(lambda:0)
    ypoints = defaultdict(lambda:0)
      
    for i in range(n):
        xpoints[a[i][0]] += 1
        ypoints[a[i][1]] += 1
          
    # Store the total count of triangle 
    count = 0
      
    # Iterate to check for total number 
    # of possible triangle 
    for i in range(n):
        if (xpoints[a[i][0]] >= 1 and 
            ypoints[a[i][1]] >= 1):
              
            # Add the count of triangles 
            # formed 
            count += ((xpoints[a[i][0]] - 1) * 
                      (ypoints[a[i][1]] - 1))
              
    # Total possible triangle
    return count
  
# Driver Code 
N = 5
  
# Given N points 
arr = [ [ 1, 2 ], [ 2, 1 ],
        [ 2, 2 ], [ 2, 3 ],
        [ 3, 2 ] ]
  
# Function call
print(RightAngled(arr, N))
  
# This code is contributed by Stuti Pathak


Java
// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to find the number of right
// angled triangle that are formed from
// given N points whose perpendicular or
// base is parallel to X or Y axis
static int RightAngled(int a[][], int n)
{
  
    // To store the number of points
    // has same x or y coordinates
    HashMap xpoints  = new HashMap();
    HashMap ypoints  = new HashMap();
  
    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);
        }
    }
  
    // Store the total count of triangle
    int count = 0;
  
    // Iterate to check for total number
    // of possible triangle
    for (int i = 0; i < n; i++)
    {
        if (xpoints.get(a[i][0]) >= 1 &&
            ypoints.get(a[i][1]) >= 1)
        {
  
            // Add the count of triangles
            // formed
            count += (xpoints.get(a[i][0]) - 1) *
                     (ypoints.get(a[i][1]) - 1);
        }
    }
  
    // Total possible triangle
    return count;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 5;
  
    // Given N points
    int arr[][] = { { 1, 2 }, { 2, 1 },
                    { 2, 2 }, { 2, 3 },
                    { 3, 2 } };
  
    // Function Call
    System.out.print(RightAngled(arr, N));
}
}
  
// This code is contributed by Rajput-Ji


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
  
    // Function to find the number of right
    // angled triangle that are formed from
    // given N points whose perpendicular or
    // base is parallel to X or Y axis
    static int RightAngled(int[, ] a, int n)
    {
  
        // To store the number of points
        // has same x or y coordinates
        Dictionary xpoints = new Dictionary();
        Dictionary ypoints = new Dictionary();
  
        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);
            }
        }
  
        // Store the total count of triangle
        int count = 0;
  
        // Iterate to check for total number
        // of possible triangle
        for (int i = 0; i < n; i++) 
        {
            if (xpoints[a[i, 0]] >= 1 && 
                ypoints[a[i, 1]] >= 1) 
            {
  
                // Add the count of triangles
                // formed
                count += (xpoints[a[i, 0]] - 1) * 
                         (ypoints[a[i, 1]] - 1);
            }
        }
  
        // Total possible triangle
        return count;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 5;
  
        // Given N points
        int[, ] arr = {{1, 2}, {2, 1}, 
                       {2, 2}, {2, 3}, {3, 2}};
  
        // Function Call
        Console.Write(RightAngled(arr, N));
    }
}
  
// This code is contributed by Rajput-Ji


输出:
4

时间复杂度: O(N)
辅助空间: O(1)