Ctrl K

Three Equal Stretches

Easy
A cashier recorded the day's revenue changes in order in the array amounts. They want to cut the day into three non-empty contiguous stretches (morning, afternoon and evening) so that every stretch has the same sum. Return True if such a split exists, and False otherwise. Elements cannot be reordered, and each element goes into exactly one stretch.
Example 1
amounts=[4, -1, 2, 5, 0, 3, 2] → True

[4, -1, 2] = 5, [5] = 5 and [0, 3, 2] = 5.

Example 2
amounts=[1, 2, 3, 3] → True

The total is 9, so each piece must sum to 3: [1, 2], [3] and [3].

Example 3
amounts=[2, 2, 2, -3, 3] → True

The total is 6, so the pieces must sum to 2 each, e.g. [2], [2], [2, -3, 3].

Constraints
  • 3 ≤ len(amounts) ≤ 5 · 10⁴
  • -10⁴ ≤ amounts[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCake Slices for Guests EasyStrength of the Weaker Partner EasyPlanting Along a Garden Row EasyMaking Change at the Kiosk EasyRise and Fall Lineup Easy
loading Python…
Input
amounts=[4, -1, 2, 5, 0, 3, 2]
Expected
True

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