Description
Given an array of integers arr, and three integers a, b and c. Find and return the number of good triplets where abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c.
Examples
Input:
arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3Output:
4Explanation:
4 triplets satisfy all conditions.
Input:
arr = [1,1,1], a = 0, b = 0, c = 0Output:
1Explanation:
Only one triplet exists: (0,1,2). Since all elements are equal (arr[0]=arr[1]=arr[2]=1), all absolute differences are 0, which satisfies the strict conditions a=0, b=0, c=0.
Input:
arr = [2,5,8,3,6], a = 4, b = 1, c = 2Output:
0Explanation:
No valid triplets exist. All three distance conditions must be satisfied simultaneously. The small values of b=1 and c=2 are very restrictive, and no combination of three elements from this array satisfies all three constraints at once.
Constraints
- •
3 ≤ arr.length ≤ 100