Untitled
unknown
python
2 years ago
492 B
15
Indexable
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)
Editor is loading...
Leave a Comment