Count Triplets with Sum Less than Target
Given an array of integers and a target integer, find the number of triplets in the array that have a sum less than the given target. A triplet is a grouping of three integers from the array. The integers in a triplet should be distinct indices (i.e., they should not be the same element repeated).
[ [ -1, 0, 2, 3 ], 3 ]
Explanation. The triplets with sum less than 3 are [-1, 0, 2] and [-1, 0, 3].
[ [ -1, 4, 2, 1, 3 ], 5 ]
Explanation. The valid triplets are [-1, 4, 1], [-1, 2, 1], [-1, 1, 3], and [-1, 2, 3].
[ [ 5, 1, 3, 4, 7 ], 12 ]
Explanation. The valid triplets are [5, 1, 3], [1, 3, 4], [1, 3, 7], and [1, 4, 7].
Follow-up: Can this problem be solved more efficiently using a specific sorting or two-pointer technique?
- The array may contain both positive and negative integers. - The size of the array is at least 3. - The target can be any integer.
- Views
- 4