Problem 503
Minimum Operations to Reduce X to Zero
Medium · 20 pts · Array, Sliding Window, Prefix Sum
Problem
In one operation you remove either the leftmost or the rightmost element of the array and subtract its value from X. Find the minimum number of operations that reduces X to exactly 0, or print -1 if it is impossible. The removed elements form a prefix and a suffix, so the elements you keep form one middle block whose sum is total - X; find the longest such block with a sliding window (all values are positive).
Input
The first line contains N and X. The second line contains N positive integers.
Output
Print the minimum number of operations, or -1.
Constraints
1 <= N <= 100000; 1 <= A[i] <= 10000; 1 <= X <= 1000000000
Example input
5 5 1 1 4 2 3
Example output
2
Explanation
In 1 1 4 2 3 with X = 5, remove the last two elements, 3 and 2, which sum to exactly 5. That takes 2 operations, and no single element equals 5.