#C613. Maximum Length Subarray Sum

    ID: 49856 Type: Default 1000ms 256MiB

Maximum Length Subarray Sum

Maximum Length Subarray Sum

In this problem, you are given an array of integers and a target integer. Your task is to find the maximum length of a contiguous subarray such that the sum of its elements is equal to the target. Formally, you need to find the maximum (\ell) for which there exists indices (i) and (j) with (0 \leq i \leq j < n) such that [ \sum_{k=i}^{j} a_k = \text{target} ] If there is no such subarray, output 0.

A common approach to solve this problem is to use the concept of prefix sums combined with a hash map to store the earliest occurrence of each prefix sum, which makes it possible to retrieve the required subarray lengths in linear time.

inputFormat

The input is read from standard input (stdin).

The first line contains two space-separated integers (n) and (target), where (n) is the number of elements in the array.
The second line contains (n) space-separated integers representing the elements of the array.

outputFormat

Output a single integer to standard output (stdout): the maximum length of a contiguous subarray whose sum equals the target. If no such subarray exists, print 0.## sample

5 3
1 -1 5 -2 3
4

</p>