Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Reduce X to Zero

Minimum Operations to Reduce X to Zero gives an array and a target x. Remove values only from either end and return the fewest removals that subtract x exactly, or -1 if impossible.

Statement

You are given an integer array nums and an integer x. In each operation, you can remove either the leftmost or the rightmost element from the array nums and subtract its value from x. This removes the element from the array for any subsequent operations.

Return the minimum number of operations required to reduce x to exactly 00. If it is impossible to reduce x to 00, return −1-1.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤104\leq 10^4

  • 1≤1 \leq x ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Operations to Reduce X to Zero

Minimum Operations to Reduce X to Zero gives an array and a target x. Remove values only from either end and return the fewest removals that subtract x exactly, or -1 if impossible.

Statement

You are given an integer array nums and an integer x. In each operation, you can remove either the leftmost or the rightmost element from the array nums and subtract its value from x. This removes the element from the array for any subsequent operations.

Return the minimum number of operations required to reduce x to exactly 00. If it is impossible to reduce x to 00, return −1-1.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤104\leq 10^4

  • 1≤1 \leq x ≤109\leq 10^9