Ways to choose three points with distance between the most distant points <= L
Given a set of n distinct points x1, x2, x3… xn all lying on the X-axis and an integer L, the task is to find the number of ways of selecting three points such that the distance between the most distant points is less than or equal to L
Note: Order is not important i.e the points {3, 2, 1} and {1, 2, 3} represent the same set of three points
Examples:
Input : x = {1, 2, 3, 4}
L = 3
Output : 4Explanation:
Ways to select three points such that the distance between
the most distant points <= L are:
1) {1, 2, 3} Here distance between farthest points = 3 – 1 = 2 <= L
2) {1, 2, 4} Here distance between farthest points = 4 – 1 = 3 <= L
3) {1, 3, 4} Here distance between farthest points = 4 – 1 = 3 <= L
4) {2, 3, 4} Here distance between farthest points = 4 – 2 = 2 <= LThus, total number of ways = 4
Naive Approach:
First of all, sort the array of points to generate triplets {a, b, c} such that a and c are the farthest points of the triplet and a < b < c, since all the points are distinct. We can generate all the possible triplets and check for the condition if the distance between the two most distant points in <= L. If it holds we count this way, else we don't
C++
// C++ program to count ways to choose // triplets such that the distance // between the farthest points <= L #include<bits/stdc++.h> using namespace std; // Returns the number of triplets with // distance between farthest points <= L int countTripletsLessThanL(int n, int L, int* arr) { // sort to get ordered triplets so that we can // find the distance between farthest points // belonging to a triplet sort(arr, arr + n); int ways = 0; // generate and check for all possible // triplets: {arr[i], arr[j], arr[k]} for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { // Since the array is sorted the // farthest points will be a[i] // and a[k]; int mostDistantDistance = arr[k] - arr[i]; if (mostDistantDistance <= L) { ways++; } } } } return ways; } // Driver Code int main() { // set of n points on the X axis int arr[] = { 1, 2, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); int L = 3; int ans = countTripletsLessThanL(n, L, arr); cout << "Total Number of ways = " << ans << "\n"; return 0; } |
Java
// Java program to count ways to choose // triplets such that the distance // between the farthest points <= L import java .io.*; import java .util.Arrays; class GFG { // Returns the number of triplets with // distance between farthest points <= L static int countTripletsLessThanL(int n, int L, int []arr) { // sort to get ordered triplets // so that we can find the // distance between farthest // points belonging to a triplet Arrays.sort(arr); int ways = 0; // generate and check for all possible // triplets: {arr[i], arr[j], arr[k]} for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { // Since the array is sorted the // farthest points will be a[i] // and a[k]; int mostDistantDistance = arr[k] - arr[i]; if (mostDistantDistance <= L) { ways++; } } } } return ways; } // Driver Code static public void main (String[] args) { // set of n points on the X axis int []arr = {1, 2, 3, 4}; int n =arr.length; int L = 3; int ans = countTripletsLessThanL(n, L, arr); System.out.println("Total Number of ways = " + ans); } } // This code is contributed by anuj_67. |
Python3
# Python3 program to count ways to choose # triplets such that the distance # between the farthest points <= L # Returns the number of triplets with # distance between farthest points <= L def countTripletsLessThanL(n, L, arr): # sort to get ordered triplets so that # we can find the distance between # farthest points belonging to a triplet arr.sort() ways = 0 # generate and check for all possible # triplets: {arr[i], arr[j], arr[k]} for i in range(n): for j in range(i + 1, n): for k in range(j + 1, n): # Since the array is sorted the # farthest points will be a[i] # and a[k]; mostDistantDistance = arr[k] - arr[i] if (mostDistantDistance <= L): ways += 1 return ways # Driver Code if __name__ == "__main__": # set of n points on the X axis arr = [1, 2, 3, 4 ] n = len(arr) L = 3 ans = countTripletsLessThanL(n, L, arr) print ("Total Number of ways =", ans) # This code is contributed by ita_c |
C#
// C# program to count ways to choose // triplets such that the distance // between the farthest points <= L using System; class GFG { // Returns the number of triplets with // distance between farthest points <= L static int countTripletsLessThanL(int n, int L, int []arr) { // sort to get ordered triplets // so that we can find the // distance between farthest // points belonging to a triplet Array.Sort(arr); int ways = 0; // generate and check for all possible // triplets: {arr[i], arr[j], arr[k]} for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { // Since the array is sorted the // farthest points will be a[i] // and a[k]; int mostDistantDistance = arr[k] - arr[i]; if (mostDistantDistance <= L) { ways++; } } } } return ways; } // Driver Code static public void Main () { // set of n points on the X axis int []arr = {1, 2, 3, 4}; int n =arr.Length; int L = 3; int ans = countTripletsLessThanL(n, L, arr); Console.WriteLine("Total Number of ways = " + ans); } } // This code is contributed by anuj_67. |
PHP
<?php // PHP program to count ways to choose // triplets such that the distance // between the farthest points <= L // Returns the number of triplets with // distance between farthest points <= L function countTripletsLessThanL($n, $L, $arr) { // sort to get ordered triplets so that // we can find the distance between // farthest points belonging to a triplet sort($arr); $ways = 0; // generate and check for all possible // triplets: {arr[i], arr[j], arr[k]} for ($i = 0; $i < $n; $i++) { for ($j = $i + 1; $j < $n; $j++) { for ($k = $j + 1; $k < $n; $k++) { // Since the array is sorted the // farthest points will be a[i] // and a[k]; $mostDistantDistance = $arr[$k] - $arr[$i]; if ($mostDistantDistance <= $L) { $ways++; } } } } return $ways; } // Driver Code // set of n points on the X axis $arr = array( 1, 2, 3, 4 ); $n = sizeof($arr); $L = 3; $ans = countTripletsLessThanL($n, $L, $arr); echo "Total Number of ways = " , $ans, "\n"; // This code is contributed by akt_mit ?> |
Output:
Total Number of ways = 4
Time Complexity: O(n3) for generating all possible triplets.
Efficient Approach:
- This problem can be solved by using Binary search.
- First of all, sort the array.
- Now, for each element of the array we find the number of elements which are greater than it(by maintaining a sorted order of points) and lie in the range (xi + 1, xi + L) both inclusive (Note that here all points are distinct so we need consider the elements equal to xi itself).
- Doing so we find all such points where the distance between the farthest points will always be less than or equal to L.
- Now let’s say for the ith point, we have M such points which are less than or equal to xi + L, then the number of ways we can select 2 points from M such points is simply
M * (M – 1) / 2
// C++ program to count ways to choose // triplets such that the distance between // the farthest points <= L */ #include<bits/stdc++.h> using namespace std; // Returns the number of triplets with the // distance between farthest points <= L int countTripletsLessThanL(int n, int L, int* arr) { // sort the array sort(arr, arr + n); int ways = 0; for (int i = 0; i < n; i++) { // find index of element greater than arr[i] + L int indexGreater = upper_bound(arr, arr + n, arr[i] + L) - arr; // find Number of elements between the ith // index and indexGreater since the Numbers // are sorted and the elements are distinct // from the points btw these indices represent // points within range (a[i] + 1 and a[i] + L) // both inclusive int numberOfElements = indexGreater - (i + 1); // if there are at least two elements in between // i and indexGreater find the Number of ways // to select two points out of these if (numberOfElements >= 2) { ways += (numberOfElements * (numberOfElements - 1) / 2); } } return ways; } // Driver Code int main() { // set of n points on the X axis int arr[] = { 1, 2, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); int L = 4; int ans = countTripletsLessThanL(n, L, arr); cout << "Total Number of ways = " << ans << "\n"; return 0; } |
Output
Total Number of ways = 4
Time Complexity:O(NlogN) where N is the number of points.
Recommended Posts:
- Minimum number of points to be removed to get remaining points on one side of axis
- Steps required to visit M points in order on a circular ring of N points
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Program for distance between two points on earth
- Hammered distance between N points in a 2-D plane
- Check whether it is possible to join two points given on circle such that distance between them is k
- Find points at a given distance on a line of given slope
- Haversine formula to find distance between two points on a sphere
- Pick points from array such that minimum distance is maximized
- Number of Integral Points between Two Points
- Number of ways to choose elements from the array such that their average is K
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Number of quadrilaterals possible from the given points
- Circle and Lattice Points
- Orientation of 3 ordered points
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



