Maximum Profit with K Months

Company: Linkedin_31aug

Difficulty: medium

Problem Statement

Maximum Profit with K Months Problem Description You need to analyze the performance of investments in a stock. The profit and loss (PnL) for each month is represented in an array where each value indicates the profit earned (positive value) or loss incurred (negative value) in that month. Your task is to find the maximum net profit that can be gained from any contiguous segment of months, with the constraint that the segment cannot exceed a given number of months k . Complete the function getMaxProfit in the editor. The function is expected to return a long_int , representing the sum of a contiguous subarray of size k or less that has the largest sum. The function accepts the following parameters: pnl : An integer array representing monthly profits and losses. k : An integer representing the maximum number of months to consider. The C++ function signature is: long getMaxProfit(vector<int> pnl, int k) Examples Example 1: Input: pnl = [-3, 4, 3, -2, 2, 5], k = 4 Output: 8 Explanat