Given an array of n integers, slope of a line i. e., m and the intercept of the line i.e c, Count the number of ordered pairs(i, j) of points where i ≠ j, such that point (Ai, Aj) satisfies the line formed with given slope and intercept.
Note : The equation of the line is y = mx + c, where m is the slope of the line and c is the intercept.
Input : m = 1, c = 1, arr = [ 1, 2, 3, 4, 2 ]
Output : 5 ordered points
Explanation : The equation of the line with given slope and intercept is : y = x + 1. The Number of pairs (i, j), for which (arri, arrj) satisfies the above equation of the line are : (1, 2), (1, 5), (2, 3), (3, 4), (5, 3).
Input : m = 2, c = 1, arr = [ 1, 2, 3, 4, 2, 5 ]
Output : 3 ordered points
Method 1 (Brute Force):
Generate all possible pairs (i, j) and check if a particular ordered pair (i, j) is such that, (arri, arrj) satisfies the given equation of the line y = mx + c, and i ≠ j. If the point is valid(a point is valid if the above condition is satisfied), increment the counter which stores the total number of valid points.
Time Complexity : O(n2)
Method 2 (Efficient) :
Given a x coordinate of a point, for each x there is a unique value of y and the value of y is nothing but m * x + c. So, for each possible x coordinate of the array arr, calculate how many times the unique value of y which satisfies the equation of the line occurs in that array. Store count of all the integers of array, arr in a map. Now, for each value, arri, add to the answer, the number of occurrences of m * arri + c. For a given i, m * a[i] + c occurs x times in the array, then, add x to our counter for total valid points, but need to check that if a[i] = m * a[i] + c then, it is obvious that since this occurs x times in the array then one occurrence is at the ith index and rest (x – 1) occurrences are the valid y coordinates so add (x – 1) to our points counter.
Time Complexity : O(n)
- Count distinct points visited on the number line
- Number of horizontal or vertical line segments to connect 3 points
- Orientation of 3 ordered points
- Program to find equation of a plane passing through 3 points
- Represent a given set of points by the best possible straight line
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Count maximum points on same line
- Program to find line passing through 2 Points
- Find points at a given distance on a line of given slope
- Closest Pair of Points | O(nlogn) Implementation
- Closest Pair of Points using Divide and Conquer algorithm
- Minimum number of points to be removed to get remaining points on one side of axis
- Program to delete a line given the line number from a file
- Number of Integral Points between Two Points
- Equation of circle when three points on the circle are given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manishshaw1