JavaScript | Array | Maximum triplet sum in Array (easy)

Find the maximum triplet sum in the array

Input : arr[] = [1, 2, 3, 0, -1, 8, 10]
Output : 21
10 + 8 + 3 = 21

Approach,

we simply run three-loop and one by one add three-element and compare with the previous sum if the sum of three-element is greater than store in the previous sum.

function maxTripletSum(arr, n) {    // sort the given array    arr.sort();
// Add last three element of the given array return arr[n - 1] + arr[n - 2] + arr[n - 3];}// input let arr = [ 1, 0, 8, 6, 4, 2 ];let n = arr.length;maxTripletSum(arr, n);// output - 18

Let’s connect on LinkedIn! Please read more for more data structure javascript question and don’t forget to follow me for more such articles.