Longest Subarray with Sum K (Mixed Signs)
Given an array that may contain negative numbers, find the longest subarray summing to K using prefix sums.
Input format
The first line contains N and K. The second line contains N integers (possibly negative).
Output format
Print the length of the longest subarray with sum exactly K (0 if none exists).
Constraints
- Values fit in a 64-bit signed integer
- Trailing whitespace and a trailing newline are ignored by the judge
Read from stdin, write to stdout. Sample cases below show the exact format.
Sample cases
Example 1
Input
6 3
1 -1 2 2 -2 3Expected output
3Example 2
Input
3 50
-1 -2 -3Expected output
0