📌  相关文章
📜  从分别平行于 X 和 Y 轴的 M 和 N 条直线上计算可能的正方形

📅  最后修改于: 2021-09-03 04:01:01             🧑  作者: Mango

给定两个数组X[]Y[]NM 个整数组成,这样有N条线平行于y 轴M条线平行于x 轴。任务是在坐标平面上找到这些线形成的正方形总数。

例子:

处理方法:按照以下步骤解决问题:

  • 查找X[]数组中所有对之间的距离并将计数存储在 Map 中,例如M1
  • 找到Y[]数组中所有对之间的距离,并将计数存储在 Map M2 中
  • 如果M1对的距离存在于M2 中,则可以通过使用这两个对来制作正方形。
  • 因此,可以通过将存储在M1M2 中的所有距离计数相加来计算正方形的总计数。
  • 完成上述步骤后,打印正方形的总数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count all the possible
// squares with given lines parallel
// to both the X and Y axis
int numberOfSquares(int X[], int Y[],
                    int N, int M)
{
    // Stores the count of all possible
    // distances in X[] & Y[] respectively
    unordered_map m1, m2;
    int i, j, ans = 0;
 
    // Find distance between all
    // pairs in the array X[]
    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N; j++) {
 
            int dist = abs(X[i] - X[j]);
 
            // Add the count to m1
            m1[dist]++;
        }
    }
 
    // Find distance between all
    // pairs in the array Y[]
    for (i = 0; i < M; i++) {
        for (j = i + 1; j < M; j++) {
 
            int dist = abs(Y[i] - Y[j]);
 
            // Add the count to m2
            m2[dist]++;
        }
    }
 
    // Find sum of m1[i] * m2[i]
    // for same distance
    for (auto i = m1.begin();
         i != m1.end(); i++) {
 
        // Find current count in m2
        if (m2.find(i->first)
            != m2.end()) {
 
            // Add to the total count
            ans += (i->second
                    * m2[i->first]);
        }
    }
 
    // Return the final count
    return ans;
}
 
// Driver Code
int main()
{
    // Given lines
    int X[] = { 1, 3, 7 };
    int Y[] = { 2, 4, 6, 1 };
 
    int N = sizeof(X) / sizeof(X[0]);
 
    int M = sizeof(Y) / sizeof(Y[0]);
 
    // Function Call
    cout << numberOfSquares(X, Y, N, M);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to count all the possible
// squares with given lines parallel
// to both the X and Y axis
static int numberOfSquares(int[] X, int[] Y, int N,
                           int M)
{
     
    // Stores the count of all possible
    // distances in X[] & Y[] respectively
    HashMap m1 = new HashMap();
    HashMap m2 = new HashMap();
 
    int i, j, ans = 0;
 
    // Find distance between all
    // pairs in the array X[]
    for(i = 0; i < N; i++)
    {
        for(j = i + 1; j < N; j++)
        {
            int dist = Math.abs(X[i] - X[j]);
 
            // Add the count to m1
            m1.put(dist, m1.getOrDefault(dist, 0) + 1);
        }
    }
 
    // Find distance between all
    // pairs in the array Y[]
    for(i = 0; i < M; i++)
    {
        for(j = i + 1; j < M; j++)
        {
            int dist = Math.abs(Y[i] - Y[j]);
 
            // Add the count to m2
            m2.put(dist, m2.getOrDefault(dist, 0) + 1);
        }
    }
 
    // Find sum of m1[i] * m2[i]
    // for same distance
    for(Map.Entry entry : m1.entrySet())
    {
         
        // Find current count in m2
        if (m2.containsKey(entry.getKey()))
        {
             
            // Add to the total count
            ans += (entry.getValue() *
             m2.get(entry.getKey()));
        }
    }
 
    // Return the final count
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given lines
    int X[] = { 1, 3, 7 };
    int Y[] = { 2, 4, 6, 1 };
 
    int N = X.length;
 
    int M = Y.length;
 
    // Function call
    System.out.println(numberOfSquares(X, Y, N, M));
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
 
# Function to count all the possible
# squares with given lines parallel
# to both the X and Y axis
def numberOfSquares(X, Y, N, M):
 
    # Stores the count of all possible
    # distances in X[] & Y[] respectively
    m1 = {}
    m2 = {}
    ans = 0
 
    # Find distance between all
    # pairs in the array X[]
    for i in range(0, N):
        for j in range(i + 1, N):
            dist = abs(X[i] - X[j])
 
            # Add the count to m1
            if dist in m1:
                m1[dist] = m1[dist] + 1
            else:
                m1[dist] = 1
 
    # Find distance between all
    # pairs in the array Y[]
    for i in range(0, M):
        for j in range(i + 1, M):
            dist = abs(Y[i] - Y[j])
 
            # Add the count to m2
            if dist in m2:
                m2[dist] = m2[dist] + 1
            else:
                m2[dist] = 1
 
    # Find sum of m1[i] * m2[i]
    # for same distance
    for key in m1:
         
        # Find current count in m2
        if key in m2:
             
            # Add to the total count
            ans = ans + (m1[key] * m2[key])
 
    # Return the final count
    return ans
 
# Driver Code
if __name__ == "__main__":
     
    # Given lines
    X = [ 1, 3, 7 ]
    Y = [ 2, 4, 6, 1 ]
 
    N = len(X)
 
    M = len(Y)
 
    # Function call
    print(numberOfSquares(X, Y, N, M))
 
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count all the possible
// squares with given lines parallel
// to both the X and Y axis
static int numberOfSquares(int[] X, int[] Y, int N,
                           int M)
{
     
    // Stores the count of all possible
    // distances in X[] & Y[] respectively
    Dictionary m1 = new Dictionary();
      Dictionary m2 = new Dictionary();
 
    int i, j, ans = 0;
 
    // Find distance between all
    // pairs in the array X[]
    for(i = 0; i < N; i++)
    {
        for(j = i + 1; j < N; j++)
        {
            int dist = Math.Abs(X[i] - X[j]);
 
            // Add the count to m1
              if (m1.ContainsKey(dist))
                m1[dist]++;
              else
                m1.Add(dist, 1);
        }
    }
 
    // Find distance between all
    // pairs in the array Y[]
    for(i = 0; i < M; i++)
    {
        for(j = i + 1; j < M; j++)
        {
            int dist = Math.Abs(Y[i] - Y[j]);
 
            // Add the count to m2
            if (m2.ContainsKey(dist))
                m2[dist]++;
              else
                m2.Add(dist, 1);
        }
    }
 
    // Find sum of m1[i] * m2[i]
    // for same distance
    foreach(KeyValuePair entry in m1)
    {
         
        // Find current count in m2
        if (m2.ContainsKey(entry.Key))
        {
             
            // Add to the total count
            ans += (entry.Value *
                 m2[entry.Key]);
        }
    }
 
    // Return the final count
    return ans;
}
 
// Driver Code
public static void Main()
{
     
    // Given lines
    int[] X = { 1, 3, 7 };
    int[] Y = { 2, 4, 6, 1 };
 
    int N = X.Length;
 
    int M = Y.Length;
 
    // Function call
    Console.WriteLine(numberOfSquares(X, Y, N, M));
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live