给定一个整数K ,以及两个由N 个整数组成的数组X[]和Y[] ,其中(X[i], Y[i])是平面中的坐标,任务是找到对的总数点数 这样穿过它们的线的斜率在[-K, K]范围内。
例子:
Input: X[] = {2, 1, 0}, Y[] = {1, 2, 0}, K = 1
Output: 2
Explanation:
The set of pairs satisfying the given condition are [(0, 0), (2, 1)] and [(1, 2), (2, 1)].
Input: X[] = {2, 4}, Y[][] = {5, 6}, K = 1
Output: 1
方法:想法是遍历所有点对并检查它们的斜率是否在[-K,K]范围内。请按照以下步骤解决问题:
- 初始化一个变量,将ans设为0以存储对的结果计数。
- 现在,生成所有可能的坐标对,如果 2 个点(X[i], Y[i])和(X[j], Y[j])的斜率在[-K, K]范围内,然后将ans增加1 。
- 完成上述步骤后,打印 和 的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of pairs
// of points such that the line passing
// through them has a slope in the range[-k, k]
void findPairs(vector x, vector y,
int K)
{
int n = x.size();
// Store the result
int ans = 0;
// Traverse through all the
// combination of points
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
// If pair satisfies
// the given condition
if (K * abs(x[i] - x[j])
>= abs(y[i] - y[j])) {
// Increment ans by 1
++ans;
}
}
}
// Print the result
cout << ans;
}
// Driver Code
int main()
{
vector X = { 2, 1, 0 },
Y = { 1, 2, 0 };
int K = 1;
// Function Call
findPairs(X, Y, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the number of pairs
// of points such that the line passing
// through them has a slope in the range[-k, k]
static void findPairs(int[] x, int[] y,
int K)
{
int n = x.length;
// Store the result
int ans = 0;
// Traverse through all the
// combination of points
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
// If pair satisfies
// the given condition
if (K * Math.abs(x[i] - x[j])
>= Math.abs(y[i] - y[j])) {
// Increment ans by 1
++ans;
}
}
}
// Print the result
System.out.print(ans);
}
// Driven Code
public static void main(String[] args)
{
int[] X = { 2, 1, 0 };
int[] Y = { 1, 2, 0 };
int K = 1;
// Function Call
findPairs(X, Y, K);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python3 program for the above approach
# Function to find the number of pairs
# of points such that the line passing
# through them has a slope in the range[-k, k]
def findPairs(x, y, K):
n = len(x)
# Store the result
ans = 0
# Traverse through all the
# combination of points
for i in range(n):
for j in range(i + 1, n):
# If pair satisfies
# the given condition
if (K * abs(x[i] - x[j]) >= abs(y[i] - y[j])):
# Increment ans by 1
ans += 1
# Print the result
print (ans)
# Driver Code
if __name__ == '__main__':
X = [2, 1, 0]
Y = [1, 2, 0]
K = 1
# Function Call
findPairs(X, Y, K)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the number of pairs
// of points such that the line passing
// through them has a slope in the range[-k, k]
static void findPairs(int[] x, int[] y,
int K)
{
int n = x.Length;
// Store the result
int ans = 0;
// Traverse through all the
// combination of points
for(int i = 0; i < n; ++i)
{
for(int j = i + 1; j < n; ++j)
{
// If pair satisfies
// the given condition
if (K * Math.Abs(x[i] - x[j]) >=
Math.Abs(y[i] - y[j]))
{
// Increment ans by 1
++ans;
}
}
}
// Print the result
Console.WriteLine(ans);
}
// Driver Code
public static void Main(String []args)
{
int[] X = { 2, 1, 0 };
int[] Y = { 1, 2, 0 };
int K = 1;
// Function Call
findPairs(X, Y, K);
}
}
// This code is contributed by souravghosh0416
Javascript
输出:
2
时间复杂度: O(N 2 )
辅助空间: O(1)