#K60657. Contiguous Subarray Sum

    ID: 31135 Type: Default 1000ms 256MiB

Contiguous Subarray Sum

Contiguous Subarray Sum

Given an integer array and a target integer \( T \), determine if there exists a contiguous subarray whose sum equals \( T \). This problem can be solved efficiently using prefix sums and a hash set. The key observation is that if at any index the current prefix sum is \( S \), and there exists a previous prefix sum equal to \( S - T \), then a contiguous subarray summing to \( T \) exists.

For example, for the array [1, 2, 3, 7, 5] and \( T = 12 \), the subarray [2, 3, 7] has a sum of 12.

inputFormat

The first line contains an integer \( n \), the number of elements in the array. The second line contains \( n \) space-separated integers representing the array elements. The third line contains an integer \( T \), the target sum.

outputFormat

Output "True" if there exists any contiguous subarray in the array that sums to \( T \); otherwise, output "False".

## sample
5
1 2 3 7 5
12
True