DiffPush Tier Arrays · Hard O(N) · O(N)
A treasury ledger records signed adjustments per period, so both debits and credits appear in the same stream. The compliance team wants the widest contiguous stretch of periods whose adjustments net out to an exact settlement amount. Because negatives appear, cumulative totals can rise and fall unpredictably.
Input: An integer N, an array A of N signed integers, and an integer K, the target net amount.
Output: The number of periods in the longest contiguous subarray whose sum equals K, or 0 when no such subarray exists.
1 <= N <= 10^5-10^4 <= A[i] <= 10^4-10^9 <= K <= 10^9Input: {"N":6,"A":[10,5,2,7,1,9],"K":15}
Output: 4
The stretch 5,2,7,1 nets to 15 and spans four periods, wider than the single 15-like alternatives.
Input: {"N":5,"A":[1,-1,5,-2,3],"K":3}
Output: 4
The first four periods combine to 3 and still beat the shorter trailing stretch that also nets 3.