Minimize difference between maximum and minimum element of all possible subarrays
Last Updated :
10 Sep, 2021
Given an array arr[ ] of size N, the task is to find the minimum difference between maximum and minimum elements of all possible sized subarrays of arr[ ].
Examples:
Input: arr[] = { 5, 14, 7, 10 }
Output: 3
Explanation: {7, 10} is the subarray having max element = 10 & min element = 7, and their difference = 10 – 7 = 3
Input: arr[] = { 2, 6, 15, 7, 6 }
Output: 1
Explanation: {7, 6} is the subarray having max element = 7 & min element = 6, and their difference = 7 – 6 = 1
Approach: The simple idea is to use two loops and check for every subarray, the minimum difference between the maximum and the minimum element. Keep track of the differences and return the minimum possible difference. Time Complexity for this approach would be Quadratic.
Efficient Approach: The idea is to use the fact that we can get minimum difference by iterating over only the subarrays of size two.
Suppose we have two elements in our subarray. Let the difference between maximum and minimum element be x. Now if we include an element from either the right side or left side to our subarray, the maximum element or minimum element might get updated. This change will ultimately make our difference x increase, as either max or min element is getting updated.
Follow the below steps to implement the above approach:
- Iterate the array and keep track of the minimum adjacent difference
- Print this min difference as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMinDifference( int arr[], int n)
{
int diff;
int mn = INT_MAX;
for ( int i = 1; i < n; i++) {
diff = abs (arr[i] - arr[i - 1]);
mn = min(diff, mn);
}
return mn;
}
int main()
{
int arr[] = { 2, 6, 15, 7, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << getMinDifference(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int getMinDifference( int []arr, int n)
{
int diff = 0 ;
int mn = Integer.MAX_VALUE;
for ( int i = 1 ; i < n; i++) {
diff = Math.abs(arr[i] - arr[i - 1 ]);
mn = Math.min(diff, mn);
}
return mn;
}
public static void main (String[] args)
{
int []arr = { 2 , 6 , 15 , 7 , 6 };
int N = arr.length;
System.out.println(getMinDifference(arr, N));
}
}
|
Python3
import sys,math
def getMinDifference(arr, n) :
INT_MAX = sys.maxsize;
mn = INT_MAX;
for i in range ( 1 , n):
diff = abs (arr[i] - arr[i - 1 ]);
mn = min (diff, mn);
return mn;
if __name__ = = "__main__" :
arr = [ 2 , 6 , 15 , 7 , 6 ];
N = len (arr);
print (getMinDifference(arr, N));
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int getMinDifference( int []arr, int n)
{
int diff = 0;
int mn = Int32.MaxValue;
for ( int i = 1; i < n; i++) {
diff = Math.Abs(arr[i] - arr[i - 1]);
mn = Math.Min(diff, mn);
}
return mn;
}
public static void Main()
{
int []arr = {2, 6, 15, 7, 6 };
int N = arr.Length;
Console.Write(getMinDifference(arr, N));
}
}
|
Javascript
<script>
function getMinDifference(arr, n) {
let diff;
let mn = Number.MAX_VALUE;
for (let i = 1; i < n; i++) {
diff = Math.abs(arr[i] - arr[i - 1]);
mn = Math.min(diff, mn);
}
return mn;
}
let arr = [2, 6, 15, 7, 6];
let N = arr.length;
document.write(getMinDifference(arr, N));
</script>
|
Time Complexity: O(N), N is the number of elements
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...