📜  满足直线方程的有序点对数

📅  最后修改于: 2021-10-27 08:02:38             🧑  作者: Mango

给定一个由 n 个整数组成的数组,直线的斜率,即 m 和直线的截距,即 c,计算其中 i ≠ j 的点的有序对 (i, j) 的数量,使得点 (A i , A j ) 满足以给定斜率和截距形成的线。
:直线方程为 y = mx + c,其中 m 为直线斜率,c 为截距。
例子 :

方法一(蛮力):
生成所有可能的对 (i, j) 并检查特定的有序对 (i, j) 是否满足 (arr i , arr j ) 满足 y = mx + c 行的给定方程,并且 i ≠ j。如果点有效(如果满足上述条件,则点有效),增加存储有效点总数的计数器。

C++
// CPP code to count the number of ordered
// pairs satisfying Line Equation
#include 
 
using namespace std;
 
/* Checks if (i, j) is valid, a point (i, j)
   is valid if point (arr[i], arr[j])
   satisfies the equation y = mx + c And
   i is not equal to j*/
bool isValid(int arr[], int i, int j,
             int m, int c)
{
 
    // check if i equals to j
    if (i == j)
        return false;
     
     
    // Equation LHS = y, and RHS = mx + c
    int lhs = arr[j];   
    int rhs = m * arr[i] + c;
 
    return (lhs == rhs);
}
 
/* Returns the number of ordered pairs
   (i, j) for which point (arr[i], arr[j])
   satisfies the equation of the line
   y = mx + c */
int findOrderedPoints(int arr[], int n,
                      int m, int c)
{
 
    int counter = 0;
 
    // for every possible (i, j) check
    // if (a[i], a[j]) satisfies the
    // equation y = mx + c
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            // (firstIndex, secondIndex)
            // is same as (i, j)
            int firstIndex = i, secondIndex = j;
 
            // check if (firstIndex,
            // secondIndex) is a valid point
            if (isValid(arr, firstIndex, secondIndex, m, c))
                counter++;
        }
    }
    return counter;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // equation of line is y = mx + c
    int m = 1, c = 1;
    cout << findOrderedPoints(arr, n, m, c);
    return 0;
}


Java
// Java code to find number of ordered
// points satisfying line equation
import java.io.*;
 
public class GFG {
     
    // Checks if (i, j) is valid,
    // a point (i, j) is valid if
    // point (arr[i], arr[j])
    // satisfies the equation
    // y = mx + c And
    // i is not equal to j
    static boolean isValid(int []arr, int i,
                        int j, int m, int c)
    {
     
        // check if i equals to j
        if (i == j)
            return false;
         
         
        // Equation LHS = y,
        // and RHS = mx + c
        int lhs = arr[j];
        int rhs = m * arr[i] + c;
     
        return (lhs == rhs);
    }
     
    /* Returns the number of ordered pairs
    (i, j) for which point (arr[i], arr[j])
    satisfies the equation of the line
    y = mx + c */
    static int findOrderedPoints(int []arr,
                       int n, int m, int c)
    {
     
        int counter = 0;
     
        // for every possible (i, j) check
        // if (a[i], a[j]) satisfies the
        // equation y = mx + c
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                 
                // (firstIndex, secondIndex)
                // is same as (i, j)
                int firstIndex = i,
                   secondIndex = j;
     
                // check if (firstIndex,
                // secondIndex) is a
                // valid point
                if (isValid(arr, firstIndex,
                         secondIndex, m, c))
                    counter++;
            }
        }
        return counter;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int []arr = { 1, 2, 3, 4, 2 };
        int n = arr.length;
     
        // equation of line is y = mx + c
        int m = 1, c = 1;
        System.out.print(
           findOrderedPoints(arr, n, m, c));
    }
}
 
// This code is contributed by
// Manish Shaw (manishshaw1)


Python3
# Python code to count the number of ordered
# pairs satisfying Line Equation
 
# Checks if (i, j) is valid, a point (i, j)
# is valid if point (arr[i], arr[j])
# satisfies the equation y = mx + c And
# i is not equal to j
def isValid(arr, i, j, m, c) :
 
    # check if i equals to j
    if (i == j) :
        return False
     
     
    # Equation LHS = y, and RHS = mx + c
    lhs = arr[j];
    rhs = m * arr[i] + c
 
    return (lhs == rhs)
 
# Returns the number of ordered pairs
# (i, j) for which point (arr[i], arr[j])
# satisfies the equation of the line
# y = mx + c */
def findOrderedPoints(arr, n, m, c) :
 
    counter = 0
 
    # for every possible (i, j) check
    # if (a[i], a[j]) satisfies the
    # equation y = mx + c
    for i in range(0, n) :
        for j in range(0, n) :
            # (firstIndex, secondIndex)
            # is same as (i, j)
            firstIndex = i
            secondIndex = j
 
            # check if (firstIndex,
            # secondIndex) is a valid point
            if (isValid(arr, firstIndex,
                      secondIndex, m, c)) :
                counter = counter + 1
 
    return counter
 
# Driver Code
arr = [ 1, 2, 3, 4, 2 ]
n = len(arr)
 
# equation of line is y = mx + c
m = 1
c = 1
print (findOrderedPoints(arr, n, m, c))
 
# This code is contributed by Manish Shaw
# (manishshaw1)


C#
// C# code to find number of ordered
// points satisfying line equation
using System;
class GFG {
     
    // Checks if (i, j) is valid,
    // a point (i, j) is valid if
    // point (arr[i], arr[j])
    // satisfies the equation
    // y = mx + c And
    // i is not equal to j
    static bool isValid(int []arr, int i,
                     int j, int m, int c)
    {
     
        // check if i equals to j
        if (i == j)
            return false;
         
         
        // Equation LHS = y,
        // and RHS = mx + c
        int lhs = arr[j];
        int rhs = m * arr[i] + c;
     
        return (lhs == rhs);
    }
     
    /* Returns the number of ordered pairs
      (i, j) for which point (arr[i], arr[j])
      satisfies the equation of the line
       y = mx + c */
    static int findOrderedPoints(int []arr, int n,
                                     int m, int c)
    {
     
        int counter = 0;
     
        // for every possible (i, j) check
        // if (a[i], a[j]) satisfies the
        // equation y = mx + c
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                 
                // (firstIndex, secondIndex)
                // is same as (i, j)
                int firstIndex = i, secondIndex = j;
     
                // check if (firstIndex,
                // secondIndex) is a valid point
                if (isValid(arr, firstIndex, secondIndex, m, c))
                    counter++;
            }
        }
        return counter;
    }
     
    // Driver Code
    public static void Main()
    {
        int []arr = { 1, 2, 3, 4, 2 };
        int n = arr.Length;
     
        // equation of line is y = mx + c
        int m = 1, c = 1;
        Console.Write(findOrderedPoints(arr, n, m, c));
    }
}
 
// This code is contributed by
// Manish Shaw (manishshaw1)


PHP


Javascript


CPP
// CPP code to find number of ordered
// points satisfying line equation
#include 
using namespace std;
 
/* Returns the number of ordered pairs
   (i, j) for which point (arr[i], arr[j])
   satisfies the equation of the line
   y = mx + c */
int findOrderedPoints(int arr[], int n,
                      int m, int c)
{
    int counter = 0;
 
    // map stores the frequency
    // of arr[i] for all i
    unordered_map frequency;
 
    for (int i = 0; i < n; i++)
        frequency[arr[i]]++;
 
    for (int i = 0; i < n; i++)
    {
        int xCoordinate = arr[i];
        int yCoordinate = (m * arr[i] + c);
 
        // if for a[i](xCoordinate),
        // a yCoordinate exists in the map
        // add the frequency of yCoordinate
        // to the counter
 
        if (frequency.find(yCoordinate) !=
            frequency.end())
            counter += frequency[yCoordinate];
 
        // check if xCoordinate = yCoordinate,
        // if this is true then since we only
        // want (i, j) such that i != j, decrement
        // the counter by one to avoid points
        // of type (arr[i], arr[i])
        if (xCoordinate == yCoordinate)
            counter--;
    }
    return counter;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 1, c = 1;
    cout << findOrderedPoints(arr, n, m, c);
    return 0;
}


输出 :

5

时间复杂度: O(n 2 )
方法二(高效):
给定一个点的 ax 坐标,对于每个 x 都有一个唯一的 y 值,而 y 的值就是 m * x + c。因此,对于数组 arr 的每个可能的 x 坐标,计算满足直线方程的 y 的唯一值在该数组中出现的次数。在地图中存储数组 arr 的所有整数的计数。现在,对于每个值 arr i ,将 m * arr i + c 的出现次数添加到答案中。对于给定的 i,m * a[i] + c 在数组中出现 x 次,然后,将 x 添加到我们的计数器以获得总有效点数,但需要检查是否 a[i] = m * a[i] + c 那么,很明显,因为这在数组中出现了 x 次,那么一次出现在第 i索引处,其余 (x – 1) 次出现是有效的 y 坐标,因此将 (x – 1) 添加到我们的点计数器中。

CPP

// CPP code to find number of ordered
// points satisfying line equation
#include 
using namespace std;
 
/* Returns the number of ordered pairs
   (i, j) for which point (arr[i], arr[j])
   satisfies the equation of the line
   y = mx + c */
int findOrderedPoints(int arr[], int n,
                      int m, int c)
{
    int counter = 0;
 
    // map stores the frequency
    // of arr[i] for all i
    unordered_map frequency;
 
    for (int i = 0; i < n; i++)
        frequency[arr[i]]++;
 
    for (int i = 0; i < n; i++)
    {
        int xCoordinate = arr[i];
        int yCoordinate = (m * arr[i] + c);
 
        // if for a[i](xCoordinate),
        // a yCoordinate exists in the map
        // add the frequency of yCoordinate
        // to the counter
 
        if (frequency.find(yCoordinate) !=
            frequency.end())
            counter += frequency[yCoordinate];
 
        // check if xCoordinate = yCoordinate,
        // if this is true then since we only
        // want (i, j) such that i != j, decrement
        // the counter by one to avoid points
        // of type (arr[i], arr[i])
        if (xCoordinate == yCoordinate)
            counter--;
    }
    return counter;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 1, c = 1;
    cout << findOrderedPoints(arr, n, m, c);
    return 0;
}
输出:
5

时间复杂度: O(n)

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