LeetCode Entry
1658. Minimum Operations to Reduce X to Zero
Min operations to remove first or last elements sum of x
1658. Minimum Operations to Reduce X to Zero medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1491
Problem TLDR
Min operations to remove first or last elements sum of x
Intuition
Invert the problem: longest subarray with sum equal to sum()-x
Approach
- use the target itself as a sum variable, compare with 0
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun minOperations(n: IntArray, x: Int) = n.run {
var t = sum() - x; var j = 0
indices.maxOf { i ->
t -= n[i]; while (t < 0 && j <= i) t += n[j++]
if (t == 0) i - j + 1 else -1
}.let { if (it < 0) -1 else size - it }
}
pub fn min_operations(n: Vec<i32>, x: i32) -> i32 {
let (mut t, mut j, l) = (n.iter().sum::<i32>() - x, 0, n.len());
(0..l).filter_map(|i| {
t -= n[i]; while t < 0 && j <= i { t += n[j]; j += 1 }
(t == 0).then_some((l + j - i - 1) as i32)
}).min().unwrap_or(-1)
}
Comments