Open In App

Minimum sum of absolute differences between pairs of a triplet from an array

Last Updated : 09 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: 1
Explanation:
For x = 0, y = 1, z = 2
|A[x] – A[y]| + |A[y] – A[z]| = 0 + 1 = 1, which is maximum possible

Input : A[] = { 1, 1, 1 }
Output : 0

 

Approach : The problem can be solved greedily. Follow the steps below to solve the problem:

  1. Traverse the array.
  2. Sort the array in ascending order.
  3. Traverse the array using a variable i over indices [0, N – 3]. For every ith index, set x = i, y = i + 1, z = i + 2
  4. Calculate the sum of the triplet (x, y, z).
  5. Update the minimum sum possible.
  6. Print the minimum sum obtained.

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 minimum
// sum of absolute differences
// of pairs of a triplet
int minimum_sum(int A[], int N)
{
    // Sort the array
    sort(A, A + N);
 
    // Stores the minimum sum
    int sum = INT_MAX;
 
    // Traverse the array
    for (int i = 0; i <= N - 3; i++) {
 
        // Update the minimum sum
        sum = min(sum,
                  abs(A[i] - A[i + 1]) +
                  abs(A[i + 1] - A[i + 2]));
    }
 
    // Print the minimum sum
    cout << sum;
}
 
// Driver Code
int main()
{
 
    // Input
    int A[] = { 1, 1, 2, 3 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function call to find minimum
    // sum of absolute differences
    // of pairs in a triplet
    minimum_sum(A, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
class GFG
{
   
// Function to find minimum
// sum of absolute differences
// of pairs of a triplet
static int minimum_sum(int []A, int N)
{
   
    // Sort the array
    Arrays.sort(A);
 
    // Stores the minimum sum
 
    int sum = 2147483647;
 
    // Traverse the array
    for (int i = 0; i <= N - 3; i++) {
 
        // Update the minimum sum
        sum = Math.min(sum,Math.abs(A[i] - A[i + 1]) + Math.abs(A[i + 1] - A[i + 2]));
    }
 
    // Print the minimum sum
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Input
    int []A = { 1, 1, 2, 3 };
    int N = A.length;
 
    // Function call to find minimum
    // sum of absolute differences
    // of pairs in a triplet
    System.out.print(minimum_sum(A, N));
}
}
 
// This code is contributed by splevel62.


Python3




# Python 3 Program for the above approach
import sys
 
# Function to find minimum
# sum of absolute differences
# of pairs of a triplet
def minimum_sum(A, N):
   
    # Sort the array
    A.sort(reverse = False)
 
    # Stores the minimum sum
    sum = sys.maxsize
 
    # Traverse the array
    for i in range(N - 2):
       
        # Update the minimum sum
        sum = min(sum, abs(A[i] - A[i + 1]) + abs(A[i + 1] - A[i + 2]))
 
    # Print the minimum sum
    print(sum)
 
# Driver Code
if __name__ == '__main__':
   
    # Input
    A = [1, 1, 2, 3]
    N = len(A)
 
    # Function call to find minimum
    # sum of absolute differences
    # of pairs in a triplet
    minimum_sum(A, N)
     
    # This code is contributed by ipg2016107


C#




// C# Program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
    
// Function to find minimum
// sum of absolute differences
// of pairs of a triplet
static int minimum_sum(int []A, int N)
{
   
    // Sort the array
    Array.Sort(A);
 
    // Stores the minimum sum
 
    int sum = 2147483647;
 
    // Traverse the array
    for (int i = 0; i <= N - 3; i++) {
 
        // Update the minimum sum
        sum = Math.Min(sum,Math.Abs(A[i] - A[i + 1]) + Math.Abs(A[i + 1] - A[i + 2]));
    }
 
    // Print the minimum sum
    return sum;
}
 
// Driver Code
public static void Main()
{
 
    // Input
    int []A = { 1, 1, 2, 3 };
    int N = A.Length;
 
    // Function call to find minimum
    // sum of absolute differences
    // of pairs in a triplet
    Console.WriteLine(minimum_sum(A, N));
}
}
 
// This code is contributed by bgangwar59.


Javascript




<script>
 
// Javascript Program for the above approach
 
// Function to find minimum
// sum of absolute differences
// of pairs of a triplet
function minimum_sum( A, N)
{
    // Sort the array
    A.sort();
 
    // Stores the minimum sum
    var sum = 1000000000;
 
    // Traverse the array
    for (var i = 0; i <= N - 3; i++) {
 
        // Update the minimum sum
        sum = Math.min(sum,
                  Math.abs(A[i] - A[i + 1]) +
                  Math.abs(A[i + 1] - A[i + 2]));
    }
 
    // Print the minimum sum
    document.write(sum);
}
 
// Driver Code
// Input
var A = [ 1, 1, 2, 3 ];
var N = A.length;
// Function call to find minimum
// sum of absolute differences
// of pairs in a triplet
minimum_sum(A, N);
 
 
</script>


Output: 

1

 

Time Complexity : O(N * logN)
Auxiliary Space : O(1)



Similar Reads

Maximum sum of absolute differences between distinct pairs of a triplet from an array
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[
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:The triplet (a[0], b[0], c[1]), i.e. (1, 2, 4) has
11 min read
Minimum and Maximum sum of absolute differences of pairs
Given an array of N integers where N is even, find the minimum and maximum sum of absolute difference of N/2 pairs formed by pairing every element with one other element. Examples: Input: a[] = {10, -10, 20, -40} Output: min_sum = 40, max_sum = 80 Explanation: Pairs selected for minimum sum (-10, -40) and (10, 20) min_sum = |-10 - -40| + |20 - 10|
8 min read
Sequence with sum K and minimum sum of absolute differences between consecutive elements
Given two integers N and K, the task is to find a sequence of integers of length N such that the sum of all the elements of the sequence is K and the sum of absolute differences between all consecutive elements is minimum. Print this minimized sum. Examples: Input: N = 3, K = 56 Output: 1 The sequence is {19, 19, 18} and the sum of absolute differe
4 min read
Minimum number of swaps required to minimize sum of absolute differences between adjacent array elements
Given an array arr[] consisting of N distinct positive integers, the task is to find the minimum number of elements required to be swapped to minimize the sum of absolute difference of each pair of adjacent elements. Examples: Input: arr[] = {8, 50, 11, 2}Output: 2Explanation:Operation 1: Swapping of elements 8 and 2, modifies the array arr[] to {2
15 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[] = {1, 2, 3, 4, 5, 7, 9, 11, 14} Output: 188 A sim
11 min read
Sum of absolute differences of pairs from the given array that satisfy the given condition
Given an array arr[] of N elements, the task is to find the sum of absolute differences between all pairs (arr[i], arr[j]) such that i &lt; j and (j - i) is prime.Example: Input: arr[] = {1, 2, 3, 5, 7, 12} Output: 45 All valid index pairs are: (5, 0) -&gt; abs(12 - 1) = 11 (3, 0) -&gt; abs(5 - 1) = 4 (2, 0) -&gt; abs(3 - 1) = 2 (4, 1) -&gt; abs(7
12 min read
Find the array element having minimum sum of absolute differences with all other array elements
Given an array arr[] of size N, the task is to find the minimum sum of absolute differences of an array element with all elements of another array. Input: arr[ ] = {1, 2, 3, 4, 5}, N = 5Output: 3Explanation: For arr[0](= 1): Sum = abs(2 - 1) + abs(3 - 1) + abs(4 - 1) + abs(5 - 1) = 10.For arr[1](= 2): Sum = abs(2 - 1) + abs(3 - 2) + abs(4 - 2) + ab
4 min read
Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1
Given an array arr[] ( 1-based indexing ) consisting of N integers, the task is to find the minimum sum of the absolute difference between all pairs of array elements by decrementing and incrementing any pair of elements by 1 any number of times. Examples: Input: arr[] = {1, 2, 3}Output: 0Explanation:Modify the array elements by performing the foll
5 min read
Array element with minimum sum of absolute differences | Set 2
Given an array arr[] consisting of N positive integers, the task is to find an array element X such that sum of its absolute differences with every array element is minimum. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Explanation: For element arr[0](= 1): |(1 - 1)| + |(2 - 1)| + |(3 - 1)| + |(4 - 1)| + |(5 - 1)| = 0 + 1 + 2 + 3 + 4 = 10.For el
7 min read