


If ‘N’ = 7, ‘ARR’ = { 1, 5, 2, 3, 4, 6, 7 } and ‘TARGET’ = 9
Then, there are three triplets with sum less than 9:
1) {1, 5, 2}
2) {1, 2, 3}
3) {1, 2, 4}
4) {1, 3, 4}
Thus, the output will be 4.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’, denoting the size of the array.
The second line of each test case contains 'N' integers ‘ARR’, denoting the array elements.
The third line of each test case contains a single integer ‘TARGET’, denoting the target value to evaluate the smaller sum.
For each test case, print the count of triplets having a sum less than the given target value.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ 'T' ≤ 10
1 ≤ 'N' ≤ 100
-100 ≤ 'ARR[i]' ≤ 100
-100 ≤ 'TARGET' ≤ 100
Time Limit: 1 sec
We can simply run three loops and generate all the possible triplets, for each triplet we can then check if its sum is less than the given target value, we can finally return the count of triplets that had a sum less than the target value (Standard Brute Force Approach!).
The steps are as follows :
An initial precomputation cost of sorting the input array can help us solve this problem quickly, after sorting the array we can generate all the pairs, and for each generated pair we can find the third number for its triplet using the simple binary search.
For each generated pair (ARR[i], ARR[j]), we can find the third number ARR[k] such that ARR[k] is greatest and the following property holds true: ARR[i] + ARR[j] + ARR[k] < Target, once we have found such an ARR[k] we can conclude that all the numbers lying between the indices [j+1, k] will also satisfy the property: ARR[i] + ARR[j] + ARR[k] < Target, (that’s the reason we sorted the array initially).
The steps are as follows :
An initial precomputation cost of sorting the input array can help us solve this problem quickly. Let us see how this works.
Let’s discuss a simpler version of this problem first and then extend the logic to this one. Consider the case where we are asked to find the number of pairs in the array with their sum less than some given target value. One can easily approach this problem using the two-pointer approach in linear time complexity, we can sort the array and then initialize two pointers one marking the start and one marking the end of the current search space. The start pointer marks the first element of our pair and we need to adjust the end pointer such that the sum of elements corresponding to the start and end pointer is less than the target value and the value of the end is as large as possible
Using this we can conclude that all the elements lying between [start+1, end] can be the second element of the pair when the first element is the element corresponding to the start pointer. Note that we don’t have to adjust the end pointer to point to the last element of the sorted element each time, we can just continue our search considering the logic that the next element pointed by the start pointer will be smaller than the previous one which means the element corresponding to the end will never lie to the right of the current position of the end pointer, in short, this means that we have to move the start pointer only to the right and end pointer only to the left (standard 2-pointer approach!).
Extending this logic further, if we fix the first element of our triplet and find the remaining two elements with the target value now equal to Target - ARR[i] where the first element of the triplet is ARR[i]. That is, we can now apply the same approach discussed above to find the other two elements of the triplets using the updated value of the target.
The steps are as follows :