← 返回 amazon 的题目列表Minimum Insertions to Avoid Zero Sum Subarray
类型:online_judge
Given an array of integers, your goal is to modify the array by inserting additional elements (at any positions) so that no contiguous segment (subarray) of the resulting array sums to zero. Determine the minimum number of insertions required to achieve this.
Input
An integer array arr.
Output
An integer, representing the minimum number of insertions.
Examples
Example 1:
Input: arr = [1, -1, 1, -1] Output: 2
Example 2:
Input: arr = [0, 0, 0] Output: 3
Constraints
The array length can be up to $10^5$.
Each number in the array can have an absolute value of up to $10^4$.
Example
Input
1 -1 1 -1