
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum triplet sum in array in C++
In this article, we are given an array of integers. Our task is to write a program to calculate the maximum triplet sum in the given array, i.e., find the set of three elements whose sum is maximum.
Input Output Scenario
Consider the following input and output scenario where we have calculated the sum of each triplet and then returned the maximum sum of the triplet in the array:
Input: arr = {4, 6, 1, 2} Output: Maximum triplet sum: 12
Here is an explanation of the above example:
All triplets are: (4, 6, 1) = 4+6+1 = 11 (4, 6, 2) = 4+6+1 = 12 (4, 1, 2) = 4+6+1 = 7 (6, 1, 2) = 4+6+1 = 9 The maximum triplet sum is 12
The approaches to calculate the maximum triplet sum of the array are given below:
Finding Maximum Triplet Sum in Array Using for Loop
This is the brute force approach where we use three for loops to iterate over each triplet and find their sum. The sum of every triplet is compared with the maxSum that stores the current maximum sum value. After comparing the value of each triplet the maxSum is returned containing the maximum triplet sum.
Example
The following example calculates the maximum sum of the triplets for the given array:
#include <iostream> using namespace std; int maxSum(int arr[], int n) { int maxSum = 0; int i, j, k; for (i = 0; i < n; i++) for (j = i + 1; j < n; j++) for (k = j + 1; k < n; k++) if (maxSum < arr[i] + arr[j] + arr[k]) maxSum = arr[i] + arr[j] + arr[k]; return maxSum; } int main() { int arr[] = {3, 5, 7, 1, 9, 0}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array is: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << "\nThe maximum triplet sum of the array is: " << maxSum(arr, n); return 0; }
The output of the above code is as follows:
Given array is: 3 5 7 1 9 0 The maximum triplet sum of the array is: 21
Finding Maximum Triplet Sum in Array Using Sorting
In this approach, first, we have sorted the given array in ascending order using the sort() function. Then, we calculated the sum of the last three elements in ascending order since the last three elements will be the maximum three elements of the array and then returned the sum.
Example
Here is an example of calculating the maximum triplet sum using sorting:
#include <iostream> #include <algorithm> using namespace std; int maxSum(int arr[], int n) { sort(arr, arr + n); int sum = arr[n - 1] + arr[n - 2] + arr[n - 3]; return sum; } int main() { int arr[] = {3, 5, 7, 1, 9, 0}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array is: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << "\nThe maximum triplet sum of the array is: " << maxSum(arr, n); return 0; }
The output of the above code is as follows:
Given array is: 3 5 7 1 9 0 The maximum triplet sum of the array is: 21
Finding Maximum Triplet Sum in Array Efficient Approach
In this approach we use a for loop to iterate over the array and find three maximum elements to calculate their sum:
- We have defined three variables: max, secondMax, and thirdMax that store the three maximum elements of the array. It is initialized with INT_MIN initially.
- Then, we iterate over the array using a for loop and compare the array elements with max, secondMax, and thirdMax.
- If the array element is greater than the max, then we store that element in max and swap the max and secondMax values in secondMax and thirdMax respectively.
- Here is an example for the above step: a[i] = 9, max = 7, secondMax = 5, thirdMax = 3. After swapping the values, max = 9, secondMax = 7, thirdMax = 5.
- If the array element is greater than the secondMax, then we store that element in secondMax and swap the thirdMax with the previous secondMax.
- If the array element is greater than the thirdMax, then we store that element in thirdMax.
- Then we return the sum of max, secondMax, and thirdMax.
Example
In this example, we have used the above steps for calculating the maximum triplet sum:
#include <iostream> #include <climits> using namespace std; int maxSum(int arr[], int n) { int max = INT_MIN, secondMax = INT_MIN, thirdMax = INT_MIN; for (int i = 0; i < n; i++) { if (arr[i] > max) { thirdMax = secondMax; secondMax = max; max = arr[i]; } else if (arr[i] > secondMax) { thirdMax = secondMax; secondMax = arr[i]; } else if (arr[i] > thirdMax) { thirdMax = arr[i]; } } return max + secondMax + thirdMax; } int main() { int arr[] = {3, 5, 7, 1, 9, 0}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array is: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << "\nThe maximum triplet sum of the array is: " << maxSum(arr, n); return 0; }
The output of the above code is as follows:
Given array is: 3 5 7 1 9 0 The maximum triplet sum of the array is: 21
Complexity Comparison
Here is a comparison of the time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Using for Loop | O(n^3) | O(1) |
Using Sorting | O(n logn) | O(1) |
Efficient Approach | O(n) | O(1) |