给定一组 n 个不同的点 x 1 , x 2 , x 3 … x n都位于 X 轴上和一个整数 L,任务是找到选择三个点的方法的数量,使得最远处的点小于或等于L
注意:顺序并不重要,即点 {3, 2, 1} 和 {1, 2, 3} 表示三个点的相同集合
例子:
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) {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 <= L
Thus, total number of ways = 4
天真的方法:
首先,对点数组进行排序以生成三元组 {a, b, c},使得 a 和 c 是三元组中最远的点,并且 a < b < c,因为所有点都是不同的。我们可以生成所有可能的三元组并检查条件,如果 <= L 中两个最远点之间的距离。如果成立,我们就这样计算,否则我们不
C++
// C++ program to count ways to choose
// triplets such that the distance
// between the farthest points <= L
#include
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
Javascript
C++
// C++ program to count ways to choose
// triplets such that the distance between
// the farthest points <= L */
#include
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;
}
Java
// Java program to count ways to choose
// triplets such that the distance between
// the farthest points <= L */
import java.util.*;
class GFG
{
// Returns the number of triplets with the
// distance between farthest points <= L
static int countTripletsLessThanL(int n, int L,
int[] arr)
{
// sort the array
Arrays.sort(arr);
int ways = 0;
for (int i = 0; i < n; i++)
{
// find index of element greater than arr[i] + L
int indexGreater = upper_bound(arr, 0, n,
arr[i] + L);
// 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;
}
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;
}
// Driver Code
public static void main(String[] args)
{
// set of n points on the X axis
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);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to count ways to choose
# triplets such that the distance between
# the farthest points <= L '''
# Returns the number of triplets with the
# distance between farthest points <= L
def countTripletsLessThanL(n, L, arr):
# sort the array
arr = sorted(arr);
ways = 0;
for i in range(n):
# find index of element greater than arr[i] + L
indexGreater = upper_bound(arr, 0, n, arr[i] + L);
# 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
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;
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;
# Driver Code
if __name__ == '__main__':
# set of n points on the X axis
arr = [1, 2, 3, 4];
n = len(arr);
L = 4;
ans = countTripletsLessThanL(n, L, arr);
print("Total Number of ways = " , ans);
# This code is contributed by 29AjayKumar
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 the
// distance between farthest points <= L
static int countTripletsLessThanL(int n, int L,
int[] arr)
{
// sort the array
Array.Sort(arr);
int ways = 0;
for (int i = 0; i < n; i++)
{
// find index of element greater than arr[i] + L
int indexGreater = upper_bound(arr, 0, n,
arr[i] + L);
// 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;
}
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;
}
// Driver Code
public static void Main(String[] args)
{
// set of n points on the X axis
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);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
Total Number of ways = 4
时间复杂度: O(n 3 )用于生成所有可能的三元组。
有效的方法:
- 这个问题可以通过使用二分搜索来解决。
- 首先,对数组进行排序。
- 现在,对于数组的每个元素,我们找到大于它的元素的数量(通过维护点的排序顺序)并且位于范围 (x i + 1, x i + L) 中(注意这里所有点都是不同的,因此我们需要考虑等于 x i本身的元素)。
- 这样做我们找到所有最远点之间的距离总是小于或等于 L 的点。
- 现在让我们说对于第 i个点,我们有 M 个这样的点,它们小于或等于 x i + L,那么我们可以从 M 个这样的点中选择 2 个点的方法数很简单
M * (M – 1) / 2
C++
// C++ program to count ways to choose
// triplets such that the distance between
// the farthest points <= L */
#include
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;
}
Java
// Java program to count ways to choose
// triplets such that the distance between
// the farthest points <= L */
import java.util.*;
class GFG
{
// Returns the number of triplets with the
// distance between farthest points <= L
static int countTripletsLessThanL(int n, int L,
int[] arr)
{
// sort the array
Arrays.sort(arr);
int ways = 0;
for (int i = 0; i < n; i++)
{
// find index of element greater than arr[i] + L
int indexGreater = upper_bound(arr, 0, n,
arr[i] + L);
// 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;
}
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;
}
// Driver Code
public static void main(String[] args)
{
// set of n points on the X axis
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);
}
}
// This code is contributed by 29AjayKumar
蟒蛇3
# Python program to count ways to choose
# triplets such that the distance between
# the farthest points <= L '''
# Returns the number of triplets with the
# distance between farthest points <= L
def countTripletsLessThanL(n, L, arr):
# sort the array
arr = sorted(arr);
ways = 0;
for i in range(n):
# find index of element greater than arr[i] + L
indexGreater = upper_bound(arr, 0, n, arr[i] + L);
# 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
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;
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;
# Driver Code
if __name__ == '__main__':
# set of n points on the X axis
arr = [1, 2, 3, 4];
n = len(arr);
L = 4;
ans = countTripletsLessThanL(n, L, arr);
print("Total Number of ways = " , ans);
# This code is contributed by 29AjayKumar
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 the
// distance between farthest points <= L
static int countTripletsLessThanL(int n, int L,
int[] arr)
{
// sort the array
Array.Sort(arr);
int ways = 0;
for (int i = 0; i < n; i++)
{
// find index of element greater than arr[i] + L
int indexGreater = upper_bound(arr, 0, n,
arr[i] + L);
// 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;
}
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;
}
// Driver Code
public static void Main(String[] args)
{
// set of n points on the X axis
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);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出
Total Number of ways = 4
时间复杂度: O(NlogN) ,其中 N 是点数。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。