educative.io

Clean O(n^3) Solution in Python

def find_subarrays(arr, target):
    result = []
    for l_idx, _ in enumerate(arr):
        r_idx = l_idx
        product = 1
        while r_idx < len(arr):
            product *= arr[r_idx]
            if product < target:
                result.append(arr[l_idx : r_idx + 1])
                r_idx += 1
            if product >= target:
                break
    return result

Type your question above this line.

Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/5902703286812672

2 Likes

Hi @Akshay_Goel
Yes, your solution is correct.

Happy learning :slight_smile:

1 Like