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: 8
Explanation:
Consider the subarray {2, 4}, the product of minimum and maximum for this subarray is 2*4 = 8, which is minimum among all possible subarrays.Input: arr[] = {3, 1, 5, 2, 3, 2}
Output: 3
Naive Approach: The simplest approach to solve the given problem is to generate all possible subarrays of the array and find the product of the maximum and minimum of all possible subarrays. After checking for all the subarrays print the minimum of all products obtained.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimize by using an observation that considering the pair of adjacent elements as an subarray because including any array element may increase our maximum element which result in the maximum product.
Therefore, the idea is to find the minimum of product of all pair of adjacent elements to find the resultant minimum product.
Below is the implementation of above approach:
Python3
# Python program for the above approach # Function to find the minimum product# of the minimum and maximum among all# the possible subarraysdef findMinMax(a): # Stores resultant minimum product min_val = 1000000000 # Traverse the given array arr[] for i in range(1, len(a)): # Min of product of all two # pair of consecutive elements min_val = min(min_val, a[i]*a[i-1]) # Return the resultant value return min_val # Driver Codeif __name__ == ("__main__"): arr = [6, 4, 5, 6, 2, 4, 1] print(findMinMax(arr)) |
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


