Given an array A[] consiting 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 possibleInput : A[] = { 1, 1, 1 }
Output : 0
Approach : The problem can be solved greedily. Follow the steps below to solve the problem:
- Traverse the array.
- Sort the array in ascending order.
- 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
- Calculate the sum of the triplet (x, y, z).
- Update the minimum sum possible.
- 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 tripletint minimum_sum(int A[], int N){ // Sort the array sort(A, A + N); // Stores the minuimum 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 Codeint 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 approachimport java.util.*;class GFG{ // Function to find minimum// sum of absolute differences// of pairs of a tripletstatic int minimum_sum(int []A, int N){ // Sort the array Arrays.sort(A); // Stores the minuimum 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 Codepublic 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 approachimport sys# Function to find minimum# sum of absolute differences# of pairs of a tripletdef minimum_sum(A, N): # Sort the array A.sort(reverse = False) # Stores the minuimum 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 Codeif __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 approachusing System;using System.Collections.Generic;class GFG{ // Function to find minimum// sum of absolute differences// of pairs of a tripletstatic int minimum_sum(int []A, int N){ // Sort the array Array.Sort(A); // Stores the minuimum 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 Codepublic 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 tripletfunction minimum_sum( A, N){ // Sort the array A.sort(); // Stores the minuimum 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// Inputvar A = [ 1, 1, 2, 3 ];var N = A.length;// Function call to find minimum// sum of absolute differences// of pairs in a tripletminimum_sum(A, N);</script> |
1
Time Complexity : O(N * logN)
Auxiliary Space : O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


