Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Easy
  • Last Updated : 09 Jul, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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)




My Personal Notes arrow_drop_up
Recommended Articles
Page :