<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>

<p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,4,2,3], x = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,6,7,8,9], x = 4
<strong>Output:</strong> -1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10
<strong>Output:</strong> 5
<strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li>
</ul>

/*

https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/935935/Java-Detailed-Explanation-O(N)-Prefix-SumMap-Longest-Target-Sub-Array

https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/936074/JavaPython-3-Sliding-window%3A-Longest-subarray-sum-to-the-target-sum(nums)-x.

https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/935956/C%2B%2B-O(n)-Two-Sum-and-Hash-Map

*/

#include <unordered_map>

using namespace std;

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        
        int n = nums.size(), total = 0, mn = INT_MAX, len = -1;
        
        for (int num : nums) {
            mn = min(mn, num);
            total += num;            
        }
        
        if ( x < mn || total < x ) {
            return -1;
        }
        if ( total == x ) {
            return n;
        }
        
/*
        int target = total - x, l = 0, r = 0, sum = 0;
        
        while (r < n) {
        
            sum += nums[r++];
            
            while ( l < r && sum > target ) {
                sum -= nums[l++];
            }
            
            if (sum == target) {
                len = max(len, r - l);
            }
        }
*/
        int target = total - x, sum = 0;
        
        unordered_map<int, int> mp;
        mp[0] = -1;
        
        for (int i = 0; i < n; ++i) {
            
            sum += nums[i];
            
            int diff = sum - target;
            
            auto it = mp.find(diff);
            if ( it != mp.end() ) {
                len = max(len, i - it->second );
            } 
            if ( mp.count(sum) == 0 ) {
                mp[sum] = i;
            }
        }        
        
        return ( len == -1 ? -1 : n - len );
    }
};
126145