Maximum Charge After Removal
Updated
Problem statement
You are given an integer array charge. Repeat the following operation until one value remains:
- Choose and remove one value.
- If the removed value had a neighbor on both sides, remove those two neighbors as well and insert their sum in the same position.
- If the removed value was the first or last value, no merge occurs.
Return the maximum possible value of the final remaining system.
Function
maximumChargeAfterRemoval(charge: int[]) → intExamples
Example 1
charge = [-2, 4, 3, -2, 1]return = 4First remove the leftmost value -2, leaving [4, 3, -2, 1]. Then remove the interior value -2; its neighbors 3 and 1 merge into 4, leaving [4, 4]. Remove either edge value to finish with 4.
No sequence can produce a final value greater than 4, so the answer is 4.
Constraints
1 <= charge.length <= 100000-10000 <= charge[i] <= 10000
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