Problem · Array

Profit Analysis

Learn this problem
MediumVirtu Financial logoVirtu FinancialNEW GRADINTERNOA

Problem statement

The profit and loss for each month is represented by an integer array pnl. A positive value represents profit earned in that month, while a negative value represents a loss.

Return the maximum net profit obtainable from any non-empty contiguous segment of months whose length is at most k.

Function

getMaxProfit(pnl: int[], k: int) → long

Examples

Example 1

pnl = [-3,4,3,-2,2,5]k = 4return = 8

The segment [3,-2,2,5] has length 4 and total profit 3 + (-2) + 2 + 5 = 8. The segment [4,3,-2,2,5] has total profit 12, but its length is 5, which exceeds k = 4.

Constraints

  • 1 <= pnl.length <= 2 * 10^5
  • -10^9 <= pnl[i] <= 10^9
  • 1 <= k <= pnl.length

More Virtu Financial problems

drafts saved locally
public long getMaxProfit(int[] pnl, int k) {
    // write your code here
}
pnl[-3,4,3,-2,2,5]
k4
expected8
checking account