Maximize the Array Value
Updated
Problem statement
You are given an array arr. In one operation you remove one element:
- If you remove the first or the last element, it simply disappears.
- If you remove an element that has a neighbour on both sides, those two neighbours are replaced by one element equal to their sum.
Keep doing operations until only one element is left. Return the largest value that last element can have.
For example, removing -2 at index 3 of [-2, 4, 3, -2, -1] merges its neighbours 3 and -1 into 2, giving [-2, 4, 2].
Function
maximizeArrayValue(arr: int[]) → intExamples
Example 1
arr = [-2, 4, 3, -2, -1]return = 4Remove -2 at index 3. Its neighbours 3 and -1 merge into 2, so the array becomes [-2, 4, 2]. Remove the first element: [4, 2]. Remove the last element: [4]. The final value is 4, and no order of operations does better.
Constraints
1 ≤ arr.length ≤ 10^5-10^9 ≤ arr[i] ≤ 10^9- The answer fits in a signed 32-bit integer.
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
- Minimum Cost to Merge Values into OnePayPal · OAPayPal interview questions
- Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Reschedule Meetings for Maximum Free TimeFivetran · OAFivetran interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Maximum ScoreNVIDIA · OANVIDIA interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions