# Count triplets with a sum smaller than a given value.

`Input : arr[] = {-2, 0, 1, 3}        sum = 2.Output : 2Explanation :  Below are triplets with sum less than 2               (-2, 0, 1) and (-2, 0, 3) Input : arr[] = {5, 1, 3, 4, 7}        sum = 12.Output : 4Explanation :  Below are triplets with sum less than 12               (1, 3, 4), (1, 3, 5), (1, 3, 7) and                (1, 4, 5)`
`class TripletCounter:    def countTriplets(self, arr, n, sum):        counter = 0        for a in range(len(arr)-2):            for b in range(a+1, len(arr)-1):                for c in range(b+1, len(arr)):                    if arr[a] + arr[b] + arr[c] < sum:                        counter += 1        return counter`
`class TripletCounter:    def countTriplets(self, arr, n, sum):        arr.sort()        counter = 0        for i in range(0,n-2):            j = i + 1            k = n-1            while(j < k):                 if (arr[i]+arr[j]+arr[k] >=sum):                        k = k-1                 else:                        ans += (k - j)                        j = j+1        return ans`

--

--

--

## More from Annamariya Tharayil

Software Engineer. Find me @ www.linkedin.com/in/annamariya-jt

Love podcasts or audiobooks? Learn on the go with our new app.

## Annamariya Tharayil

Software Engineer. Find me @ www.linkedin.com/in/annamariya-jt