Problem · Array
Stock Maximum Profit
Learn this problemProblem statement
Given arrays price and profit of equal length, choose three days with indices i < j < k such that:
price[i] < price[j] < price[k]
Maximize profit[i] + profit[j] + profit[k]. Return the maximum possible total profit, or -1 if no valid triplet exists.
Function
getMaxProfit(price: int[], profit: int[]) → longExamples
Example 1
price = [2, 3, 1, 5, 9]profit = [1, 2, 6, 1, 5]return = 12Choose indices (2, 3, 4) using 0-based indexing. The prices are 1 < 5 < 9 and the total profit is 6 + 1 + 5 = 12.
Example 2
price = [4, 3, 2, 1]profit = [4, 3, 2, 1]return = -1The prices are strictly decreasing, so no valid triplet exists.
Constraints
1 ≤ price.length = profit.length ≤ 40001 ≤ price[i] ≤ 10^91 ≤ profit[i] ≤ 10^9
More IBM problems
- Parent Process NumberOA · Seen Jul 2026
- Request Retry CountOA · Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA · Seen Jul 2026
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Spam Text ClassificationOA · Seen Jul 2026
- Count Ideal NumbersOA · Seen Jun 2026