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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Orientation of 3 ordered points
- Program to find equation of a plane passing through 3 points
- Equation of circle when three points on the circle are given
- Find the equation of plane which passes through two points and parallel to a given axis
- Number of Integral Points between Two Points
- Minimum number of points to be removed to get remaining points on one side of axis
- Number of horizontal or vertical line segments to connect 3 points
- Count distinct points visited on the number line
- Program to delete a line given the line number from a file
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Ways to choose three points with distance between the most distant points <= L
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Count maximum points on same line
- Represent a given set of points by the best possible straight line
- Program to find line passing through 2 Points
- Find points at a given distance on a line of given slope
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Check whether a straight line can be formed using N co-ordinate points
- Slope of the line parallel to the line with the given slope
- Closest Pair of Points using Divide and Conquer algorithm
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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