Description
Solutions
Count Triplets
π₯ FULLTIMEπRELATED PROBLEMS
Given an array of n
distinct integers, d = [d[0], d[1], ..., d[n-1]]
, and an integer threshold,
t
, how many (a, b, c)
index triplets exist that satisfy both of the following conditions?
d[a] < d[b] < d[c]
d[a] + d[b] + d[c] β€ t
Function Description
Complete the function triplets
in the editor.
triplets
has the following parameter(s):
- 1.
int t
: an integer threshold - 2.
int d[n]
: an array of integers
Returns
long integer
: a long integer that denotes the number of (a, b, c)
triplets that satisfy the given conditions
Example 1:
Input: t = 8, d = [1, 2, 3, 4, 5]
Output: 4
Explanation:The following 4 triplets satisfy the constraints:(1,2,3) β 1 + 2 + 3 = 6 β€ 8 (1,2,4) β 1 + 2 + 4 = 7 β€ 8 (1,2,5) β 1 + 2 + 5 = 8 β€ 8 (1,3,4) β 1 + 3 + 4 = 8 β€ 8
Example 2:
Input: t = 8, d = [1, 2, 3, 4, 6]
Output: 3
Explanation:Givent = 8
andd = [1, 2, 3, 4, 6]
, the following triplets satisfy the conditions:(0, 1, 2) β 1 + 2 + 3 β€ 8 (0, 1, 3) β 1 + 2 + 4 β€ 8 (0, 2, 3) β 1 + 3 + 4 β€ 8
Constraints:
0 β€ d[i] β€ 109
0 < t < 3 Γ 109
Related Problems
Testcase
Result
Case 1
input:
output: