Geeksforgeeks

Error

Given an array **arr[]** of distinct integers of size **N** and a value **sum**, the task is to find the count of triplets **(i, j, k)**, having **(i<j<k) **with the sum of** (arr[i] + arr[j] + arr[k]) **smaller than the given value sum.

**Example 1:**

N = 4, sum = 2 arr[] = {-2, 0, 1, 3}Input:2Output:Below are triplets with sum less than 2 (-2, 0, 1) and (-2, 0, 3).Explanation:

**Example 2:**

N = 5, sum = 12 arr[] = {5, 1, 3, 4, 7}Input:4Output:Below are triplets with sum less than 12 (1, 3, 4), (1, 3, 5), (1, 3, 7) and (1, 4, 5).Explanation:

**Your Task:**

This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.

** Expected Time Complexity: **O(N

3 ≤ N ≤ 10

-10^{3} ≤ arr[i] ≤ 10^{3}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Count triplets with sum smaller than X

...