Untitled

mail@pastecode.io avatar
unknown
python
6 months ago
492 B
3
Indexable
Never
def find_subarrays_with_sum(nums, target_sum):
    result = []
    current_sum = 0
    subarray = []
    
    for num in nums:
        current_sum += num
        subarray.append(num)

        while current_sum > target_sum:
            current_sum -= subarray.pop(0)

        if current_sum == target_sum:
            result.append(subarray.copy())

    return result

# Example usage:
nums = [5, 1, 2, 7, 3, 4]
target_sum = 8
result = find_subarrays_with_sum(nums, target_sum)
print(result)
Leave a Comment