Ctrl K

Richest Repeat-Free Stretch

Medium
You are given an array values of positive numbers. Choose a contiguous stretch of the array in which all numbers are distinct, so no value appears twice. Return the largest possible sum of such a stretch.
Example 1
values=[3, 1, 3, 2, 5, 1] → 11

The stretch [1, 3, 2, 5] has no repeats and sums to 11, as does [3, 2, 5, 1]. No longer repeat-free stretch exists.

Example 2
values=[9, 9, 9] → 9

Any stretch longer than one element repeats a value, so the answer is 9.

Example 3
values=[1, 2, 10, 2, 1] → 13

Both [1, 2, 10] and [10, 2, 1] sum to 13.

Constraints
  • 1 ≤ values.length ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo-Genre Stretch of a Playlist MediumWindows With an Exact Alarm Count MediumStretches With k Odd Numbers MediumTwo Separate Segments With a Given Sum MediumTrim the Ends to a Target Medium
loading Python…
Input
values=[3, 1, 3, 2, 5, 1]
Expected
11

Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.