#C12027. Longest Subarray with Target Sum

    ID: 41409 Type: Default 1000ms 256MiB

Longest Subarray with Target Sum

Longest Subarray with Target Sum

Given an array of integers and a target sum, your task is to find the length of the longest contiguous subarray whose elements sum exactly to the target value.

A subarray is a contiguous segment of the array, and the order of the elements must be preserved. If there is no subarray whose sum equals the target, output 0.

Constraints:

  • \(1 \leq n \leq 10^4\) (where n is the number of elements in the array)
  • \(-10^4 \leq a_i \leq 10^4\) for every element \(a_i\) in the array
  • \(-10^5 \leq target \leq 10^5\)

Example:

Input:
5 3
1 -1 5 -2 3

Output: 4

</p>

inputFormat

The first line of input contains two space-separated integers: n (the number of elements in the array) and target (the desired sum). The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer, which is the length of the longest contiguous subarray whose sum is equal to the target. If no such subarray exists, output 0.## sample

5 3
1 -1 5 -2 3
4