You are given a 0-indexed integer array nums. You can rearrange the elements of nums to any order (including the original order).
Let prefix be the prefix sum array of nums. nums is considered valid if prefix[i] >= 0 for all i.
Return the minimum number of elements you need to rearrange in nums such that nums is valid.
Example 1:
Input: nums = [5,-2,-1,5,-5] Output: 1 Explanation: One possible rearrangement is [5,5,-2,-1,-5], with prefix sum values [5,10,8,7,2], which are all non-negative. We can prove that 1 is the minimum number of elements to rearrange to make nums valid.
Example 2:
Input: nums = [-1,1,-3,2] Output: 0 Explanation: One possible rearrangement is [1, 2, -1, -3], with prefix sum values [1,3,2,-1], which is not valid. However, the original array [-1,1,-3,2] has prefix sum values [-1,0,-3,-1], which is also not valid. We can prove that 0 is the minimum number of elements to rearrange to make nums valid.
Constraints:
1 <= nums.length <= 105-109 <= nums[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to make a running total of numbers always be zero or more. We'll try fixing the list by swapping pairs of numbers to see if we can achieve this. The brute force way means testing *every* possible swap until we find one that works.
Here's how the algorithm would work step-by-step:
def make_prefix_sum_non_negative_brute_force(numbers):
list_length = len(numbers)
for first_index in range(list_length):
for second_index in range(first_index, list_length):
# Attempt to swap the numbers at the two indices.
temporary_value = numbers[first_index]
numbers[first_index] = numbers[second_index]
numbers[second_index] = temporary_value
prefix_sum = 0
is_non_negative = True
for number in numbers:
prefix_sum += number
# We need to check if prefix sum goes negative
if prefix_sum < 0:
is_non_negative = False
break
# If all prefix sums are non-negative, return.
if is_non_negative:
return numbers
# Swap back if the current swap did not work.
temporary_value = numbers[first_index]
numbers[first_index] = numbers[second_index]
numbers[second_index] = temporary_value
# No valid swap found.
return NoneThe main idea is to keep track of the running total. If the running total ever becomes negative, we need to fix it by strategically swapping some numbers to make it non-negative. We always swap the most negative number that caused the issue.
Here's how the algorithm would work step-by-step:
def make_prefix_sum_non_negative(numbers):
prefix_sum = 0
swap_count = 0
negative_numbers = []
for i in range(len(numbers)):
prefix_sum += numbers[i]
# Store negative numbers to swap if prefix sum goes negative
if numbers[i] < 0:
negative_numbers.append(i)
negative_numbers.sort(key=lambda index: numbers[index])
# Need to swap if the prefix sum dips below zero
while prefix_sum < 0:
if not negative_numbers:
return -1
index_to_swap = negative_numbers.pop(0)
# Always swap the most negative number seen thus far
prefix_sum -= 2 * numbers[index_to_swap]
numbers[index_to_swap] *= -1
swap_count += 1
return swap_count| Case | How to Handle |
|---|---|
| Empty input array | Return 0, as no operations are needed to make the prefix sum non-negative. |
| Array with all positive numbers | Return 0, as the prefix sum is already non-negative. |
| Array with all negative numbers | Sort the array in ascending order (smallest/most negative first) and perform the swap operations until the prefix sums are all non-negative. |
| Array with a large number of elements | Ensure the algorithm has O(n log n) time complexity due to sorting, avoiding quadratic or worse performance. |
| Array contains very large negative numbers | Use 64-bit integers to avoid integer overflow when calculating prefix sums. |
| Array where no amount of swapping can make prefix sums non-negative | Return -1 to indicate that no solution exists. |
| Array with many zeros | Zeros should be ignored during the sorting step as their placement doesn't affect the negativity of the prefix sum. |
| Presence of duplicates and their impact on the sorting and swapping operations. | Sorting algorithm needs to be stable if duplicate negative numbers significantly affect the number of operations. |