Skip to main content

Subarray Product Less Than K

Question

Given an array of positive integers nums and an integer k, find the maximum subarray product less than or equal to k.

Example 1
Input: nums = [10, 5, 2, 6], k = 100

Output: [10], [5], [2], [5, 2], [6], [10, 5], [2, 6], [5, 2, 6]

Solution

all//Subarray Product Less Than K.py


# Python program for Subarray Product Less Than K

# Create a list to store the subarray
subarrays = []

# Function to find subarray with product less than k
def findSubarray(arr, n, k):

# Pick starting point
for i in range(0, n):
product = 1

# Consider all subarrays starting from i
for j in range(i, n):

# Update product of subarray between i and j
product *= arr[j]

# If product is less than k, update subarrays list
if (product < k):
subarrays.append(arr[i:j+1])

# Driver Code
# Array of elements
arr = [10, 5, 2, 6]
n = len(arr)
k = 100

# Function call
findSubarray(arr, n, k)

# Print subarray
print(subarrays)