📌  相关文章
📜  由给定的 N 个点形成的直角三角形的数量,这些点的底边或垂线平行于 X 或 Y 轴

📅  最后修改于: 2021-10-26 06:03:06             🧑  作者: Mango

给定一个包含N 个不同整数的数组arr[]点在2D平面上。任务是从N个点计算直角三角形的数量,使得底面或垂线平行于XY 轴

例子:

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

以下是步骤:

  • 创建两个地图来存储点数,一个用于具有相同的X 坐标,另一个用于具有相同的Y 坐标
  • 对于x 坐标映射和y 坐标映射中的每个值,选择那对点作为枢轴元素并找到该枢轴元素的频率。
  • 对于上述步骤中的每个枢轴元素(例如pivot ),直角的计数由下式给出:
  • 类似地,计算给定的其他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


Javascript


输出:
4

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程