📌  相关文章
📜  所有可能子数组的最大和最小元素的最小乘积

📅  最后修改于: 2022-05-13 01:56:06.949000             🧑  作者: Mango

所有可能子数组的最大和最小元素的最小乘积

给定一个由N个正整数组成的数组arr[] ,任务是在所有可能的子数组中找到最大值和最小值的最小乘积。

例子:

朴素方法:解决给定问题的最简单方法是生成数组的所有可能子数组,并找到所有可能子数组的最大值和最小值的乘积。检查所有子数组后,打印获得的所有产品的最小值。

时间复杂度: O(N 2 )
辅助空间: O(N)

有效方法:上述方法也可以通过使用将相邻元素对视为子数组的观察来优化,因为包含任何数组元素可能会增加我们的最大元素,从而导致最大乘积。

因此,这个想法是找到所有相邻元素对的乘积的最小值,以找到合成的最小乘积。

以下是上述方法的实现:

C++
//  C++ program for the above approach
#include 
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


输出:
4

时间复杂度: O(N)
辅助空间: O(1)