Calculate the Max Value
Updated
Problem statement
Amazon’s engineering team is developing a tool designed to minimize the size of an n x n grid named matrix, based on a compression ratio described by the array limit. Their challenge is to compute the highest possible sum of exactly x elements from the matrix while adhering to the following conditions:
- For every row
i(where0 ≤ i < n), the count of elements picked from that row must not surpasslimit[i]. - If it turns out impossible to pick exactly
xelements, the function should return-1.
Your task is to assist by computing the largest possible sum achievable under these restrictions.
Function
findMaxValue(limit: int[], matrix: int[][], x: int) → long Complete the function findMaxValue in the editor.
findMaxValue has the following parameters:
int[] limit: an array where each entry is the maximum count of elements allowed to be chosen from the matching row ofmatrixint[][] matrix: a two-dimensional square grid of integersint x: the exact total number of elements to pick
Returns long: the maximum sum when choosing exactly x elements within the limits, or -1 if that is not possible
Examples
Example 1
limit = [1, 2, 1]matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]x = 2return = 15The top selections from each row, respecting the limit constraints, are as follows:
Row 0: Choose 3 (since limit[0] = 1 allows only one pick)
Row 1: Pick 5 and 6 (as limit[1] = 2 permits two selections)
Row 2: Choose 9 (since limit[2] = 1 restricts to a single selection)
However, only x = 2 items can be chosen in total.
The most optimal selection strategy is to pick 6 from Row 1 and 9 from Row 2, maximizing the sum.
Constraints
- 1 ≤
n≤ 50 - 1 ≤
limit[i]≤n - 1 ≤
matrix[i][j]≤ 10^4 - 1 ≤
x≤n*n
More Amazon interview questions
- Lexicographically Largest IdentifierOA · Seen Nov 2026
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fill One Exact-Length Binary WindowOA · Seen Nov 2026
Similar questions at other companies
- Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
- Grid Paths with Override PassesIMC Trading · OAIMC Trading interview questions
- Largest Square of OnesOpenAI · OAOpenAI interview questions
- Grid Paths with Override PassesMicrosoft · OAMicrosoft interview questions
- Minimum Moves to Spread Stones Across a GridAmerican Express · OAAmerican Express interview questions
- Maximum Even-Sum Neighboring PairsDRW · OADRW interview questions
- Minimize Total Input Cost (for LTMS)Salesforce · OASalesforce interview questions
- Minimum Operations To Reduce To ZeroUber · OAUber interview questions
- Maximum Reward (Intuit India)Intuit · OAIntuit interview questions
- Get Minimum OperationsJP Morgan Chase · OAJP Morgan Chase interview questions