Count pairs of coordinates connected by a line with slope in the range [ | C ++ 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 ] ; Store the result ; Traverse through all the combination of points ; If pair satisfies the given condition ; Increment ans by 1 ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPairs ( vector < int > x , vector < int > y , int K ) { int n = x . size ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { if ( K * abs ( x [ i ] - x [ j ] ) >= abs ( y [ i ] - y [ j ] ) ) { ++ ans ; } } } cout << ans ; } int main ( ) { vector < int > X = { 2 , 1 , 0 } , Y = { 1 , 2 , 0 } ; int K = 1 ; findPairs ( X , Y , K ) ; return 0 ; }