Longest Subarray with Sum K
Mediumjavapythonccppjavascript
Given an integer array and a target K, find the length of the longest contiguous subarray whose sum is exactly K.
Input Format
The first line contains two integers N and K. The second line contains N space-separated integers.
Output Format
Print the length of the longest such subarray, or 0 if none exists.
Example 1
Input
6 15 10 5 2 7 1 9
Output
4
Explanation: The subarray [5, 2, 7, 1] sums to 15 and has length 4.
- 1 <= N <= 100000
- -1000000000 <= A[i] <= 1000000000
- -100000000000000 <= K <= 100000000000000