Ctrl K

The Largest Stray Value

Medium
You are given an integer array values of length n. Its elements play three roles: - n − 2 elements are **addends**; - one element is the **total**, equal to the sum of all addends; - one more element is the **stray**, unrelated to the rest. You don't know in advance which element plays which role, and there may be several valid assignments. Every element plays exactly one role, but elements in different roles may share a value. Return the largest value the stray element can have.
Example 1
values=[4, 1, 3, 9, 8] → 9

If 9 is the stray, the remaining 4, 1 and 3 sum to 8, which is in the array. Every other split gives a smaller stray.

Example 2
values=[-5, 2, -3, 7] → 7

-5 and 2 sum to -3, so the stray is 7.

Example 3
values=[6, 2, 2, 2, 6] → 6

Three 2s add up to 6 and the other 6 is the stray: equal values are fine as long as they are different elements.

Constraints
  • 3 ≤ len(values) ≤ 10⁵
  • -1000 ≤ values[i] ≤ 1000
  • At least one valid assignment of roles exists
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPrimes Along Grid Rays MediumOne-Swap Twin Numbers MediumBalanced Cuts After One Replacement HardTwins Within Two Swaps HardMore Than a Third of the Votes Medium
loading Python…
Input
values=[4, 1, 3, 9, 8]
Expected
9

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