Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Count pairs of coordinates connected by a line with slope in the range [-K, K]

  • Last Updated : 24 Mar, 2021

Given an integer K, and two arrays X[] and Y[] both consisting of N integers, where (X[i], Y[i]) is a coordinate in a plane, the task is to find the total number of pairs of points such that the line passing through them has a slope in the range [-K, K].

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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



Approach: The idea is to traverse through all pairs of points and check whether their slope lies in the range [-K, K] or not. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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<int> x, vector<int> 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<int> 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




<script>
    // Javascript 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]
    function findPairs(x, y, K)
    {
        let n = x.length;
 
        // Store the result
        let ans = 0;
 
        // Traverse through all the
        // combination of points
        for (let i = 0; i < n; ++i) {
 
            for (let 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
        document.write(ans);
    }
 
  // Driver code
    let X = [ 2, 1, 0 ], Y = [ 1, 2, 0 ];
    let K = 1;
  
    // Function Call
    findPairs(X, Y, K);
     
    // This code is contributed by divyesh072019.
</script>
Output: 
2

 

Time Complexity: O(N2)
Auxiliary Space: O(1)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :