Triplet Counting
Start Timer
0:00:00
Imagine you have been given an array of integers, and a query number k
. Your task is to write a function that finds all the triplets in the array that sum up to the query number k
.
Let’s illustrate this with some examples:
Example 1:
Input:
nums = [1, 2, 3, 4, 5]
k = 6
Output:
count_triplets(nums, k) -> 1
In this case, there is only one triplet that sums up to 6: (1, 2, 3).
Example 2:
Input:
nums = [10, 10, 20, 30, 40]
k = 60
Output:
count_triplets(nums, k) -> 3
In this case, there are three triplets that sum up to 60: (10, 10, 40), (10, 20, 30), and (10, 20, 30).
The value 10 can be retrieved as nums[0]
or nums[1]
, therefore resulting in two triplets with the same value.
Recommended questions for you
Personalized based on your user activity, skill level, and preferences.
.
.
.
.
.
.
.
.
.
Comments