Ctrl K

Drop a Third, Compare the Halves

Hard
You are given an array values of 3n integers. Remove exactly n elements of your choice (they need not be adjacent); the remaining 2n keep their original order. The remaining array is cut in half: the first n elements form the left half and the next n the right half. The difference is the sum of the left half minus the sum of the right half, and it may be negative. Return the smallest possible difference.
Example 1
values=[4, 2, 6] → -4

Here n = 1. Removing 6 leaves [4, 2] with 4 - 2 = 2; removing 4 leaves [2, 6] with 2 - 6 = -4; removing 2 leaves [4, 6] with -2. The minimum is -4.

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

n = 2. Removing 2 and 3 leaves [5, 1, 8, 7], so the difference is (5 + 1) - (8 + 7) = -9. Nothing smaller is possible.

Example 3
values=[9, 9, 9, 9, 9, 9, 9, 9, 9] → 0
Constraints
  • len(values) = 3n
  • 1 ≤ n ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFuel Stops Along the Way HardA Round Cake and Two Neighbors HardMaximum Sum With Short Hops HardFerry Runs From the Depot HardLong Leaps Across a Board Hard
loading Python…
Input
values=[4, 2, 6]
Expected
-4

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