Maximum Subarray Endpoint Score
Updated
Problem statement
You are given an integer array nums, which may contain negative values.
Choose a section of consecutive elements nums[i..j] containing at least two elements, so i < j. Its score is the sum of its two endpoint values minus the sum of all values strictly between them:
score(i,j) = nums[i] + nums[j] - sum(nums[i+1..j-1]).
The interior of a two-element subarray is empty and has sum 0. A one-element subarray is invalid.
Return the maximum score over all valid subarrays as a signed 64-bit integer.
Use O(n) time and O(1) auxiliary space, where n = nums.length.
Function
maximumEndpointScore(nums: int[]) → longExamples
Example 1
nums = [5,-100,5]return = 110The full array scores 5 + 5 - (-100) = 110. Either adjacent pair scores -95.
Example 2
nums = [4,1,7,3]return = 10The adjacent pair [7,3] scores 10. The interval [4,1,7] also scores 4 + 7 - 1 = 10, and no interval has a higher score.
Example 3
nums = [-5,-2]return = -7The only legal subarray contains both elements. Its interior is empty, so its score is -5 + -2 = -7; a singleton is not allowed.
Example 4
nums = [3,1,2,4,5]return = 9The adjacent subarray [4,5] has an empty interior and scores 4 + 5 = 9, the maximum. For comparison, [3,1,2,4] scores 3 + 4 - (1 + 2) = 4.
Constraints
2 <= nums.length <= 200000.-10^9 <= nums[i] <= 10^9.- Use signed 64-bit arithmetic for scores and running sums.
More Google Onsite Interview questions
- 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
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Subarray Sum Equals KServiceNow · ONSITE INTERVIEWServiceNow interview questions
- Count Subarrays with Equal Even and Odd CountsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Product of Array Except SelfWalmart Labs · ONSITE INTERVIEW
- Subarrays Whose Sum Is a Power of TwoKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Maximize Compatible Car RidersUber Freight · PHONE SCREEN · ONSITE INTERVIEWUber Freight interview questions
- Count Subarrays with Sum KMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
- Product Of Array Except SelfInfosys · ONSITE INTERVIEWInfosys interview questions
- Centered K-by-K Submatrix SumsCitadel · ONSITE INTERVIEWCitadel interview questions
- Farthest Affordable Shop per QueryAdobe · ONSITE INTERVIEWAdobe interview questions
- Product of Array Except SelfSquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions