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 : 4
Explanation:
Ways to select three points such that the distance between the most distant points <= L are:
- {1, 2, 3} Here distance between farthest points = 3 – 1 = 2 <= L
- {1, 2, 4} Here distance between farthest points = 4 – 1 = 3 <= L
- {1, 3, 4} Here distance between farthest points = 4 – 1 = 3 <= L
- {2, 3, 4} Here distance between farthest points = 4 – 2 = 2 <= L
Thus, 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
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int countTripletsLessThanL( int n, int L, int * arr)
{
sort(arr, arr + n);
int ways = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
int mostDistantDistance = arr[k] - arr[i];
if (mostDistantDistance <= L) {
ways++;
}
}
}
}
return ways;
}
int main()
{
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
import java .io.*;
import java .util.Arrays;
class GFG {
static int countTripletsLessThanL( int n, int L,
int []arr)
{
Arrays.sort(arr);
int ways = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
for ( int k = j + 1 ; k < n; k++) {
int mostDistantDistance =
arr[k] - arr[i];
if (mostDistantDistance <= L)
{
ways++;
}
}
}
}
return ways;
}
static public void main (String[] args)
{
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);
}
}
|
Python3
def countTripletsLessThanL(n, L, arr):
arr.sort()
ways = 0
for i in range (n):
for j in range (i + 1 , n):
for k in range (j + 1 , n):
mostDistantDistance = arr[k] - arr[i]
if (mostDistantDistance < = L):
ways + = 1
return ways
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ]
n = len (arr)
L = 3
ans = countTripletsLessThanL(n, L, arr)
print ( "Total Number of ways =" , ans)
|
C#
using System;
class GFG {
static int countTripletsLessThanL( int n, int L,
int []arr)
{
Array.Sort(arr);
int ways = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
int mostDistantDistance = arr[k] - arr[i];
if (mostDistantDistance <= L)
{
ways++;
}
}
}
}
return ways;
}
static public void Main ()
{
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);
}
}
|
PHP
<?php
function countTripletsLessThanL( $n , $L , $arr )
{
sort( $arr );
$ways = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i + 1; $j < $n ; $j ++)
{
for ( $k = $j + 1; $k < $n ; $k ++)
{
$mostDistantDistance = $arr [ $k ] -
$arr [ $i ];
if ( $mostDistantDistance <= $L )
{
$ways ++;
}
}
}
}
return $ways ;
}
$arr = array ( 1, 2, 3, 4 );
$n = sizeof( $arr );
$L = 3;
$ans = countTripletsLessThanL( $n , $L , $arr );
echo "Total Number of ways = " , $ans , "\n" ;
?>
|
Javascript
<script>
function countTripletsLessThanL(n , L, arr)
{
arr.sort();
var ways = 0;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
for (k = j + 1; k < n; k++) {
var mostDistantDistance = arr[k] - arr[i];
if (mostDistantDistance <= L) {
ways++;
}
}
}
}
return ways;
}
var arr = [ 1, 2, 3, 4 ];
var n = arr.length;
var L = 3;
var ans = countTripletsLessThanL(n, L, arr);
document.write( "Total Number of ways = " + ans);
</script>
|
Output
Total Number of ways = 4
Time Complexity: O(n3) for generating all possible triplets.
Auxiliary space: O(1) because using constant space for variables
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
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int countTripletsLessThanL( int n, int L, int * arr)
{
sort(arr, arr + n);
int ways = 0;
for ( int i = 0; i < n; i++) {
int indexGreater = upper_bound(arr, arr + n,
arr[i] + L) - arr;
int numberOfElements = indexGreater - (i + 1);
if (numberOfElements >= 2) {
ways += (numberOfElements
* (numberOfElements - 1) / 2);
}
}
return ways;
}
int main()
{
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;
}
|
Java
import java.util.*;
class GFG
{
static int countTripletsLessThanL( int n, int L,
int [] arr)
{
Arrays.sort(arr);
int ways = 0 ;
for ( int i = 0 ; i < n; i++)
{
int indexGreater = upper_bound(arr, 0 , n,
arr[i] + L);
int numberOfElements = indexGreater - (i + 1 );
if (numberOfElements >= 2 )
{
ways += (numberOfElements *
(numberOfElements - 1 ) / 2 );
}
}
return ways;
}
static int upper_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2 ;
if (a[middle] > element)
high = middle;
else
low = middle + 1 ;
}
return low;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int n = arr.length;
int L = 4 ;
int ans = countTripletsLessThanL(n, L, arr);
System.out.println( "Total Number of ways = " + ans);
}
}
|
Python3
def countTripletsLessThanL(n, L, arr):
arr = sorted (arr);
ways = 0 ;
for i in range (n):
indexGreater = upper_bound(arr, 0 , n, arr[i] + L);
numberOfElements = indexGreater - (i + 1 );
if (numberOfElements > = 2 ):
ways + = (numberOfElements * (numberOfElements - 1 ) / 2 );
return ways;
def upper_bound(a, low, high, element):
while (low < high):
middle = int (low + (high - low) / 2 );
if (a[middle] > element):
high = middle;
else :
low = middle + 1 ;
return low;
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 ];
n = len (arr);
L = 4 ;
ans = countTripletsLessThanL(n, L, arr);
print ( "Total Number of ways = " , ans);
|
C#
using System;
class GFG
{
static int countTripletsLessThanL( int n, int L,
int [] arr)
{
Array.Sort(arr);
int ways = 0;
for ( int i = 0; i < n; i++)
{
int indexGreater = upper_bound(arr, 0, n,
arr[i] + L);
int numberOfElements = indexGreater - (i + 1);
if (numberOfElements >= 2)
{
ways += (numberOfElements *
(numberOfElements - 1) / 2);
}
}
return ways;
}
static int upper_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2;
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4 };
int n = arr.Length;
int L = 4;
int ans = countTripletsLessThanL(n, L, arr);
Console.WriteLine( "Total Number of ways = " + ans);
}
}
|
Javascript
<script>
function countTripletsLessThanL(n, L, arr)
{
arr.sort( function (a, b){ return a - b});
let ways = 0;
for (let i = 0; i < n; i++)
{
let indexGreater = upper_bound(arr, 0, n,
arr[i] + L);
let numberOfElements = indexGreater - (i + 1);
if (numberOfElements >= 2)
{
ways += (numberOfElements *
(numberOfElements - 1) / 2);
}
}
return ways;
}
function upper_bound(a, low, high, element)
{
while (low < high)
{
let middle = low +
parseInt((high - low) / 2, 10);
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
let arr = [ 1, 2, 3, 4 ];
let n = arr.length;
let L = 4;
let ans = countTripletsLessThanL(n, L, arr);
document.write( "Total Number of ways = " + ans);
</script>
|
Output
Total Number of ways = 4
Time Complexity: O(NlogN) where N is the number of points.
Auxiliary Space: O(1) because using constant space for variables.
Last Updated :
25 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...