Problem · Sorting
Count Increasing Triplets
Learn this problemProblem statement
Given an array d of distinct integers and a threshold t, count the triplets of distinct indices (a, b, c) that satisfy both conditions:
d[a] < d[b] < d[c]d[a] + d[b] + d[c] ≤ t
Because all values in d are distinct, each selection of three values has at most one ordering that is strictly increasing.
Return the number of valid triplets as a long.
Function
triplets(t: long, d: int[]) → longExamples
Example 1
t = 8d = [1, 2, 3, 4, 5]return = 4The valid increasing value triplets are:
(1, 2, 3), with sum6.(1, 2, 4), with sum7.(1, 2, 5), with sum8.(1, 3, 4), with sum8.
Therefore the result is 4.
Example 2
t = 8d = [1, 2, 3, 4, 6]return = 3The valid increasing value triplets are (1, 2, 3), (1, 2, 4), and (1, 3, 4). Their sums are at most 8, so the result is 3.
Constraints
- All values in
dare distinct. 0 ≤ d[i] ≤ 10^90 < t < 3 × 10^9
More Citadel problems
- Limit Order Book Matching EnginePHONE SCREEN · Seen Jul 2026
- Minimum Changes for a Periodic PalindromeOA · Seen Jul 2026
- Minimum Image Processing CostOA · Seen Jul 2026
- Minimum Path Sum to Target in Binary TreePHONE SCREEN · Seen Apr 2026
- Minimum Time to Process RequestsOA · Seen Mar 2026
- Process SchedulingOA · Seen Mar 2026
- Social Media SuggestionsOA · Seen May 2025
- Best Sum Downward Tree PathOA · Seen May 2025