Minimize difference between maximum and minimum element of all possible subarrays (original) (raw)
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 = 3Input: 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
`
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 sum of differences between maximum and minimum elements present in K subsets Given an array arr[] of size N and an integer K, the task is to minimize the sum of the difference between the maximum and minimum element of each subset by splitting the array into K subsets such that each subset consists of unique array elements only. Examples: Input: arr[] = { 6, 3, 8, 1, 3, 1, 2 12 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
- Minimum distance between the maximum and minimum element of a given Array Given an array A[] consisting of N elements, the task is to find the minimum distance between the minimum and the maximum element of the array.Examples: Input: arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 8, 2} Output: 3 Explanation: The minimum element(= 1) is present at indices {2, 4} The maximum elemen 8 min read
- Split array into K Subarrays to minimize sum of difference between min and max Given a sorted array arr[] of size N and integer K, the task is to split the array into K non-empty subarrays such that the sum of the difference between the maximum element and the minimum element of each subarray is minimized. Note: Every element of the array must be included in one subarray and e 6 min read
- Sum of minimum and maximum elements of all subarrays of size k. Given an array of both positive and negative integers, the task is to compute sum of minimum and maximum elements of all sub-array of size k.Examples: Input : arr[] = {2, 5, -1, 7, -3, -1, -2} K = 4Output : 18Explanation : Subarrays of size 4 are : {2, 5, -1, 7}, min + max = -1 + 7 = 6 {5, -1, 7, -3 15+ min read
- Minimum common element in subarrays of all possible lengths Given an array arr[] consisting of N integers from the range [1, N]( repetition allowed ), the task is to find the minimum common element for each possible subarray length. If no such element exists for any particular length of the subarray, then print -1. Examples: Input: arr[] = {1, 3, 4, 5, 6, 7} 11 min read