Minimum Operations to Reduce X to Zero (Leetcode 1658)
Minimum Operations to Reduce X to Zero (Leetcode 1658)
Pattern:
Idea:
Variations :
- part of Subarray with Given Sum β Important Interview Variations (Solutions)
- part of Complementary Counting
π» Code
def isPalindrome(x):
if x < 0:
return False
original = x
rev = 0
while x > 0:
digit = x % 10
rev = rev * 10 + digit
x //= 10
return original == rev
Time complexity - O(D) , D is no of digits
Aux. Space complexity - O(1)
Minimum Operations to Reduce X to Zero (Leetcode 1658)
Tags: #SlidingWindow #TwoPointers #PrefixSum #Arrays #Optimization #Interview-Pattern #LeetCode #FAANG
Problem Statement
Given an integer array nums and an integer x, you may remove elements only from the left or the right end of the array.
Each removed element is subtracted from x.
Return the minimum number of operations needed to reduce x to exactly 0. If impossible, return -1.
Example
-
nums = [1,1,4,2,3] -
x = 5 -
Answer:
2(2 + 3removed from the right)
Key Idea
Instead of deciding what to remove, find what to keep.
Let:
-
total = sum(nums) -
Removed sum =
x
Then the kept middle subarray must have sum:
So the problem becomes:
Find the longest subarray whose sum is
total - x.
If that subarray has length L, then:
This transformation is the entire interview trick.
Intuition (The WHY)
Removing only from both ends always leaves one contiguous middle subarray.
Example:
[1, 1, 4, 2, 3]
β β
remove remove
Remaining:
[1, 1, 4]
Instead of exploring every left/right combination, maximize the length of the kept middle segment.
Minimum removed = Maximum kept.
Approach β Longest Subarray with Sum = Target
Since all numbers are positive in this problem, the target-sum subarray can be found using a variable-size sliding window.
Algorithm
-
Compute
target = sum(nums) - x. -
Edge cases:
-
target < 0β impossible -
target == 0β remove every element
-
-
Use sliding window to find the longest subarray with sum =
target. -
Return
n - longest.
Python Solution
def minOperations(nums, x):
target = sum(nums) - x
if target < 0:
return -1
if target == 0:
return len(nums)
left = 0
curr = 0
longest = -1
for right in range(len(nums)):
curr += nums[right]
while curr > target:
curr -= nums[left]
left += 1
if curr == target:
longest = max(longest, right - left + 1)
return -1 if longest == -1 else len(nums) - longest
Dry Run
nums = [1,1,4,2,3]
x = 5
Total sum:
Target:
Find the longest subarray with sum 6.
| Window | Sum |
|---|---|
[1] | 1 |
[1,1] | 2 |
[1,1,4] | 6 β |
[1,1,4,2] | 8 |
[4,2] | 6 β |
Longest valid window:
[1, 1, 4]
Length = 3
Operations:
Answer = 2
Why Sliding Window Works Here
Sliding window requires the sum to behave monotonically.
Since every element is positive:
-
Expand β sum increases
-
Shrink β sum decreases
This guarantees every pointer moves only forward, giving linear time.
If negative numbers were allowed, this approach would fail and weβd use Prefix Sum + HashMap instead.
Edge Cases
Case 1 β Impossible
nums = [5,6,7]
x = 30
Target:
Negative target is impossible.
Return -1.
Case 2 β Remove Everything
nums = [3,2,5]
x = 10
Target:
Keep nothing.
Answer = 3.
Case 3 β No Valid Middle
nums = [1,1,1]
x = 2
Target:
Longest window = [1]
Operations = 3 - 1 = 2.
Complexity
| Metric | Value |
|---|---|
| Time | O(n) |
| Auxiliary Space | O(1) |
The window pointers each traverse the array at most once.
Relationship to Previous Problems
| Original Problem | Transformed Into |
|---|---|
Remove ends to make sum = x | Longest subarray with sum = total-x |
| Longest Subarray with Sum K | Same sliding-window pattern |
| Count Subarrays with Sum K | Prefix Sum + HashMap |
This is a classic problem reversal interview pattern.
Common Mistakes
1. Solving the removal problem directly
Many candidates try DFS or two-pointer removal from both ends, leading to exponential or quadratic solutions.
Instead, transform it into a kept subarray problem.
2. Forgetting target == 0
If:
the answer is simply:
return len(nums)
3. Using Prefix Sum Unnecessarily
Because all numbers are positive, sliding window is both simpler and more space-efficient.
Key Takeaways / Pattern Recognition
-
Remove from both ends almost always means keep one contiguous middle subarray.
-
Convert:
-
Removed Sum =
x -
Kept Sum =
total - x
-
-
Then maximize the kept length.
-
This is one of the most important examples of reframing an optimization problem into a familiar sliding-window pattern.