Skip to main content

Maximum Profitable Triplets With Increasing Prices II - Solution & Explanation

HardPremiumFree on FleetCodeArrayBinary Indexed TreeSegment Tree11 min readAsked at: IBM
Practice this problem

Problem Statement

Given the 0-indexed arrays prices and profits of length n. There are n items in an store where the ith item has a price of prices[i] and a profit of profits[i].

We have to pick three items with the following condition:

  • prices[i] < prices[j] < prices[k] where i < j < k.

If we pick items with indices i, j and k satisfying the above condition, the profit would be profits[i] + profits[j] + profits[k].

Return the maximum profit we can get, and -1 if it's not possible to pick three items with the given condition.

 

Example 1:

Input: prices = [10,2,3,4], profits = [100,2,7,10]
Output: 19
Explanation: We can't pick the item with index i=0 since there are no indices j and k such that the condition holds.
So the only triplet we can pick, are the items with indices 1, 2 and 3 and it's a valid pick since prices[1] < prices[2] < prices[3].
The answer would be sum of their profits which is 2 + 7 + 10 = 19.

Example 2:

Input: prices = [1,2,3,4,5], profits = [1,5,3,4,6]
Output: 15
Explanation: We can select any triplet of items since for each triplet of indices i, j and k such that i < j < k, the condition holds.
Therefore the maximum profit we can get would be the 3 most profitable items which are indices 1, 3 and 4.
The answer would be sum of their profits which is 5 + 4 + 6 = 15.

Example 3:

Input: prices = [4,3,2,1], profits = [33,20,19,87]
Output: -1
Explanation: We can't select any triplet of indices such that the condition holds, so we return -1.

 

Constraints:

  • 3 <= prices.length == profits.length <= 50000
  • 1 <= prices[i] <= 5000
  • 1 <= profits[i] <= 106

Approach Overview

Problem Overview: You are given arrays representing item prices and profits. Pick three indices i < j < k such that prices[i] < prices[j] < prices[k] and the total profits[i] + profits[j] + profits[k] is maximized. If no valid triplet exists, return -1.

Approach 1: Brute Force Enumeration (O(n^3) time, O(1) space)

Check every possible triplet (i, j, k) with three nested loops. For each combination, verify the increasing price condition and compute the total profit. Track the maximum profit seen. This method is straightforward but impractical once n grows beyond a few hundred because the number of combinations explodes to O(n^3). Useful only for understanding the constraints of the problem or validating smaller test cases.

Approach 2: Fix the Middle Element (O(n^2) time, O(1) space)

Treat index j as the middle element of the triplet. Scan the left side to find the best i where prices[i] < prices[j], then scan the right side to find the best k where prices[k] > prices[j]. Combine the best profits from both sides with profits[j]. This reduces one loop compared to brute force but still requires scanning the array for each middle index, resulting in O(n^2) time. Works for moderate input sizes but still too slow for large constraints.

Approach 3: Binary Indexed Tree (Fenwick Tree) Optimization (O(n log n) time, O(n) space)

The key observation: for each middle index j, you only need two values — the maximum profit to the left with a smaller price, and the maximum profit to the right with a larger price. These queries can be answered efficiently using a Binary Indexed Tree over compressed price coordinates.

First pass left-to-right: maintain a Fenwick tree storing the maximum profit seen for each price. Query the structure for the maximum profit among prices strictly less than prices[j]. This gives the best candidate for i. Update the tree with profits[j].

Second pass right-to-left: repeat the process to compute the best profit for k where prices[k] > prices[j]. Combine the best left and right values with profits[j] to compute the maximum triplet profit. Coordinate compression ensures the Fenwick tree size stays manageable.

This approach transforms repeated scans into O(log n) range queries and updates. The technique is common in problems involving ordered queries on arrays such as array optimization and range maximum lookups with segment trees or Fenwick trees.

Recommended for interviews: The Binary Indexed Tree approach. Interviewers expect you to recognize that the middle element splits the problem into two range maximum queries. Starting from the brute force demonstrates understanding, but the O(n log n) Fenwick tree solution shows strong command of advanced data structures.

Solution

We can use two Binary Indexed Trees (BITs) to maintain the maximum profit on the left and right of each price, respectively. Then, we enumerate the middle price, query the maximum profit on both sides through the BIT, and finally take the maximum value.

The time complexity is O(n times log M), and the space complexity is O(M). Here, n is the length of the array prices, and M is the maximum value in the array prices. In this problem, M \le 5000.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force TripletsO(n^3)O(1)Conceptual understanding or very small inputs
Fix Middle Element + Scan SidesO(n^2)O(1)Moderate constraints where brute force is too slow
Binary Indexed Tree / Fenwick TreeO(n log n)O(n)Large arrays requiring fast ordered queries for max profit
Segment TreeO(n log n)O(n)Alternative to Fenwick tree when range queries need more flexibility

Video Solution

2921. Maximum Profitable Triplets With Increasing Prices II (Leetcode Hard) • Programming Live with Larry • 275 views views

Frequently Asked Questions

Is Maximum Profitable Triplets With Increasing Prices II easy or hard?
Maximum Profitable Triplets With Increasing Prices II is classified as a Hard problem. The challenge comes from recognizing that the middle element splits the problem into two ordered queries and that a Fenwick tree or segment tree is required to achieve O(n log n) performance.
Maximum Profitable Triplets With Increasing Prices II Python/Java solution
Efficient implementations typically use a Fenwick tree to track maximum profits by compressed price index. The logic remains identical across languages such as Python, Java, C++, Go, TypeScript, and Rust: perform prefix maximum queries and updates while scanning the array.
How to solve Maximum Profitable Triplets With Increasing Prices II in O(n log n)?
Treat each index as the middle element of the triplet. Use a Fenwick tree to store the maximum profit for prices encountered so far and query the best profit with a smaller price. Perform a reverse pass to compute the best profit on the right with larger prices. Combine left + current + right profits while maintaining O(log n) updates and queries.
What is the best approach for Maximum Profitable Triplets With Increasing Prices II?
The optimal approach uses a Binary Indexed Tree (Fenwick Tree) with coordinate compression. For each index treated as the middle element, you query the maximum profit on the left with a smaller price and the maximum profit on the right with a larger price. Each query and update takes O(log n), leading to an overall O(n log n) time complexity.
Is Maximum Profitable Triplets With Increasing Prices II asked at Google/Amazon/Meta?
Problems involving increasing sequences with profit maximization and range queries appear frequently in interviews at companies like Google, Amazon, and Meta. Variants that require Fenwick trees, segment trees, or ordered maps are common in senior-level algorithm rounds.
What data structure is used in Maximum Profitable Triplets With Increasing Prices II?
The most common data structure is a Binary Indexed Tree (Fenwick Tree) used for range maximum queries after coordinate-compressing prices. A Segment Tree can also solve the same problem with similar O(n log n) complexity.
What is the time complexity of Maximum Profitable Triplets With Increasing Prices II?
The optimal solution runs in O(n log n) time using a Binary Indexed Tree or Segment Tree to perform range maximum queries. Brute force enumeration takes O(n^3), and a middle-element scanning approach reduces it to O(n^2). The Fenwick tree solution scales efficiently for large inputs.

Ready to solve this problem?

Practice Maximum Profitable Triplets With Increasing Prices II with our built-in code editor and test cases.

Practice on FleetCode