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 = 3
Output: 2
Explanation:
All subarrays of length = 3 are:
{3, 2, 4} where maximum element = 4 and minimum element = 2 difference = 2
{2, 4, 5} where maximum element = 5 and minimum element = 2 difference = 3
{4, 5, 6} where maximum element = 6 and minimum element = 4 difference = 2
{5, 6, 1} where maximum element = 6 and minimum element = 1 difference = 5
{6, 1, 9} where maximum element = 9 and minimum element = 6 difference = 3
Out of these minimum is 2.
Input: arr[] = { 1, 2, 3, 3, 2, 2 } Y = 4
Output: 1
Explanation:
All subarrays of length = 4 are:
{1, 2, 3, 3} maximum element = 3 and minimum element = 1 difference = 2
{2, 3, 3, 2} maximum element = 3 and minimum element = 2 difference = 1
{3, 3, 2, 2} maximum element = 3 and minimum element = 2 difference = 1
Out of these minimum is 1.
Naive Approach: The naive idea is to traverse for every index i in the range [0, N – Y] use another loop to traverse from ith index to (i + Y – 1)th index and then calculate the minimum and maximum element in a subarray of size Y and hence calculate the difference of maximum and minimum element for that ith iteration. Finally, by keeping a check on differences, evaluate the minimum difference.
Time Complexity: O(N*Y)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use the concept of the approach discussed in the Next Greater Element article. Below are the steps:
- Build two arrays maxarr[] and minarr[], where maxarr[] will store the index of the element which is next greater to the element at ith index and minarr[] will store the index of next elements which is less than the element at the ith index.
- Initialize a stack with 0 to store the indices in both the above cases.
- For each index, i in the range [0, N – Y], using a nested loop and sliding window approach form two arrays submax and submin. These arrays will store maximum and minimum elements in subarray in ith iteration.
- Finally, calculate the minimum difference.
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 maximum of all // the subarrays of size Y vector< int > get_submaxarr( int * arr, int n, int y) { int j = 0; stack< int > stk; // ith index of maxarr array // will be the index upto which // Arr[i] is maximum vector< int > maxarr(n); stk.push(0); for ( int i = 1; i < n; i++) { // Stack is used to find the // next larger element and // keeps track of index of // current iteration while (stk.empty() == false and arr[i] > arr[stk.top()]) { maxarr[stk.top()] = i - 1; stk.pop(); } stk.push(i); } // Loop for remaining indexes while (!stk.empty()) { maxarr[stk.top()] = n - 1; stk.pop(); } vector< int > submax; for ( int i = 0; i <= n - y; i++) { // j < i used to keep track // whether jth element is // inside or outside the window while (maxarr[j] < i + y - 1 or j < i) { j++; } submax.push_back(arr[j]); } // Return submax return submax; } // Function to get the minimum for // all subarrays of size Y vector< int > get_subminarr( int * arr, int n, int y) { int j = 0; stack< int > stk; // ith index of minarr array // will be the index upto which // Arr[i] is minimum vector< int > minarr(n); stk.push(0); for ( int i = 1; i < n; i++) { // Stack is used to find the // next smaller element and // keeping track of index of // current iteration while (stk.empty() == false and arr[i] < arr[stk.top()]) { minarr[stk.top()] = i; stk.pop(); } stk.push(i); } // Loop for remaining indexes while (!stk.empty()) { minarr[stk.top()] = n; stk.pop(); } vector< int > submin; for ( int i = 0; i <= n - y; i++) { // j < i used to keep track // whether jth element is inside // or outside the window while (minarr[j] <= i + y - 1 or j < i) { j++; } submin.push_back(arr[j]); } // Return submin return submin; } // Function to get minimum difference void getMinDifference( int Arr[], int N, int Y) { // Create submin and submax arrays vector< int > submin = get_subminarr(Arr, N, Y); vector< int > submax = get_submaxarr(Arr, N, Y); // Store initial difference int minn = submax[0] - submin[0]; int b = submax.size(); for ( int i = 1; i < b; i++) { // Calculate temporary difference int dif = submax[i] - submin[i]; minn = min(minn, dif); } // Final minimum difference cout << minn << "\n" ; } // Driver Code int main() { // Given array arr[] int arr[] = { 1, 2, 3, 3, 2, 2 }; int N = sizeof arr / sizeof arr[0]; // Given subarray size int Y = 4; // Function Call getMinDifference(arr, N, Y); return 0; } |
Python3
# Python3 program for the above approach # Function to get the maximum of all # the subarrays of size Y def get_submaxarr(arr, n, y): j = 0 stk = [] # ith index of maxarr array # will be the index upto which # Arr[i] is maximum maxarr = [ 0 ] * n stk.append( 0 ) for i in range ( 1 , n): # Stack is used to find the # next larger element and # keeps track of index of # current iteration while ( len (stk) > 0 and arr[i] > arr[stk[ - 1 ]]): maxarr[stk[ - 1 ]] = i - 1 stk.pop() stk.append(i) # Loop for remaining indexes while (stk): maxarr[stk[ - 1 ]] = n - 1 stk.pop() submax = [] for i in range (n - y + 1 ): # j < i used to keep track # whether jth element is # inside or outside the window while (maxarr[j] < i + y - 1 or j < i): j + = 1 submax.append(arr[j]) # Return submax return submax # Function to get the minimum for # all subarrays of size Y def get_subminarr(arr, n, y): j = 0 stk = [] # ith index of minarr array # will be the index upto which # Arr[i] is minimum minarr = [ 0 ] * n stk.append( 0 ) for i in range ( 1 , n): # Stack is used to find the # next smaller element and # keeping track of index of # current iteration while (stk and arr[i] < arr[stk[ - 1 ]]): minarr[stk[ - 1 ]] = i stk.pop() stk.append(i) # Loop for remaining indexes while (stk): minarr[stk[ - 1 ]] = n stk.pop() submin = [] for i in range (n - y + 1 ): # j < i used to keep track # whether jth element is inside # or outside the window while (minarr[j] < = i + y - 1 or j < i): j + = 1 submin.append(arr[j]) # Return submin return submin # Function to get minimum difference def getMinDifference(Arr, N, Y): # Create submin and submax arrays submin = get_subminarr(Arr, N, Y) submax = get_submaxarr(Arr, N, Y) # Store initial difference minn = submax[ 0 ] - submin[ 0 ] b = len (submax) for i in range ( 1 , b): # Calculate temporary difference diff = submax[i] - submin[i] minn = min (minn, diff) # Final minimum difference print (minn) # Driver code # Given array arr[] arr = [ 1 , 2 , 3 , 3 , 2 , 2 ] N = len (arr) # Given subarray size Y = 4 # Function call getMinDifference(arr, N, Y) # This code is contributed by Stuti Pathak |
1
Time Complexity: O(N)
Auxiliary Space: O(N)