📜  最大子阵列乘积M

📅  最后修改于: 2021-05-17 21:14:26             🧑  作者: Mango

给定一个大小为N的数组arr []和一个正整数M ,任务是找到模M的最大子数组乘积和最大乘积子数组的最小长度。

例子:

天真的方法:最简单的方法是生成所有可能的子阵列,并为每个子阵列计算其乘积M的模,并打印最大子阵列乘积和此类子阵列的最小长度。

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

高效方法:可以通过将arr [j]乘以[i,j – 1]范围内的子数组的乘积来计算[i,j]范围内的子数组的乘积来优化上述方法。请按照以下步骤解决问题:

  • 初始化两个变量,例如ans和length,以存储最大子阵列乘积和最大乘积子阵列的最小长度。
  • 迭代范围[0,N – 1]并执行以下步骤:
    • 初始化一个变量,例如乘积,以存储子数组{arr [i],…,arr [j]}的乘积。
    • 在[i,N-1]范围内进行迭代,并将乘积乘以arr [j]来更新乘积,即(乘积* arr [j])%M。
    • 在每次迭代中,如果ans (j – i + 1),则更新长度。
  • 最后,打印以ans形式获得的最大子阵列乘积,以及具有最大乘积长度的子阵列的最小长度。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to find maximum subarray product
// modulo M and minimum length of the subarray
void maxModProdSubarr(int arr[], int n, int M)
{
 
    // Stores maximum subarray product modulo
    // M and minimum length of the subarray
    int ans = 0;
 
    // Stores the minimum length of
    // subarray having maximum product
    int length = n;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // Stores the product of a subarray
        int product = 1;
 
        // Calculate Subarray whose start
        // index is i
        for (int j = i; j < n; j++) {
 
            // Multiply product by arr[i]
            product = (product * arr[i]) % M;
 
            // If product greater than ans
            if (product > ans) {
 
                // Update ans
                ans = product;
                if (length > j - i + 1) {
 
                    // Update length
                    length = j - i + 1;
                }
            }
        }
    }
 
    // Print maximum subarray product mod M
    cout << "Maximum subarray product is "
         << ans << endl;
 
    // Print minimum length of subarray
    // having maximum product
    cout << "Minimum length of the maximum product "
         << "subarray is " << length << endl;
}
 
// Drivers Code
int main()
{
    int arr[] = { 2, 3, 4, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int M = 5;
 
    maxModProdSubarr(arr, N, M);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find maximum subarray product
// modulo M and minimum length of the subarray
static void maxModProdSubarr(int arr[], int n, int M)
{
     
    // Stores maximum subarray product modulo
    // M and minimum length of the subarray
    int ans = 0;
 
    // Stores the minimum length of
    // subarray having maximum product
    int length = n;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // Stores the product of a subarray
        int product = 1;
 
        // Calculate Subarray whose start
        // index is i
        for(int j = i; j < n; j++)
        {
             
            // Multiply product by arr[i]
            product = (product * arr[i]) % M;
 
            // If product greater than ans
            if (product > ans)
            {
                 
                // Update ans
                ans = product;
                 
                if (length > j - i + 1)
                {
                     
                    // Update length
                    length = j - i + 1;
                }
            }
        }
    }
 
    // Print maximum subarray product mod M
    System.out.println(
        "Maximum subarray product is " + ans);
 
    // Print minimum length of subarray
    // having maximum product
    System.out.println(
        "Minimum length of the maximum " +
        "product subarray is " + length);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 4, 2 };
    int N = arr.length;
    int M = 5;
 
    maxModProdSubarr(arr, N, M);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for above approach
 
# Function to find maximum subarray product
# modulo M and minimum length of the subarray
def maxModProdSubarr(arr, n, M):
   
    # Stores maximum subarray product modulo
    # M and minimum length of the subarray
    ans = 0
 
    # Stores the minimum length of
    # subarray having maximum product
    length = n
 
    # Traverse the array
    for i in range(n):
       
        # Stores the product of a subarray
        product = 1
 
        # Calculate Subarray whose start
        # index is i
        for j in range(i, n, 1):
           
            # Multiply product by arr[i]
            product = (product * arr[i]) % M
 
            # If product greater than ans
            if (product > ans):
               
                # Update ans
                ans = product
                if (length > j - i + 1):
                   
                    # Update length
                    length = j - i + 1
 
    # Print maximum subarray product mod M
    print("Maximum subarray product is", ans)
 
    # Print minimum length of subarray
    # having maximum product
    print("Minimum length of the maximum product subarray is",length)
 
# Drivers Code
if __name__ == '__main__':
    arr =  [2, 3, 4, 2]
    N = len(arr)
    M = 5
    maxModProdSubarr(arr, N, M)
 
    # This code is contributed by ipg2016107.


C#
// C# program for above approach
using System;
 
class GFG{
 
// Function to find maximum subarray product
// modulo M and minimum length of the subarray
static void maxModProdSubarr(int[] arr, int n,
                             int M)
{
     
    // Stores maximum subarray product modulo
    // M and minimum length of the subarray
    int ans = 0;
 
    // Stores the minimum length of
    // subarray having maximum product
    int length = n;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // Stores the product of a subarray
        int product = 1;
 
        // Calculate Subarray whose start
        // index is i
        for(int j = i; j < n; j++)
        {
             
            // Multiply product by arr[i]
            product = (product * arr[i]) % M;
 
            // If product greater than ans
            if (product > ans)
            {
                 
                // Update ans
                ans = product;
                 
                if (length > j - i + 1)
                {
                     
                    // Update length
                    length = j - i + 1;
                }
            }
        }
    }
 
    // Print maximum subarray product mod M
    Console.WriteLine(
        "Maximum subarray product is " + ans);
 
    // Print minimum length of subarray
    // having maximum product
    Console.WriteLine(
        "Minimum length of the maximum " +
        "product subarray is " + length);
}
 
// Driver code
static void Main()
{
    int[] arr = { 2, 3, 4, 2 };
    int N = arr.Length;
    int M = 5;
 
    maxModProdSubarr(arr, N, M);
}
}
 
// This code is contributed by code_hunt


输出:
Maximum subarray product is 4
Minimum length of the maximum product subarray is 1

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