Make a Positive Array - Problem
You are given an array nums. An array is considered positive if the sum of all numbers in each subarray with more than two elements is positive.
You can perform the following operation any number of times: Replace one element in nums with any integer between -10¹⁸ and 10¹⁸.
Find the minimum number of operations needed to make nums positive.
Input & Output
Example 1 — Basic Negative Array
$
Input:
nums = [-5, 2, -3, 1]
›
Output:
1
💡 Note:
Subarrays with length ≥ 3: [-5, 2, -3] has sum -6 ≤ 0, [2, -3, 1] has sum 0 ≤ 0, and [-5, 2, -3, 1] has sum -5 ≤ 0. Replace nums[1] = 2 with a large positive number. This makes [-5, X, -3] positive, [X, -3, 1] positive, and [-5, X, -3, 1] positive. So 1 operation is needed.
Example 2 — Already Positive
$
Input:
nums = [1, 2, 3]
›
Output:
0
💡 Note:
The only subarray with length ≥ 3 is [1, 2, 3] with sum 6 > 0. Array is already positive, so 0 operations needed.
Example 3 — Multiple Problems
$
Input:
nums = [-2, -3, -1, -4, 1]
›
Output:
2
💡 Note:
Multiple negative subarrays exist. Replace the two most impactful negative elements (like nums[1] = -3 and nums[3] = -4) to make all subarrays positive with 2 operations.
Constraints
- 1 ≤ nums.length ≤ 1000
- -1018 ≤ nums[i] ≤ 1018
- You can replace any element with any integer between -1018 and 1018
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code