Maximum subarray product modulo M
Last Updated :
14 Mar, 2022
Given an array, arr[] of size N and a positive integer M, the task is to find the maximum subarray product modulo M and the minimum length of the maximum product subarray.
Examples:
Input: arr[] = {2, 3, 4, 2}, N = 4, M = 5
Output:
Maximum subarray product is 4
Minimum length of the maximum product subarray is 1
Explanation:
Subarrays of length 1 are {{2}, {3}, {4}, {2}} and their product modulo M(= 5) are {2, 3, 4, 2} respectively.
Subarrays of length 2 are {{2, 3}, {3, 4}, {4, 2}} and the product modulo M(= 5) are {1, 2, 3} respectively.
Subarrays of length 3 are {{2, 3, 4}, {3, 4, 2}} and the product modulo M(= 5) are {4, 4, } respectively.
Subarrays of length 4 is {2, 3, 4, 2} and the product modulo M(= 5) is 3.
Therefore, the maximum subarray product mod M(= 5) is 4 and smallest possible length is 1.
Input: arr[] = {5, 5, 5}, N = 3, M = 7
Output:
Maximum subarray product is 6
Minimum length of the maximum product subarray is 3
Naive Approach: The simplest approach is to generate all possible subarrays and for each subarray, calculate its product modulo M and print the maximum subarray product and the minimum length of such subarray.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by calculating the product of subarray in the range [i, j] by multiplying arr[j] with the precalculated product of subarray in the range [i, j – 1]. Follow the steps below to solve the problem:
- Initialize two variables, say ans and length, to store the maximum subarray product and the minimum length of maximum product subarray.
- Iterate over the range [0, N – 1] and perform the following steps:
- Initialize a variable, say product, to store the product of subarray {arr[i], …, arr[j]}.
- Iterate over the range [i, N-1] and update the product by multiplying it by arr[j], i.e. (product * arr[j]) % M.
- In every iteration, update ans if ans < product and then update length, if length > (j – i + 1).
- Finally, print the maximum subarray product obtained in ans and minimum length of subarray having the maximum product, length.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxModProdSubarr( int arr[], int n, int M)
{
int ans = 0;
int length = n;
for ( int i = 0; i < n; i++) {
int product = 1;
for ( int j = i; j < n; j++) {
product = (product * arr[i]) % M;
if (product > ans) {
ans = product;
if (length > j - i + 1) {
length = j - i + 1;
}
}
}
}
cout << "Maximum subarray product is "
<< ans << endl;
cout << "Minimum length of the maximum product "
<< "subarray is " << length << endl;
}
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
import java.io.*;
import java.util.*;
class GFG{
static void maxModProdSubarr( int arr[], int n, int M)
{
int ans = 0 ;
int length = n;
for ( int i = 0 ; i < n; i++)
{
int product = 1 ;
for ( int j = i; j < n; j++)
{
product = (product * arr[i]) % M;
if (product > ans)
{
ans = product;
if (length > j - i + 1 )
{
length = j - i + 1 ;
}
}
}
}
System.out.println(
"Maximum subarray product is " + ans);
System.out.println(
"Minimum length of the maximum " +
"product subarray is " + length);
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 2 };
int N = arr.length;
int M = 5 ;
maxModProdSubarr(arr, N, M);
}
}
|
Python3
def maxModProdSubarr(arr, n, M):
ans = 0
length = n
for i in range (n):
product = 1
for j in range (i, n, 1 ):
product = (product * arr[i]) % M
if (product > ans):
ans = product
if (length > j - i + 1 ):
length = j - i + 1
print ( "Maximum subarray product is" , ans)
print ( "Minimum length of the maximum product subarray is" ,length)
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 2 ]
N = len (arr)
M = 5
maxModProdSubarr(arr, N, M)
|
C#
using System;
class GFG{
static void maxModProdSubarr( int [] arr, int n,
int M)
{
int ans = 0;
int length = n;
for ( int i = 0; i < n; i++)
{
int product = 1;
for ( int j = i; j < n; j++)
{
product = (product * arr[i]) % M;
if (product > ans)
{
ans = product;
if (length > j - i + 1)
{
length = j - i + 1;
}
}
}
}
Console.WriteLine(
"Maximum subarray product is " + ans);
Console.WriteLine(
"Minimum length of the maximum " +
"product subarray is " + length);
}
static void Main()
{
int [] arr = { 2, 3, 4, 2 };
int N = arr.Length;
int M = 5;
maxModProdSubarr(arr, N, M);
}
}
|
Javascript
<script>
function maxModProdSubarr(arr , n , M)
{
var ans = 0;
var length = n;
for (i = 0; i < n; i++) {
var product = 1;
for (j = i; j < n; j++) {
product = (product * arr[i]) % M;
if (product > ans) {
ans = product;
if (length > j - i + 1) {
length = j - i + 1;
}
}
}
}
document.write( "Maximum subarray product is " + ans+ "<br/>" );
document.write( "Minimum length of the maximum " + "product subarray is " + length);
}
var arr = [ 2, 3, 4, 2 ];
var N = arr.length;
var M = 5;
maxModProdSubarr(arr, N, M);
</script>
|
Output:
Maximum subarray product is 4
Minimum length of the maximum product subarray is 1
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...