Maximize Two Endpoint Values
Updated
Problem statement
You are given an integer array values. Choose two indices i and j with i < j.
The score of the chosen pair is
sum(values[i..j]) - sum(values[i+1..j-1]),
where the sum of an empty interior range is 0.
Return the maximum score over all valid pairs. The result is a 64-bit integer.
Function
maximumEndpointSum(values: int[]) → longExamples
Example 1
values = [4,1,7,3]return = 11Choose the endpoints with values 4 and 7. Interior values cancel from the two range sums, so the score is 4 + 7 = 11.
Example 2
values = [-5,-2,-8]return = -7The two largest values are -2 and -5, producing the maximum score -7.
Example 3
values = [6,6]return = 12The only pair has no interior values and scores 6 + 6 = 12.
Constraints
2 <= values.length <= 2 * 10^5-10^9 <= values[i] <= 10^9
More Google Onsite Interview questions
- Minimum Piano Hand Raises and Thumb PositionsONSITE INTERVIEW · Seen Dec 2026
- Swap Adjacent in LR StringONSITE INTERVIEW · Seen Dec 2026
- Top Ten Largest FilesPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Count Winning Dice Face PairsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find a Failing Test PairONSITE INTERVIEW · Seen Nov 2026
- Minimum Cars for Rental RequestsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Squirrel Nut Collection Shortest PathAmazon · ONSITE INTERVIEW
- Server InvestmentRippling · OARippling interview questions
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
- Make Layer Times OddPatreon · OAPatreon interview questions
- Halve Values Until All Are at Most kAdobe · PHONE SCREENAdobe interview questions
- Delete and Freeze GameRubrik · OARubrik interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Operations to Make Every Array Element OneOtter.ai · PHONE SCREENOtter.ai interview questions