FastPrepMaximum Subarray Sum with Equal Endpoints

Maximum Subarray Sum with Equal Endpoints

Google logoGoogle● MediumINTERNNEW GRADONSITE INTERVIEW

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.

The problem statement continues
Pro

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.

FastPrep Pro
Reported in 1 Google interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
See Google hiring insights
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week