Maximum Number of Products You Can Pick
Updated
Problem statement
You are given an array products. The value products[i] is how many items of product i are in stock.
Choose a subarray of products. A subarray is a stretch of products that sit next to each other. Go through it from left to right and pick some items from every product in it.
- From each product, pick at least 1 item and at most its stock.
- The number you pick from a product must be bigger than the number you picked from the product before it.
Return the largest total number of items you can pick.
For example, take products = [7, 5, 3]. If you use the whole array, you must pick 1, 2 and 3 items, which gives 6. If you use only [7, 5], you can pick 4 and 5 items, which gives 9. So the answer for this array is 9.
Another example is products = [2, 9, 4, 7, 5, 3]. Use the subarray [2, 9, 4, 7] and pick 2, 3, 4 and 7 items. The total is 16, and no other choice gives more.
Function
maximumProductsPicked(products: int[]) → long Complete the function maximumProductsPicked in the editor.
maximumProductsPicked has the following parameter:
int[] products: the stock of each product
Returns long: the largest total number of items you can pick
Examples
Example 1
products = [2, 9, 4, 7, 5, 3]return = 16Use the subarray [2, 9, 4, 7]. Pick 2 items from the first product, 3 from the second, 4 from the third and 7 from the fourth. Each number is bigger than the one before, and none is above its stock. The total is 2 + 3 + 4 + 7 = 16. No other subarray gives more, so the answer is 16.
Constraints
1 <= n <= 5000, wherenis the length ofproducts1 <= products[i] <= 10^9
More Amazon interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
Similar questions at other companies
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Largest Consistent Visiting GroupAuxoAI · OA
- Delete K Subsets for Maximum ScoreRubrik · OARubrik interview questions
- Optimize Discount Selection Under a Point BudgetPalantir · OAPalantir interview questions
- Maximum Index-Weighted Sum with Disjoint SwapsVisa · OAVisa interview questions
- Maximum Sum of HeightsVirtu Financial · OAVirtu Financial interview questions
- Pressure-Isolation Shutdown OrderIntuit · OAIntuit interview questions
- Get Minimum IncrementIBM · OAIBM interview questions
- Jump Game with Prime-3 StepsUber · OAUber interview questions
- Maximum Payout with Context SwitchesPhonePe · OAPhonePe interview questions