Question: PnL [ i ] represents the ith month profits or loss. We can perform the following operation on the PnL array any number of times:

PnL[i] represents the ith month profits or loss. We can perform the following operation on the PnL array any number of times: choose any month i (i>=0 and i0
Few test inputs and outputs:
1. N =4 PnL=[5,3,1,2]
Output : 2
Because [5,-3,-1,-2] leads to a cumulative PnL of [5,2,1,-1] which has a negative cumulative pnl at last month
And [-5,3,1,2] leads to a cumulative pnl of [-5,-2,-1,-1] and is invalid as cumulative PnL of first three months are negative
2. N =5, PnL =[1,1,1,1,1]
Output : 2
As PnL of [1,1,-1,1,-1] or [1,1,1,-1,-1] leads to answer.
3. N =6, PnL =[5,2,3,5,2,3]
Output: 3
Please give brute force and most optimal java code separately for it

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!