Check if subarray with given product exists in an array
Last Updated :
05 Jul, 2023
Given an array of both positive and negative integers and a number K., The task is to check if any subarray with product K is present in the array or not.
Examples:
Input: arr[] = {-2, -1, 3, -4, 5}, K = 2
Output: YES
Input: arr[] = {3, -1, -1, -1, 5}, K = 3
Output: YES
Approach: The approach is similar to that used in the Maximum Product Subarray, the only task is to simultaneously check that the product is equal to k or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool maxProduct( int * arr, int n, int p)
{
int minVal = arr[0];
int maxVal = arr[0];
int maxProduct = arr[0];
for ( int i = 1; i < n; i++) {
if (arr[i] < 0)
swap(maxVal, minVal);
maxVal = max(arr[i], maxVal * arr[i]);
minVal = min(arr[i], minVal * arr[i]);
if (minVal == p || maxVal == p) {
return true ;
}
maxProduct = max(maxProduct, maxVal);
}
return false ;
}
int main()
{
int arr[] = { 1, 2, -5, -4 };
int product = -10;
int n = sizeof (arr) / sizeof (arr[0]);
if (maxProduct(arr, n, product)) {
cout << "YES" << endl;
}
else
cout << "NO" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean maxProduct( int arr[],
int n, int p)
{
int minVal = arr[ 0 ];
int maxVal = arr[ 0 ];
int maxProduct = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (arr[i] < 0 )
{
int temp = maxVal;
maxVal = minVal;
minVal = temp;
}
maxVal = Math.max(arr[i],
maxVal * arr[i]);
minVal = Math.min(arr[i],
minVal * arr[i]);
if (minVal == p || maxVal == p)
{
return true ;
}
maxProduct = Math.max(maxProduct,
maxVal);
}
return false ;
}
public static void main (String[] args)
{
int []arr = { 1 , 2 , - 5 , - 4 };
int product = - 10 ;
int n = arr.length;
if (maxProduct(arr, n, product))
{
System.out.println( "YES" );
}
else
System.out.println( "NO" );
}
}
|
Python 3
def maxProduct(arr,n, p):
minVal = arr[ 0 ]
maxVal = arr[ 0 ]
maxProduct = arr[ 0 ]
for i in range ( 1 , n):
if (arr[i] < 0 ):
maxVal, minVal = minVal, maxVal
maxVal = max (arr[i], maxVal * arr[i])
minVal = min (arr[i], minVal * arr[i])
if (minVal = = p or maxVal = = p):
return True
maxProduct = max (maxProduct, maxVal)
return False
if __name__ = = "__main__" :
arr = [ 1 , 2 , - 5 , - 4 ]
product = - 10
n = len (arr)
if (maxProduct(arr, n, product)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool maxProduct( int []arr,
int n, int p)
{
int minVal = arr[0];
int maxVal = arr[0];
int maxProduct = arr[0];
for ( int i = 1; i < n; i++)
{
if (arr[i] < 0)
{
int temp = maxVal;
maxVal = minVal;
minVal = temp;
}
maxVal = Math.Max(arr[i],
maxVal * arr[i]);
minVal = Math.Min(arr[i],
minVal * arr[i]);
if (minVal == p || maxVal == p)
{
return true ;
}
maxProduct = Math.Max(maxProduct,
maxVal);
}
return false ;
}
public static void Main ()
{
int []arr = { 1, 2, -5, -4 };
int product = -10;
int n = arr.Length;
if (maxProduct(arr, n, product))
{
Console.WriteLine( "YES" );
}
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function maxProduct(& $arr , $n , $p )
{
$minVal = $arr [0];
$maxVal = $arr [0];
$maxProduct = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
{
if ( $arr [ $i ] < 0)
{
$temp = $maxVal ;
$maxVal = $minVal ;
$minVal = $temp ;
}
$maxVal = max( $arr [ $i ], $maxVal *
$arr [ $i ]);
$minVal = min( $arr [ $i ], $minVal *
$arr [ $i ]);
if ( $minVal == $p || $maxVal == $p )
{
return true;
}
$maxProduct = max( $maxProduct , $maxVal );
}
return false;
}
$arr = array (1, 2, -5, -4 );
$product = -10;
$n = sizeof( $arr );
if (maxProduct( $arr , $n , $product ))
{
echo ( "YES" ) ;
}
else
echo ( "NO" );
?>
|
Javascript
<script>
function maxProduct(arr,n,p)
{
let minVal = arr[0];
let maxVal = arr[0];
let maxProduct = arr[0];
for (let i = 1; i < n; i++)
{
if (arr[i] < 0)
{
let temp = maxVal;
maxVal = minVal;
minVal = temp;
}
maxVal = Math.max(arr[i],
maxVal * arr[i]);
minVal = Math.min(arr[i],
minVal * arr[i]);
if (minVal == p || maxVal == p)
{
return true ;
}
maxProduct = Math.max(maxProduct,
maxVal);
}
return false ;
}
let arr=[1, 2, -5, -4];
let product = -10;
let n = arr.length;
if (maxProduct(arr, n, product))
{
document.write( "YES" );
}
else
document.write( "NO" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Share your thoughts in the comments
Please Login to comment...