📜  最大子阵列乘积模 M

📅  最后修改于: 2021-09-06 06:37:49             🧑  作者: Mango

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

例子:

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

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

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

  • 初始化两个变量,比如 ans 和 length,以存储最大子数组乘积和最大乘积子数组的最小长度。
  • 迭代范围 [0, N – 1] 并执行以下步骤:
    • 初始化一个变量,比如 product,来存储子数组 {arr[i], …, arr[j]} 的乘积。
    • 迭代范围 [i, N-1] 并通过乘以 arr[j] 来更新乘积,即 (product * arr[j]) % M。
    • 在每次迭代中,更新 ans if ans < product 然后更新 length, if length > (j – i + 1)。
  • 最后,打印 ans 中获得的最大子数组乘积和具有最大乘积的子数组的最小长度,length。

下面是上述方法的实现:

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


Javascript


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

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live