Count of triplets with sum less than given value

Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

In the given array, find the number of triplets with sum less than the given value.


a)    Input array : [1,2,3,4,5,6,7,8]
Sum = 10

Output : 7
Possible triplets are : (1,2,3), (1,2,4), (1,2,5), (1,2,6), (1,3,4), (1,3,5), (2,3,4)

b)    Input array : [3, 7, 9, 1, 2, 5, 11, 4]
Sum = 10

Output : 6
Possible triplets are : (3,1,2), (3,1,5), (3,1,4), (3,2,4), (1,2,5), (1,2,4)

Method 1

Time Complexity : O(n3)


Step 1 : Run 3 nested loops, selecting all triplets possible.

Step 2 : Initialize result = 0.

Step 3 : If Sum of elements is less than Sum, increment count.

Step 4 : Print result as count of triplets satisfying the condition.

C++ Program

#include <bits/stdc++.h>
using namespace std;
//Main function
int main()
    int array[] = {3, 7, 9, 1, 2, 5, 11, 4};
    int N = sizeof(array)/sizeof(array[0]);
    int sum = 10;
    int result = 0;
    //First for loop for selecting first element
    //Second loop for second element
    //Third loop for third element.
    //check for triplets satisfing the condition, and increment result
    for (int i = 0; i < N-2; i++)
       for (int j = i+1; j < N-1; j++)
           for (int k = j+1; k < N; k++)
               if(array[i]+array[j]+array[k] < sum)
    cout<<"Number of Triplets found = ";
    return 0;
Try It

Method 2

Time Complexity : O(n2)


Step 1 : Sort the given array, initialize result = 0.

Step 2 : Iterate from i to N -2 (N is size of array), and take array[i] and first element of triplet.

Step 3 : Initialize other two elements as corner elements. array[i+1] and array[N-1].

Step 4 : move j and k until they meet do,

  1. If sum is greater than the given sum, then move pointer of last element. (array[N-1]).
  2. Else if sum is less than the given sum, then there can k -j possible tird elements satisfying, so add (k-j) to result. And move array[i+1] pointer.

Step 5 : Print result after ending the loop.

Algorithm working

C++ Program

#include <bits/stdc++.h>
using namespace std;
// Main function
int main()
    int array[] ={1, 2,3,5,6,};//input array
    int N = sizeof(array)/sizeof(array[0]);//size of the input array(N)
    int Sum = 10;//input Sum
    int result = 0;//initilizing count of triplets
    sort(array,array+N);//sorting the input array
    //selecting first element
    for(int i = 0; i < N - 2; i++)
        int j=i+1,k=N-1;//second and third elements as last two elements
            // Sum of triplet is greater than or equalto given sum, move to find smaller values
            if(array[i] + array[j] + array[k] >= Sum)
            // Sum of triplet is less than given sum
                //for current i and j there can be k-j elements, move j pointer
                result += (k - j);
    cout<<"Number of Triplets found = ";
Try It

C++ Online Test - Check Your Rank across the World

Next > < Prev
Scroll to Top