def count_subarrays_with_sum(arr, target_sum):
count = 0
sum_dict = {0: 1} # Dictionary to store cumulative sum and its frequency
curr_sum = 0
for num in arr:
curr_sum += num
if (curr_sum - target_sum) in sum_dict:
count += sum_dict[curr_sum - target_sum]
if curr_sum in sum_dict:
sum_dict[curr_sum] += 1
else:
sum_dict[curr_sum] = 1
return count
# Example usage
arr = [1, 2, 3, 4, 5]
target_sum = 5
result = count_subarrays_with_sum(arr, target_sum)
print(f"Number of subarrays with sum {target_sum}: {result}")