<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>

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int n = nums.size();
        vector<int> pstart(n,0),pend(n,0);
        long long sum=0;
        for(int i=0;i<n;i++)
        {
            sum=sum+nums[i];
            pstart[i]=sum;
        }
        sum=0;
        for(int i=n-1;i>=0;i--)
        {
            sum=sum+nums[i];
            pend[i]=sum;
        }
        int ans=1e9;
        int res=1e9;
        for(int i=n-1;i>=0;i--)
        {
            int target=x-pend[i];
            if(target==0)
            {
                res=n-i;
                ans=min(ans,res);
                continue;
            }
            if(target<0)
            break;

            int start=0,end=i-1;
            while(start<=end)
            {
                int mid = (start+end)/2;
                if(pstart[mid]==target)
                {
                    res=mid+1+(n-i);
                    break;
                }
                else if(pstart[mid]>target)
                {
                    end=mid-1;
                }
                else
                start=mid+1;
            }
            ans=min(ans,res);
        }


        for(int i=0;i<n;i++)
        {
            int target=x-pstart[i];
            if(target==0)
            {
                res=i+1;
                ans=min(ans,res);
                continue;
            }
            if(target<0)
            break;

            int start=i+1,end=n-1;
            while(start<=end)
            {
                int mid = (start+end)/2;
                if(pend[mid]==target)
                {
                    res=i+1+(n-mid);
                    break;
                }
                else if(pend[mid]>target)
                {
                    end=mid-1;
                }
                else
                start=mid+1;
            }
            ans=min(ans,res);
        }
        return ans==1e9 ? -1 : ans;
    }
};
110095