📜  满足点方程的有序点数

📅  最后修改于: 2021-05-06 19:49:37             🧑  作者: Mango

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

例子 :

方法1(蛮力):
生成所有可能的对(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


输出 :
5

时间复杂度: O(n 2 )

方法2(高效):
给定一个点的轴坐标,对于每个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 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)

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”