Dsa
Prefix Sum
Medium
Max Subarray Length with Sum
Given an integer array and a target sum, find the length of the longest contiguous subarray where the sum equals the target. You are to implement this using prefix sums to improve efficiency.
Input:
- The first line contains two integers n (1 ≤ n ≤ 100,000) and target (−1,000,000 ≤ target ≤ 1,000,000).
- The second line contains n integers representing the array elements (−10000 ≤ a[i] ≤ 10000).
Output:
- Output a single integer, the length of the longest subarray that sums to the target.
Example:
Input:
5 5
1 2 3 -1 -3
Output:
3
Key concepts
prefix_sumarray
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now