Ctrl K

Four Equal Pieces

Hard
You are given an integer array rod. Choose three cut positions i < j < k; the elements at those positions are thrown away, and the array falls apart into four pieces: - rod[0..i-1], - rod[i+1..j-1], - rod[j+1..k-1], - rod[k+1..n-1]. Every piece must be non-empty, so 0 < i, i + 1 < j, j + 1 < k and k < n - 1, where n = len(rod). Return True if the cuts can be chosen so that all four pieces have the same sum, and False otherwise.
Example 1
rod=[2, 5, 1, 1, 7, 2, 0, 9, 2] → True

Cut at positions 1, 4 and 7 (dropping 5, 7 and 9). The pieces left are [2], [1, 1], [2, 0] and [2], each summing to 2.

Example 2
rod=[3, 1, 3, 1, 3, 1] → False

Four non-empty pieces plus three cuts need at least 7 numbers, and there are only 6.

Example 3
rod=[1, 8, 3, -2, 0, 1, 5, -1, 2] → True

Cuts at positions 1, 4 and 6 leave [1], [3, -2], [1] and [-1, 2], each summing to 1. Numbers may be negative.

Constraints
  • 1 ≤ len(rod) ≤ 2000
  • -10⁶ ≤ rod[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRectangles With a Given Sum HardA Shelf With Matching Ends HardBalanced Cuts After One Replacement HardFlowers in Bloom on Visit Day HardStretches With a Given Median Hard
loading Python…
Input
rod=[2, 5, 1, 1, 7, 2, 0, 9, 2]
Expected
True

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