Ways to choose three points with distance between the most distant points <= L


The problem states that we need to figure out the number of ways to choose three points with distance between the most distant points less than or equal to L, a positive integer that will be given as an input.

In the problem we will be given an array of different points which lies on the x-axis and an integer L greater than 0. The task involves finding the number of sets of three points where the distance between the most distant points is less than or equal to that integer L.

NOTE : The set of points can be in any order with respect to given in the array.

We can understand the problem better with the below given examples.

Input

a[] = {5, 6, 7, 8}
L=3

Output

4

Explanation : The sets of three points from the given array with the most distant points less than or equal to three in distance are as follows:

{5, 6, 7} ,{ 5, 6, 8} , {5, 7, 8} and {6, 7, 8}.

Never are the most distant points in a set more than three times away from one another.

Since the set of points can be in any order {5,6,7} and {5,7,6} are considered a similar set of three points.

Input

a[] = {3, 5, 6, 9}
L=3

Output

1

Explanation : Since the distance between two farthest points of the triplet can only be atmost 3. There is only one triplet possible i.e. {3, 5, 6}.

We can solve the above problem with different approaches in C++. Let’s look at the approach to solve the problem from a naive approach to an efficient solution.

Approach-1

This will be the fundamental strategy for solving the issue. The approach's goal is to examine every triplet that can exist in the provided array and determine whether or not the distance between the furthest points is less than or equal to the specified value of L. The triplet will be counted if the distance between its most distant locations is less than or equal to L otherwise, we shall look for further triplets. This will provide us with every option for selecting the three points whose distance between the furthest point is less than or equal to L.

Following these steps we can implement this approach in our code:

  • Sort the given array so that iterating in the given array we can check for every triplet possible such that a[i]<a[i+1]<a[i+2].

  • Declare a variable with name count=0 to count the number of triplets possible with the furthest points less than or equal to L.

  • Traverse the whole array using three nested loops for each and every triplet starting from x=0 in the first loop, y=x+1 in the second and z=y+1 in the third loop.

  • If we find any triplet where a[z]-a[x]<=L, we will increase the count by 1 and keep traversing the array for next possible triplets.

  • Traversing the whole array from i=0 to i<array size-2 to check for all the triplets possible, we can find every possible way to choose three points with the furthest points <=L.

Example

//C++ code to find number of ways of choosing triplet

#include <bits/stdc++.h>

using namespace std;

//function to count the number of ways of choosing three points
int triplets(int a[], int N, int L){
   
   int count =0; //to count the total number of ways
   
   sort(a,a+N); //sorting the array using in-buit sort() function
   
   //iterating in the nested loops to check every triplet
   for(int x=0;x<N-2;x++){
      for(int y=x+1;y<N-1;y++){
         for(int z=y+1;z<N;z++){
            int furthest = a[z] - a[x];
            if(furthest<=L){ //if distance between the furthest points <=L
               count += 1; //increasing count by 1
            }
         }
      }
   }
   
   return count;  //return the possible number of ways satisfying the condition
}

int main()
{
   int a[] = {10, 2, 3, 7, 13, 9};
   int L=5;
   
   int N= sizeof(a)/sizeof(a[0]); //to calculate size of array
   
   cout<<"Total triplets possible : "<<triplets(a, N, L)<<endl;

   return 0;
}

Output

Total triplets possible : 3

Time Complexity : O(N^3) , where N is the size of the given array.

Space Complexity : O(1) , because we didn’t use any extra space.

Approach-2 (an efficient approach)

The efficient solution to the above naive approach could be using binary search. We will simply iterate in the array from i=0 to i<array size. For each element present in the array we will find the number of points greater than a[i] and less than or equal to a[i]+L. To find all the possible triplets with a[i] where the distance between the farthest points is less than or equal to L, we can use combinations.

$\mathrm{^n{C_{r}}=\frac{n!}{(n-r)!r!}}$ where n will be the number of points greater than a[i] and less than or equal to a[i]+L and r will be 2 since we want to choose two points to make a possible triplet.

So replacing the values of n and r, we can write

Number of triplets = number of points * (number of points - 1)/2

Using this approach, we can find every possible triplets satisfying the condition for every point in the array i.e. a[i].

Follow the below steps to implement the approach in C++:

  • We will make a function to calculate the number of possible triplets.

  • After that, sort the given array to apply binary search using sort() function which is present in C++.

  • Traverse the whole array from i=0 to i<array’s size-2 to calculate possible triplets for every element.

  • For every element a[i], find the index of the first element greater than a[i]+L using the upper bound library in C++, which returns the pointer pointing to the index in the range provided as parameter, greater than the value passed.

  • To find the number of points greater than a[i] and less than or equal to L, subtract the index of the point greater than a[i]+L with i+1.

  • If number of points greater than a[i] and <=a[i]+L, is greater than or equal to 2, the number of possible triplets where the furthest point will be <=L can be calculated using the above derived formula i.e. number of points * (number of points - 1)/2.

  • Calculate every possible triplet where distance between furthest points is <=L for every element present in the given array.

Example

//C++ code to calculate number of possible ways to choose triplet using binary search
#include<bits/stdc++.h>

using namespace std;

//function to print the total triplets possible where distance
//between the furthest points <=L
int triplets(int a[], int N, int L)
{
   sort(a, a + N); //sort the array using sort() function
   
   int count = 0; //to count the number of ways possible
   
   
   for (int i = 0; i <= N-2; i++) {
       //using upper bound function to find the index greater than a[i]+L
     int index = upper_bound(a, a + N,a[i] + L) - a; 
     
      int x = index - (i + 1); //number of points
      
      if (x >= 2) {
         
         count += (x * (x - 1) / 2); 
      }
   }
   //return the total ways
   return count;
}

int main()
{
   int a[] = {4,7,3,9,8,10};
   int L = 6;
     
   int N = sizeof(a) / sizeof(a[0]); //to calculate the size of the array
  
   cout<<"Total triplets possible : "<<triplets(a, N, L)<<endl; //calling the function
   
   return 0;
}

Output

Total triplets possible: 16

Time Complexity : O(NlogN) , where N array’s size.

Space Complexity : O(1) , because no extra space is being used.

Conclusion

After reading this post, I hope that all of your concepts about the issue have been cleared.

Updated on: 21-Aug-2023

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements