Minimum product subarray of size K including negative integers
Last Updated :
14 Dec, 2021
Given an array arr[] of length N, the task is to find the minimum product of subarray of size K of an array including negative integers.
Example:
Input: arr = [2, 3, -1, -5, 4, 0], K = 3
Output: -6
Explanation: The product of the subarray {2, 3, -1} is -6 which is the minimum possible.
Input: arr = [-2, -4, 0, 1, 5, -6, 9], K =4
Output: -270
Explanation: The product of the subarray {1, 5, -6, 9} is -270 which is the minimum possible.
If the array consists of only positive numbers the problem can be efficiently solved using only the sliding window technique as discussed here.
Approach: The given problem can be solved using the two-pointers technique and the sliding window approach. Below steps can be followed to solve the problem:
- Iterate the array arr till K – 1 and multiply every non-zero number to find the product and also count the number of zeros in the subarray.
- Iterate the array arr from K till the end of the array and at every iteration:
- If the current element is not a zero then multiply it to the product or else increment the count of zeros
- If the element to the left of the current sliding window is not a zero then divide the product by that element or else reduce the count of zeros
- If the number of zeros in the sliding window is 0 then update the result with the current product or else update it with zero
- Return the result res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minProdK(vector< int >& arr, int K)
{
int prod = 1, zeros = 0;
int N = arr.size();
for ( int i = 0; i < K; i++) {
if (arr[i] == 0)
zeros++;
else
prod *= arr[i];
}
int res = zeros == 0 ? prod : 0;
for ( int right = K; right < N; right++) {
if (arr[right] == 0)
zeros++;
else
prod *= arr[right];
if (arr[right - K] == 0)
zeros--;
else
prod /= arr[right - K];
if (zeros == 0)
res = min(res, prod);
else if (res > 0)
res = 0;
}
return res;
}
int main()
{
vector< int > arr = { 2, 3, -1, -5, 4, 0 };
int K = 3;
cout << minProdK(arr, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int minProdK(
int arr[], int K)
{
int prod = 1 , zeros = 0 ;
int N = arr.length;
for ( int i = 0 ; i < K; i++) {
if (arr[i] == 0 )
zeros++;
else
prod *= arr[i];
}
int res = zeros == 0 ? prod : 0 ;
for ( int right = K; right < N; right++) {
if (arr[right] == 0 )
zeros++;
else
prod *= arr[right];
if (arr[right - K] == 0 )
zeros--;
else
prod /= arr[right - K];
if (zeros == 0 )
res = Math.min(res, prod);
else if (res > 0 )
res = 0 ;
}
return res;
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , - 1 , - 5 , 4 , 0 };
int K = 3 ;
System.out.println(minProdK(arr, K));
}
}
|
Python3
def minProdK(arr, K):
prod = 1
zeros = 0
N = len (arr)
for i in range (K):
if (arr[i] = = 0 ):
zeros + = 1
else :
prod * = arr[i]
if zeros = = 0 :
res = prod
else :
res = 0
for right in range (K, N):
if (arr[right] = = 0 ):
zeros + = 1
else :
prod * = arr[right]
if (arr[right - K] = = 0 ):
zeros - = 1
else :
prod / / = arr[right - K]
if (zeros = = 0 ):
res = min (res, prod)
elif (res > 0 ):
res = 0
return res
if __name__ = = "__main__" :
arr = [ 2 , 3 , - 1 , - 5 , 4 , 0 ]
K = 3
print (minProdK(arr, K))
|
C#
using System;
class GFG
{
public static int minProdK(
int []arr, int K)
{
int prod = 1, zeros = 0;
int N = arr.Length;
for ( int i = 0; i < K; i++) {
if (arr[i] == 0)
zeros++;
else
prod *= arr[i];
}
int res = zeros == 0 ? prod : 0;
for ( int right = K; right < N; right++) {
if (arr[right] == 0)
zeros++;
else
prod *= arr[right];
if (arr[right - K] == 0)
zeros--;
else
prod /= arr[right - K];
if (zeros == 0)
res = Math.Min(res, prod);
else if (res > 0)
res = 0;
}
return res;
}
public static void Main()
{
int [] arr = { 2, 3, -1, -5, 4, 0 };
int K = 3;
Console.Write(minProdK(arr, K));
}
}
|
Javascript
<script>
function minProdK(arr, K) {
let prod = 1, zeros = 0;
let N = arr.length;
for (let i = 0; i < K; i++) {
if (arr[i] == 0)
zeros++;
else
prod *= arr[i];
}
let res = zeros == 0 ? prod : 0;
for (let right = K; right < N; right++) {
if (arr[right] == 0)
zeros++;
else
prod *= arr[right];
if (arr[right - K] == 0)
zeros--;
else
prod /= arr[right - K];
if (zeros == 0)
res = Math.min(res, prod);
else if (res > 0)
res = 0;
}
return res;
}
let arr = [2, 3, -1, -5, 4, 0];
let K = 3;
document.write(minProdK(arr, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...