Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++



In this article, we have an array of positive integers of size n. Our task is to calculate the maximum sum of triplet ( ai + aj + ak ) such that 0 <= i < j < k < n and ai < aj < ak.

Here is an example to calculate the maximum increasing triplet sum of the array:

Input:
arr = 3 6 4 2 5 10

Output:
19

The explanation of the above example is as follows:

All possible triplets are:-

3 4 5 => sum = 12
3 6 10 => sum = 19
3 4 10 => sum = 17
4 5 10 => sum = 19
2 5 10 => sum = 17
Maximum sum = 19

Finding maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k]

Here are the approaches to calculate the maximum sum of increasing triplets in the given array:

Using Nested for Loop

To find the maximum triplet sum in an array with given constraints, we have used nested for loops.

  • We check the first constraint if i < j < k by setting the starting index of the second and third loop to i+1 and j+1 respectively.
  • Then we check the other constraint: arr[i] < arr[j] && arr[j] < arr[k] using the if condition.
  • If both constraints are satisfied then we calculate the triplet sum.
  • By using the max() function we compare maxSum and current triplet sum and return the maximum triplet sum of the array.

Example

The following example implements the above steps to calculate the maximum sum of triplet in the given array with the given constraints using nested for loops:

#include <iostream>
using namespace std;

int tripletMaxSum(int arr[], int n)
{
   int maxSum = 0;
   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 (arr[i] < arr[j] && arr[j] < arr[k])
            {
               maxSum = max(maxSum, arr[i] + arr[j] + arr[k]);
            }
         }
      }
   }
   return maxSum;
}

int main()
{
   int arr[] = {1, 101, 2, 3, 100};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "Given array is: ";
   for (int i = 0; i < n; i++)
   {
      cout << arr[i] << " ";
   }
   cout << "\nMaximum triplet sum is: " << tripletMaxSum(arr, n);
}

The output of the above code is as follows:

Given array is: 1 101 2 3 100 
Maximum triplet sum is: 105

Using Optimized Brute Force

In this approach, we have used an optimized brute force approach with a single outer loop.

  • The outer for loop selects the middle element of the triplet.
  • The first inner for loop iterates elements to the left of the middle element and selects elements less than the middle element of the triplet.
  • The second inner for loop iterates elements to the right of the middle element and selects elements greater than the middle element of the triplet.
  • After selecting the valid left and right elements for the selected middle element, we calculate the sum of the triplet and store it in maxSum.
  • We compare the maxSum value with the sum obtained in each iteration and update its value using the max() function and then maxSum is returned.

Example

Here is an example of an optimized brute force technique to calculate the triplet max sum with given constraints:

#include <iostream>
#include <algorithm>
using namespace std;

int tripletMaxSum(int arr[], int n)
{
   int maxSum = 0;
   for (int j = 1; j < n - 1; j++)
   {
      int left = 0;
      for (int i = 0; i < j; i++)
         if (arr[i] < arr[j])
            left = max(left, arr[i]);
      int right = 0;
      for (int k = j + 1; k < n; k++)
         if (arr[k] > arr[j])
            right = max(right, arr[k]);
      if (left && right)
         maxSum = max(maxSum, left + arr[j] + right);
   }
   return maxSum;
}

int main()
{
   int arr[] = {1, 101, 2, 3, 100};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "Given array is: ";
   for (int i = 0; i < n; i++)
   {
      cout << arr[i] << " ";
   }
   cout << "\nMaximum triplet sum is: " << tripletMaxSum(arr, n);
}

The output of the above code is as follows:

Given array is: 1 101 2 3 100 
Maximum triplet sum is: 105

Using Set and Vector

The following approach uses a vector and set to calculate the maximum sum of the triplet with given constraints:

  • First, we create a rightMax array using a vector to store the maximum element from the current index to the end of the array. Example: for the arr = {1, 101, 2, 3, 100}, the rightMax = {101, 101, 100, 100, 100}.
  • Then we iterate over the given array using a for loop from the second element to the second last element for searching the valid middle elements for the triplets. This is checked using rightMax[j + 1] >= arr[j].
  • Example: arr[2] and arr[3] are valid middle elements as rightMax[3] > arr[2] and rightMax[4] > arr[3].
  • Now, for each middle element, we check for their valid left elements. The lower_bound() function selects the first element greater than the given middle element and then decreases the counter to get the largest smaller element than the middle element.
  • The set is used for storing the left element and getting the largest smaller element than the middle element.
  • For example: Left to the first middle element(2) is: 1 and 101. Using lower_bound() 101 is selected and by itr--, 1 is selected. Similarly, this process is repeated till we get {2, 3, 100}.

Example

In this example, we have used a vector and set for calculating the maximum increasing triplet sum:

#include <iostream>
#include <set>
#include <vector>
using namespace std;

int tripletMaxSum(int arr[], int n)
{
   vector<int> rightMax(n);
   rightMax[n - 1] = arr[n - 1];

   for (int i = n - 2; i >= 0; i--)
   {
      rightMax[i] = max(arr[i], rightMax[i + 1]);
   }

   set<int> left;
   left.insert(arr[0]);
   int maxSum = 0;

   for (int j = 1; j < n - 1; j++)
   {
      if (rightMax[j + 1] <= arr[j])
         continue;

      auto itr = left.lower_bound(arr[j]);
      if (itr != left.begin())
      {
         --itr;
         int left = *itr;
         maxSum = max(maxSum, left + arr[j] + rightMax[j + 1]);
      }
      left.insert(arr[j]);
   }

   return maxSum;
}

int main()
{
   int arr[] = {1, 101, 2, 3, 100};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "Given array is: ";
   for (int i = 0; i < n; i++)
   {
      cout << arr[i] << " ";
   }
   cout << "\nMaximum triplet sum is: " << tripletMaxSum(arr, n);
}

The output of the above code is as follows:

Given array is: 1 101 2 3 100 
Maximum triplet sum is: 105

Complexity Comparison

Here is a comparison of the time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Using Nested for Loop O(n^3) O(1)
Using Optimized Brute Force O(n^2) O(1)
Using Set and Vector O(n logn) O(n)
Updated on: 2025-07-01T15:35:10+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements