You are given an integer array `nums` and an integer `x`. In one operation, you can either remove the leftmost or the rightmost element from the array `nums` and subtract its value from `x`. Return the minimum number of operations to reduce `x` to exactly `0` if it is possible, otherwise return `-1`.
You are given an integer array nums and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. Return the minimum number of operations to reduce x to exactly 0 if it is possible, otherwise return -1.
Example 1:
Input: {"nums":[1,1,4,2,3],"x":5}
Output: 2
Constraints:
1≤nums.length≤105
1≤nums[i]≤104
1≤x≤109
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[1,1,4,2,3],"x":5}
Removing from both ends leaves a middle block. The removed parts must add to x = 5, so the kept block must add to 11 - 5 = 6. Find the LONGEST block with that sum.
the array as a ribbon of length 11
keep exactly 6
block sum must match the green bracket; whatever is outside it is what x removes