Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmMinimum product of maximum and minimum element over all possible subarrays

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(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<int>& 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<int> 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




   <script>
        // JavaScript Program to implement
        // the above approach
 
//  Function to find the minimum product
//  of the minimum and maximum among all
//  the possible subarrays
function findMinMax( a)
{
 
    // Stores resultant minimum product
    let min_val = 1000000000;
 
    // Traverse the given array arr[]
    for (let 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
 
    let arr = [6, 4, 5, 6, 2, 4, 1];
    document.write( findMinMax(arr))
 
// This code is contributed by Potta Lokesh
    </script>


Output: 

4

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments