Company: BNY_18nov
Difficulty: medium
Portfolio Backtesting Problem Description You want to maximize the profit from a trading portfolio while taking a minimum number of losses. Given: An array pnl of n integers representing monthly profits/losses (negative values are losses) An integer k representing the minimum number of losses that must be taken The traders can: Add any positive pnl[i] to their profit For negative pnl[i], either subtract it or skip it The net profit must always be non-negative Find the maximum total profit that can be earned after taking at least k losses. Return -1 if it is not possible to take k losses. Examples Example 1: Input: n = 5, k = 2, pnl = [4, -3, 6, -2, -1] Output: 7 Explanation: The optimal strategy is: Take the profits: 4 + 6 = 10 Take the losses: -2 and -1 = -3 Net profit: 10 - 3 = 7 This is the maximum possible profit while taking at least 2 losses. Function Description Complete the function getMaxProfit in the editor with the following parameter(s): int pnl[]: the profit/loss predicted