Talentd
FleetCode

3511. Make a Positive Array

Medium36.9% acceptance1 companyPremium elsewhere, free here

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 -1018 and 1018.

Find the minimum number of operations needed to make nums positive.

Example 1:

Input: nums = [-10,15,-12]

Output: 1

Explanation:

The only subarray with more than 2 elements is the array itself. The sum of all elements is (-10) + 15 + (-12) = -7. By replacing nums[0] with 0, the new sum becomes 0 + 15 + (-12) = 3. Thus, the array is now positive.

Example 2:

Input: nums = [-1,-2,3,-1,2,6]

Output: 1

Explanation:

The only subarrays with more than 2 elements and a non-positive sum are:

Subarray Indices Subarray Sum Subarray After Replacement (Set nums[1] = 1) New Sum
nums[0...2] [-1, -2, 3] 0 [-1, 1, 3] 3
nums[0...3] [-1, -2, 3, -1] -1 [-1, 1, 3, -1] 2
nums[1...3] [-2, 3, -1] 0 [1, 3, -1] 3

Thus, nums is positive after one operation.

Example 3:

Input: nums = [1,2,3]

Output: 0

Explanation:

The array is already positive, so no operations are needed.

Constraints:

  • 3 <= nums.length <= 105
  • -109 <= nums[i] <= 109

Asked at 1 company

Hints open one at a time

Hint 1
Only check subarrays up to a specific size.
Hint 2
Find the subarrays containing between 3 and 5 elements with a non-positive total sum.
Hint 3
Use a greedy approach to assign certain indices the maximum value.
Hint 4
Start with the subarray with the smallest last index i. Replace the element at index i with the maximum value. Find the selected subarray with the next smallest last index j that does not contain index i. Replace the element at index j with the maximum value, and repeat this process until all non-positive subarrays are fixed.
Still stuck?Read the written solution.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[-10,15,-12]
Input
[-1,-2,3,-1,2,6]
Input
[1,2,3]
Your code is saved on this device.Solution