Maximum Subarray Sum with Equal Endpoints
Problem statement
Given an integer array nums, choose indices i and j such that i <= j and nums[i] == nums[j].
The score of a pair is the inclusive subarray sum nums[i] + nums[i + 1] + ... + nums[j]. Return [i, j] for a pair with the maximum score.
If several pairs have the same maximum score, choose the one with the smallest i; if there is still a tie, choose the smallest j. A one-element subarray is valid, so an answer always exists.
Examples
Example 1
nums = [2,-5,2,4,2]return = [2,4]The endpoints at indices 2 and 4 are both 2, and their inclusive sum is 8, larger than every other valid pair.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 3 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.