Maximum sum of absolute differences between distinct pairs of a triplet from an array Last Updated : 01 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array arr[] consisting of N integers, the task is to find the maximum sum of absolute differences between all distinct pairs of the triplet in the array. Examples: Input: arr[] = {1, 2, 3, 4}Output: 6Explanation:The valid triplet is (1, 3, 4) as sum = |1 - 4| + |1 - 3| + |3 - 4| = 6, which is the maximum among all the triplets. Input: arr[] = {2, 2, 2}Output: 0 Approach: The idea to solve the given problem is to sort the array in ascending order and find the sum of absolute differences between the pairs of the first and the last two elements of the array. Follow the steps below to solve the problem: Initialize a variable, say sum, to store the maximum possible sum.Sort the given array arr[] in ascending order.Find the sum of differences between the pairs of the first and the last two elements of the array i.e., sum = (arr[N - 2] - arr[0]) + (arr[N - 1] - arr[0]) + (arr[N - 2] - arr[N - 1]).After completing the above steps, print the value of the sum as the result. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum sum of // absolute differences between // distinct pairs of triplet in array void maximumSum(int arr[], int N) { // Stores the maximum sum int sum; // Sort the array in // ascending order sort(arr, arr + N); // Sum of differences between // pairs of the triplet sum = (arr[N - 1] - arr[0]) + (arr[N - 2] - arr[0]) + (arr[N - 1] - arr[N - 2]); // Print the sum cout << sum; } // Driver Code int main() { int arr[] = { 1, 3, 4, 2 }; int N = sizeof(arr) / sizeof(arr[0]); maximumSum(arr, N); return 0; } Java // Java program for the above approach import java.util.*; class GFG { // Function to find the maximum sum of // absolute differences between // distinct pairs of triplet in array static void maximumSum(int[] arr, int N) { // Stores the maximum sum int sum; // Sort the array in // ascending order Arrays.sort(arr); // Sum of differences between // pairs of the triplet sum = (arr[N - 1] - arr[0]) + (arr[N - 2] - arr[0]) + (arr[N - 1] - arr[N - 2]); // Print the sum System.out.println(sum); } // Driver Code public static void main(String[] args) { int[] arr = { 1, 3, 4, 2 }; int N = arr.length; maximumSum(arr, N); } } // This code is contributed by susmitakundugoaldanga. Python3 # Python program for the above approach # Function to find the maximum sum of # absolute differences between # distinct pairs of triplet in array def maximumSum(arr, N): # Stores the maximum sum sum = 0 # Sort the array in # ascending order arr.sort() # Sum of differences between # pairs of the triplet sum = (arr[N - 1] - arr[0]) + (arr[N - 2] - arr[0]) + (arr[N - 1] - arr[N - 2]); # Print the sum print(sum) # Driver Code arr = [ 1, 3, 4, 2 ] N = len(arr) maximumSum(arr, N) # This code is contributed by rohitsingh07052. C# // C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to find the maximum sum of // absolute differences between // distinct pairs of triplet in array static void maximumSum(int[] arr, int N) { // Stores the maximum sum int sum; // Sort the array in // ascending order Array.Sort(arr); // Sum of differences between // pairs of the triplet sum = (arr[N - 1] - arr[0]) + (arr[N - 2] - arr[0]) + (arr[N - 1] - arr[N - 2]); // Print the sum Console.Write(sum); } // Driver Code public static void Main() { int[] arr = { 1, 3, 4, 2 }; int N = arr.Length; maximumSum(arr, N); } } // This code is contributed by chitranayal. JavaScript <script> // Javascript program for the above approach // Function to find the maximum sum of // absolute differences between // distinct pairs of triplet in array function maximumSum(arr, N) { // Stores the maximum sum let sum; // Sort the array in // ascending order arr.sort(); // Sum of differences between // pairs of the triplet sum = (arr[N - 1] - arr[0]) + (arr[N - 2] - arr[0]) + (arr[N - 1] - arr[N - 2]); // Print the sum document.write(sum); } // Driver Code let arr = [ 1, 3, 4, 2 ]; let N = arr.length; maximumSum(arr, N); // This code is contributed by Mayank Tyagi </script> Output: 6 Time Complexity: O(N*log N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Minimum sum of absolute differences of pairs in a triplet from three arrays D dharanendralv23 Follow Improve Article Tags : Greedy Sorting Technical Scripter DSA Arrays Technical Scripter 2020 +2 More Practice Tags : ArraysGreedySorting Similar Reads Minimum sum of absolute differences between pairs of a triplet from an array Given an array A[] consisting of positive integers, the task is to find the minimum value of |A[x] - A[y]| + |A[y] - A[z]| of any triplet (A[x], A[y], A[z]) from an array. Examples: Input: A[] = { 1, 1, 2, 3 }Output: 1Explanation:For x = 0, y = 1, z = 2|A[x] - A[y]| + |A[y] - A[z]| = 0 + 1 = 1, whic 5 min read Minimum sum of absolute differences of pairs in a triplet from three arrays Given three arrays a[], b[] and c[] of sizes A, B and C respectively, the task is to find the minimum possible value of abs(a[i] - b[j]) + abs(b[j] - c[k]) where 0 ? i ? A, 0 ? j ? B and 0 ? k ? C. Examples: Input: A = 3, B = 2, C = 2, a[] = {1, 8, 5}, b[] = {2, 9}, c[] = {5, 4}Output: 3Explanation: 11 min read Maximum absolute difference between distinct elements in an Array Given an array arr[] of N integers, the task is to find the maximum absolute difference between distinct elements of the array.Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45, 10} Output: 10 Explanation: Distinct elements of given array are 12, 9, 2. Therefore, the maximum absolute difference 6 min read Find maximum absolute difference with min sum of ratios in an Array Given an array of positive integers arr[], the task is to find the maximum absolute difference between the pairs from the array such that the sum of their ratios is minimum. Examples: Input: arr[] = {2, 6, 3, 4, 8, 9}Output: 4Explanation: In the above example, the ratios of every pair are calculated 7 min read Minimum sum of all differences between unique pairs in the Array Given an array arr[] consisting of N integers, the task is to find the minimum sum of all absolute differences between unique pairs of elements in the array after updating the array arr[]. To update the array, any two elements from the array can be chosen in any order. 1 is subtracted from the first 7 min read Sum of absolute differences of all pairs in a given array Given a sorted array of distinct elements, the task is to find the summation of absolute differences of all pairs in the given array. Examples: Input : arr[] = {1, 2, 3, 4} Output: 10 Sum of |2-1| + |3-1| + |4-1| + |3-2| + |4-2| + |4-3| = 10 Input : arr[] = {1, 8, 9, 15, 16} Output: 74 Input : arr[] 11 min read Like