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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to get the min difference
// between max and min element of all
// possible subarrays
int getMinDifference(int arr[], int n)
{
// To store the adjacent difference
int diff;
// To compare with min difference
int mn = INT_MAX;
for (int i = 1; i < n; i++) {
// Storing adjacent difference
diff = abs(arr[i] - arr[i - 1]);
// Updating the min difference
mn = min(diff, mn);
}
// Returning min difference
return mn;
}
// Driver code
int main()
{
int arr[] = { 2, 6, 15, 7, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << getMinDifference(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to get the min difference
// between max and min element of all
// possible subarrays
static int getMinDifference(int []arr, int n)
{
// To store the adjacent difference
int diff = 0;
// To compare with min difference
int mn = Integer.MAX_VALUE;
for (int i = 1; i < n; i++) {
// Storing adjacent difference
diff = Math.abs(arr[i] - arr[i - 1]);
// Updating the min difference
mn = Math.min(diff, mn);
}
// Returning min difference
return mn;
}
// Driver code
public static void main (String[] args)
{
int []arr = {2, 6, 15, 7, 6 };
int N = arr.length;
System.out.println(getMinDifference(arr, N));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program for the above approach
import sys,math
# Function to get the min difference
# between max and min element of all
# possible subarrays
def getMinDifference(arr, n) :
INT_MAX = sys.maxsize;
# To compare with min difference
mn = INT_MAX;
for i in range(1, n):
# Storing adjacent difference
diff = abs(arr[i] - arr[i - 1]);
# Updating the min difference
mn = min(diff, mn);
# Returning min difference
return mn;
# Driver code
if __name__ == "__main__" :
arr = [ 2, 6, 15, 7, 6 ];
N = len(arr);
print(getMinDifference(arr, N));
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to get the min difference
// between max and min element of all
// possible subarrays
static int getMinDifference(int []arr, int n)
{
// To store the adjacent difference
int diff = 0;
// To compare with min difference
int mn = Int32.MaxValue;
for (int i = 1; i < n; i++) {
// Storing adjacent difference
diff = Math.Abs(arr[i] - arr[i - 1]);
// Updating the min difference
mn = Math.Min(diff, mn);
}
// Returning min difference
return mn;
}
// Driver code
public static void Main()
{
int []arr = {2, 6, 15, 7, 6 };
int N = arr.Length;
Console.Write(getMinDifference(arr, N));
}
}
// This code is contributed by SURENDRA_GANGWAR.
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to get the min difference
// between max and min element of all
// possible subarrays
function getMinDifference(arr, n) {
// To store the adjacent difference
let diff;
// To compare with min difference
let mn = Number.MAX_VALUE;
for (let i = 1; i < n; i++) {
// Storing adjacent difference
diff = Math.abs(arr[i] - arr[i - 1]);
// Updating the min difference
mn = Math.min(diff, mn);
}
// Returning min difference
return mn;
}
// Driver code
let arr = [2, 6, 15, 7, 6];
let N = arr.length;
document.write(getMinDifference(arr, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N), N is the number of elements
Auxiliary Space: O(1)
Similar Reads
Min difference between maximum and minimum element in all Y size subarrays Given an array arr[] of size N and integer Y, the task is to find a minimum of all the differences between the maximum and minimum elements in all the sub-arrays of size Y. Examples: Input: arr[] = { 3, 2, 4, 5, 6, 1, 9 } Y = 3Output: 2Explanation:All subarrays of length = 3 are:{3, 2, 4} where maxi
15+ min read
Minimize difference between maximum and minimum array elements by removing a K-length subarray Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum difference between the maximum and minimum element present in the array after removing any subarray of size K. Examples: Input: arr[] = {4, 5, 8, 9, 1, 2}, K = 2Output: 4Explanation: Remove the subarray {
10 min read
Minimum product of maximum and minimum element over all possible subarrays Given an array arr[] consisting of N positive integers, the task is to find the minimum product of maximum and minimum among all possible subarrays. Examples: Input: arr[] = {6, 4, 5, 6, 2, 4}Output: 8Explanation:Consider the subarray {2, 4}, the product of minimum and maximum for this subarray is 2
4 min read
Minimize difference between maximum and minimum Subarray sum by splitting Array into 4 parts Given an array arr[] of size N, the task is to find the minimum difference between the maximum and the minimum subarray sum when the given array is divided into 4 non-empty subarrays. Examples: Input: N = 5, arr[] = {3, 2, 4, 1, 2}Output: 2Explanation: Divide the array into four parts as {3}, {2}, {
14 min read
Split a given array into K subarrays minimizing the difference between their maximum and minimum Given a sorted array arr[] of N integers and an integer K, the task is to split the array into K subarrays such that the sum of the difference of maximum and minimum element of each subarray is minimized. Examples: Input: arr[] = {1, 3, 3, 7}, K = 4 Output: 0 Explanation: The given array can be spli
6 min read