Maximize Negative PnL Months
Learn this problemProblem statement
You are given an array PnL of positive integers, where PnL[i] is the profit for the i-th month before any changes.
You may perform the following operation any number of times: choose an index i with 0 <= i < n and multiply PnL[i] by -1. Each index can be chosen at most once because applying the operation twice would restore the original value and cannot improve the answer.
After choosing which months become negative, every prefix sum must remain strictly positive. In other words, for every index i, PnL[0] + PnL[1] + ... + PnL[i] > 0 after the sign changes.
Return the maximum number of months that can have negative PnL while satisfying this condition.
Function
maximizeNegativePnLMonths(PnL: int[]) → intExamples
Example 1
PnL = [5, 3, 1, 2]return = 2One valid choice is to make the second and third months negative, producing [5, -3, -1, 2]. The prefix sums are [5, 2, 1, 3], all strictly positive. It is not possible to make 3 months negative while keeping every prefix sum strictly positive, so the answer is 2.
Example 2
PnL = [1, 1, 1, 1, 1]return = 2For PnL = [1, 1, 1, 1, 1], valid choices with 2 negative months include [1, 1, -1, 1, -1] and [1, 1, 1, -1, -1]. Their prefix sums stay strictly positive. Any attempt to make 3 months negative forces some prefix sum to be 0 or less, so the answer is 2.
Example 3
PnL = [5, 2, 3, 5, 2, 3]return = 3Constraints
1 <= n <= 1051 <= PnL[i] <= 109
More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026