Given an array of non - 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[] = {1, 2, 3, 4}, K = 6 Output: YES Input: arr[] = {2, 0, 4, 5}, K = 20 Output: YES
Approach: The code provided seeks to determine if an array arr contains a contiguous subarray whose product of elements equals a particular number k. Using a brute-force method, the algorithm calculates the products of every conceivable subarray to see if any of them equal k.
Below is the implementation of the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to check if there exists a subarray with a product equal to k
bool hasSubarrayWithProduct(int* arr, int n, int k) {
// Iterate over all possible starting points of subarrays
for (int start = 0; start < n; ++start) {
int product = 1; // Initialize the product for the current subarray
// Iterate over all possible end points for subarrays starting at 'start'
for (int end = start; end < n; ++end) {
product *= arr[end]; // Update the product for the current subarray
// Check if the current subarray product is equal to k
if (product == k) {
return true; // Return true if such subarray is found
}
}
}
return false; // Return false if no subarray with product k is found
}
int main() {
int arr[] = {1, 2, 3, 4}; // Input array
int product = 6; // Target product value
int n = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
// Check if there is a subarray with the given product and print the result
if (hasSubarrayWithProduct(arr, n, product)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0; // Exit the program
}
public class Main {
// Function to check if there exists a subarray with a product equal to k
public static boolean hasSubarrayWithProduct(int[] arr, int k) {
int n = arr.length;
// Iterate over all possible starting points of subarrays
for (int start = 0; start < n; ++start) {
int product = 1; // Initialize the product for the current subarray
// Iterate over all possible end points for subarrays starting at 'start'
for (int end = start; end < n; ++end) {
product *= arr[end]; // Update the product for the current subarray
// Check if the current subarray product is equal to k
if (product == k) {
return true; // Return true if such subarray is found
}
}
}
return false; // Return false if no subarray with product k is found
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4}; // Input array
int product = 6; // Target product value
// Check if there is a subarray with the given prod1, 2, -5, -4uct and print the result
if (hasSubarrayWithProduct(arr, product)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
def has_subarray_with_product(arr, k):
n = len(arr)
# Iterate over all possible starting points of subarrays
for start in range(n):
product = 1 # Initialize the product for the current subarray
# Iterate over all possible end points for subarrays starting at 'start'
for end in range(start, n):
product *= arr[end] # Update the product for the current subarray
# Check if the current subarray product is equal to k
if product == k:
return True # Return true if such subarray is found
return False # Return false if no subarray with product k is found
# Input array
arr = [1, 2, 3, 4]
# Target product value
product = 6
# Check if there is a subarray with the given product and print the result
if has_subarray_with_product(arr, product):
print("YES")
else:
print("NO")
using System;
public class Program {
// Function to check if there exists a subarray with a product equal to k
public static bool HasSubarrayWithProduct(int[] arr, int k) {
int n = arr.Length;
// Iterate over all possible starting points of subarrays
for (int start = 0; start < n; ++start) {
int product = 1; // Initialize the product for the current subarray
// Iterate over all possible end points for subarrays starting at 'start'
for (int end = start; end < n; ++end) {
product *= arr[end]; // Update the product for the current subarray
// Check if the current subarray product is equal to k
if (product == k) {
return true; // Return true if such subarray is found
}
}
}
return false; // Return false if no subarray with product k is found
}
public static void Main() {
int[] arr = {1, 2, 3, 4}; // Input array
int product = 6; // Target product value
// Check if there is a subarray with the given product and print the result
if (HasSubarrayWithProduct(arr, product)) {
Console.WriteLine("YES");
} else {
Console.WriteLine("NO");
}
}
}
function hasSubarrayWithProduct(arr, k) {
const n = arr.length;
// Iterate over all possible starting points of subarrays
for (let start = 0; start < n; ++start) {
let product = 1; // Initialize the product for the current subarray
// Iterate over all possible end points for subarrays starting at 'start'
for (let end = start; end < n; ++end) {
product *= arr[end]; // Update the product for the current subarray
// Check if the current subarray product is equal to k
if (product === k) {
return true; // Return true if such subarray is found
}
}
}
return false; // Return false if no subarray with product k is found
}
// Input array
const arr = [1, 2, 3, 4];
// Target product value
const product = 6;
// Check if there is a subarray with the given product and print the result
if (hasSubarrayWithProduct(arr, product)) {
console.log("YES");
} else {
console.log("NO");
}
Output
YES
Time Complexity: O(n2)
Auxiliary Space: O(1)
Efficient Approach:
we use the two-pointer technique (sliding window), but adapt it to handle products.
Here is a way to approach it:
- Initialize two pointers, start and end.
- Use a variable product to keep track of the product of elements in the current window.
- Expand the window by moving end to the right and update the product.
- If the product exceeds k, move start to the right until the product is less than or equal to k.
- Check if the product matches k.
Below is the implementation of above idea.
#include <iostream>
using namespace std;
// Function to check if there exists a subarray with a product equal to k
bool hasSubarrayWithProduct(int* arr, int n, int k) {
int start = 0;
int product = 1;
// Special case when k is zero
if (k == 0) {
for (int i = 0; i < n; i++) {
if (arr[i] == 0) return true;
}
return false;
}
for (int end = 0; end < n; end++) {
// Reset window when zero is encountered
if (arr[end] == 0) {
start = end + 1;
product = 1;
continue;
}
product *= arr[end];
// Shrink window if product exceeds k
while (start <= end && product > k) {
product /= arr[start];
start++;
}
// Check if current window product equals k
if (product == k) {
return true;
}
}
return false;
}
int main() {
int arr[] = {1, 2, 3, 4};
int k = 6;
int n = sizeof(arr) / sizeof(arr[0]);
if (hasSubarrayWithProduct(arr, n, k)) {
cout << "YES\n";
} else {
cout << "NO\n";
}
return 0;
}
import java.util.ArrayList;
class GFG {
// Function to check if there exists a subarray with a product equal to k
static boolean hasSubarrayWithProduct(int[] arr, int n, int k) {
int start = 0;
int product = 1;
// Special case when k is zero
if (k == 0) {
for (int x : arr) {
if (x == 0) return true;
}
return false;
}
for (int end = 0; end < n; end++) {
// Reset window when zero is encountered
if (arr[end] == 0) {
start = end + 1;
product = 1;
continue;
}
product *= arr[end];
// Shrink window if product exceeds k
while (start <= end && product > k) {
product /= arr[start];
start++;
}
// Check if current window product equals k
if (product == k) {
return true;
}
}
return false;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int k = 6;
if (hasSubarrayWithProduct(arr, arr.length, k)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
# Function to check if there exists a subarray with a product equal to k
def hasSubarrayWithProduct(arr, n, k):
start = 0
product = 1
# Special case when k is zero
if k == 0:
for x in arr:
if x == 0:
return True
return False
for end in range(n):
# Reset window when zero is encountered
if arr[end] == 0:
start = end + 1
product = 1
continue
product *= arr[end]
# Shrink window if product exceeds k
while start <= end and product > k:
product //= arr[start]
start += 1
# Check if current window product equals k
if product == k:
return True
return False
if __name__ == "__main__":
arr = [1, 2, 3, 4]
k = 6
if hasSubarrayWithProduct(arr, len(arr), k):
print("YES")
else:
print("NO")
using System;
class GFG
{
// Function to check if there exists a subarray with a product equal to k
static bool hasSubarrayWithProduct(int[] arr, int n, int k)
{
int start = 0;
int product = 1;
// Special case when k is zero
if (k == 0)
{
foreach (int x in arr)
{
if (x == 0) return true;
}
return false;
}
for (int end = 0; end < n; end++)
{
// Reset window when zero is encountered
if (arr[end] == 0)
{
start = end + 1;
product = 1;
continue;
}
product *= arr[end];
// Shrink window if product exceeds k
while (start <= end && product > k)
{
product /= arr[start];
start++;
}
// Check if current window product equals k
if (product == k)
{
return true;
}
}
return false;
}
static void Main()
{
int[] arr = { 1, 2, 3, 4 };
int k = 6;
if (hasSubarrayWithProduct(arr, arr.Length, k))
{
Console.WriteLine("YES");
}
else
{
Console.WriteLine("NO");
}
}
}
// Function to check if there exists a subarray with a product equal to k
function hasSubarrayWithProduct(arr, n, k) {
let start = 0;
let product = 1;
// Special case when k is zero
if (k === 0) {
for (let x of arr) {
if (x === 0) return true;
}
return false;
}
for (let end = 0; end < n; end++) {
// Reset window when zero is encountered
if (arr[end] === 0) {
start = end + 1;
product = 1;
continue;
}
product *= arr[end];
// Shrink window if product exceeds k
while (start <= end && product > k) {
product /= arr[start];
start++;
}
// Check if current window product equals k
if (product === k) {
return true;
}
}
return false;
}
// Driver code
let arr = [1, 2, 3, 4];
let k = 6;
if (hasSubarrayWithProduct(arr, arr.length, k)) {
console.log("YES");
} else {
console.log("NO");
}
Output
YES
Time Complexity: O(n)
Auxiliary Space: O(1)