Reduce a Target by Removing Array Ends

Quick Overview

Reduce a Target by Removing Array Ends. In each operation, remove the current leftmost or rightmost element of nums and subtract that element from x. The removal changes which elements are available at the ends for later operations.

Reduce a Target by Removing Array Ends

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem In each operation, remove the current leftmost or rightmost element of `nums` and subtract that element from `x`. The removal changes which elements are available at the ends for later operations. Return the fewest operations that make x exactly zero, or `-1` when impossible. ```cpp class Solution { public: int minOperations(vector<int>& nums, int x); }; ``` ### Examples **Example 1:** ```text nums = [2,3,1,4], x = 6 Output: 2 Explanation: Remove 2 from the left and 4 from the right. ``` **Example 2:** ```text nums = [3,2,2], x = 7 Output: 3 Explanation: Every element must be removed to subtract the full sum. ``` ### Constraints - `1 <= nums.length <= 10^5`. - `1 <= nums[i] <= 10^4`. - `1 <= x <= 10^9`. ### Clarifying Questions The remaining elements form one contiguous middle block, possibly empty. Removing elements from arbitrary internal positions is not allowed. ```hint Inspect the structure After several legal removals, which positions are still eligible for the next operation? ``` ### Reasoning checkpoints - Relate every proposed operation to a legal removal from a current end. - Prove the smallest operation count and distinguish impossibility from removal of the whole array. ### Optional extensions Why would negative array values invalidate this sliding-window argument?

Overview: Reduce a Target by Removing Array Ends. In each operation, remove the current leftmost or rightmost element of nums and subtract that element from x. The removal changes which elements are available at the ends for later operations.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

In each operation, remove the current leftmost or rightmost element of nums and subtract that element from x. The removal changes which elements are available at the ends for later operations. Return the fewest operations that make x exactly zero, or -1 when impossible.

class Solution {
public:
    int minOperations(vector<int>& nums, int x);
};

Examples

Example 1:

nums = [2,3,1,4], x = 6
Output: 2
Explanation: Remove 2 from the left and 4 from the right.

Example 2:

nums = [3,2,2], x = 7
Output: 3
Explanation: Every element must be removed to subtract the full sum.

Constraints

  • 1 <= nums.length <= 10^5 .
  • 1 <= nums[i] <= 10^4 .
  • 1 <= x <= 10^9 .

Clarifying Questions Guidance

The remaining elements form one contiguous middle block, possibly empty. Removing elements from arbitrary internal positions is not allowed.

Reasoning checkpoints

  • Relate every proposed operation to a legal removal from a current end.
  • Prove the smallest operation count and distinguish impossibility from removal of the whole array.

Optional extensions

Why would negative array values invalidate this sliding-window argument?