Given an array of distinct integers and a range [a, b], the task is to count the number of triplets having a sum in the range [a, b].

**Examples:**

Input : arr[] = {8, 3, 5, 2} range = [7, 11] Output : 1 There is only one triplet {2, 3, 5} having sum 10 in range [7, 11]. Input : arr[] = {2, 7, 5, 3, 8, 4, 1, 9} range = [8, 16] Output : 36

A **naive** approach is to run three loops to consider all the triplets one by one. Find the sum of each triplet and increment the count if the sum lies in a given range [a, b].

Below is the implementation of above approach:

// C++ program to count triplets with // sum that lies in given range [a, b]. #include <bits/stdc++.h> using namespace std; // Function to count triplets int countTriplets(int arr[], int n, int a, int b) { // Initialize result int ans = 0; // Fix the first element as A[i] for (int i = 0; i < n - 2; i++) { // Fix the second element as A[j] for (int j = i + 1; j < n - 1; j++) { // Now look for the third number for (int k = j + 1; k < n; k++) if (arr[i] + arr[j] + arr[k] >= a && arr[i] + arr[j] + arr[k] <= b) ans++; } } return ans; } // Driver Code int main() { int arr[] = { 2, 7, 5, 3, 8, 4, 1, 9 }; int n = sizeof arr / sizeof arr[0]; int a = 8, b = 16; cout << countTriplets(arr, n, a, b) << endl; return 0; }

**Output:**

36

**Time complexity:** O(n^{3})

An **efficient** solution is to first find the count of triplets having a sum less than or equal to upper limit b in the range [a, b]. This count of triplets will also include triplets having a sum less than the lower limit a. Subtract the count of triplets having a sum less than a. The final result is the count of triplets having a sum in the range [a, b].

The algorithm is as follows:

- Find count of triplets having a sum less than or equal to b. Let this count be x.
- Find count of triplets having a sum less than a. Let this count be y.
- Final result is x-y.

To find the count of triplets having a sum less than or equal to given value, refer Count triplets with sum smaller than a given value

Below is the implementation of the above approach:

// CPP program to count triplets with // sum that lies in given range [a, b]. #include <bits/stdc++.h> using namespace std; // Function to find count of triplets having // sum less than or equal to val. int countTripletsLessThan(int arr[], int n, int val) { // sort the input array. sort(arr, arr + n); // Initialize result int ans = 0; int j, k; // to store sum int sum; // Fix the first element for (int i = 0; i < n - 2; i++) { // Initialize other two elements as // corner elements of subarray arr[j+1..k] j = i + 1; k = n - 1; // Use Meet in the Middle concept. while (j != k) { sum = arr[i] + arr[j] + arr[k]; // If sum of current triplet // is greater, then to reduce it // decrease k. if (sum > val) k--; // If sum is less than or equal // to given value, then add // possible triplets (k-j) to result. else { ans += (k - j); j++; } } } return ans; } // Function to return count of triplets having // sum in range [a, b]. int countTriplets(int arr[], int n, int a, int b) { // to store count of triplets. int res; // Find count of triplets having sum less // than or equal to b and subtract count // of triplets having sum less than or // equal to a-1. res = countTripletsLessThan(arr, n, b) - countTripletsLessThan(arr, n, a - 1); return res; } // Driver Code int main() { int arr[] = { 2, 7, 5, 3, 8, 4, 1, 9 }; int n = sizeof arr / sizeof arr[0]; int a = 8, b = 16; cout << countTriplets(arr, n, a, b) << endl; return 0; }

**Output:**

36

**Time complexity:** O(n^{2})

**Auxiliary space:** O(1)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.