Minimum product of maximum and minimum element over all possible subarrays (original) (raw)
Last Updated : 20 Mar, 2023
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(1)
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:
C++ `
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the minimum product // of the minimum and maximum among all // the possible subarrays int findMinMax(vector& a) {
// Stores resultant minimum product
int min_val = 1000000000;
// Traverse the given array arr[]
for (int i = 1; i < a.size(); ++i) {
// 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 Code int main() { vector arr = { 6, 4, 5, 6, 2, 4, 1 };
cout << findMinMax(arr);
return 0;
}
// This code is contributed by rakeshsahni
Java
/*package whatever //do not write package name here / import java.io.;
class GFG {
// Function to find the minimum product // of the minimum and maximum among all // the possible subarrays static int findMinMax(int[] a) {
// Stores resultant minimum product
int min_val = 1000000000;
// Traverse the given array arr[]
for (int i = 1; i < a.length; ++i) {
// Min of product of all two
// pair of consecutive elements
min_val = Math.min(min_val, a[i] * a[i - 1]);
}
// Return the resultant value
return min_val;
}
// Driver Code public static void main (String[] args) { int[] arr = { 6, 4, 5, 6, 2, 4, 1 };
System.out.println(findMinMax(arr));
} }
// This code is contributed by maddler.
Python3
Python program for the above approach
Function to find the minimum product
of the minimum and maximum among all
the possible subarrays
def 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 Code
if name == ("main"):
arr = [6, 4, 5, 6, 2, 4, 1]
print(findMinMax(arr))
C#
// C# program for the above approach using System;
public class GFG {
// Function to find the minimum product // of the minimum and maximum among all // the possible subarrays static int findMinMax(int[] a) {
// Stores resultant minimum product
int min_val = 1000000000;
// Traverse the given array arr[]
for (int i = 1; i < a.Length; ++i) {
// Min of product of all two
// pair of consecutive elements
min_val = Math.Min(min_val, a[i] * a[i - 1]);
}
// Return the resultant value
return min_val;
}
// Driver Code
public static void Main (string[] args)
{
int[] arr = { 6, 4, 5, 6, 2, 4, 1 };
Console.WriteLine(findMinMax(arr));
}
}
// This code is contributed by avijitmondal1998.
JavaScript
`
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
- Minimize difference between maximum and minimum element of all possible subarrays 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: 3Explanation: {7, 10} is the subarray having max element = 10 & min element = 7, and their 5 min read
- Maximum XOR value of maximum and second maximum element among all possible subarrays Given an array arr[] of N distinct positive integers, let's denote max(i, j) and secondMax(i, j) as the maximum and the second maximum element of the subarray arr[i...j]. The task is to find the maximum value of max(i, j) XOR secondMax(i, j) for all possible values of i and j. Note that the size of 5 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
- Minimum and Maximum of all subarrays of size K using Map Given an array arr[] of N integers and an integer K, the task is to find the minimum and maximum of all subarrays of size K. Examples: Input: arr[] = {2, -2, 3, -9, -5, -8}, K = 4 Output: -9 3 -9 3 -9 3 Explanation: Below are the subarray of size 4 and minimum and maximum value of each subarray: 1. 9 min read
- Sum and Product of minimum and maximum element of an Array Given an array. The task is to find the sum and product of the maximum and minimum elements of the given array. Examples: Input : arr[] = {12, 1234, 45, 67, 1} Output : Sum = 1235 Product = 1234 Input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9} Output : Sum = 10 Product = 9 Take two variables min and max to 13 min read
- Minimum common element in all subarrays of size K Given an array arr[] consisting of N distinct integers and a positive integer K, the task is to find the minimum element that occurs in all subarrays of size K. If no such element exists, then print "-1". Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 4Output: 2Explanation:The subarrays of size 4 are 7 min read
- Find sum of difference of maximum and minimum over all possible subsets of size K Given an array arr[] of N integers and an integer K, the task is to find the sum of the difference between the maximum and minimum elements over all possible subsets of size K. Examples: Input: arr[] = {1, 1, 3, 4}, K = 2Output: 11Explanation:There are 6 subsets of the given array of size K(= 2). Th 12 min read
- Find maximum (or minimum) sum of a subarray of size k Given an array of integers and a number k, find the maximum sum of a subarray of size k. Examples: Input : arr[] = {100, 200, 300, 400}, k = 2Output : 700Input : arr[] = {1, 4, 2, 10, 23, 3, 1, 0, 20}, k = 4 Output : 39Explanation: We get maximum sum by adding subarray {4, 2, 10, 23} of size 4.Input 14 min read
- 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