Find Three Cut Indices That Split an Array into Four Equal-Sum Parts

Quick Overview

Decide whether three cut indices can split an integer array into four non-empty parts with equal sums, where the cut elements themselves are left out. The problem tests prefix sums, careful index bounds and how to avoid a cubic search over all cut positions.

Find Three Cut Indices That Split an Array into Four Equal-Sum Parts

Company: Alibaba

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `nums` of length `n`, return `True` if there is a triplet `(i, j, k)` such that the following conditions are met, otherwise return `False`: - `0 < i, i + 1 < j, j + 1 < k < n - 1` - The sums of the subarrays `(0, i - 1)`, `(i + 1, j - 1)`, `(j + 1, k - 1)` and `(k + 1, n - 1)` are all equal. A subarray `(l, r)` is the slice of the original array from the element at index `l` to the element at index `r`, both included. ### Function Signature ```python def split_into_four_equal_sums(nums: list[int]) -> bool: ``` ### Rules - The elements at indices `i`, `j` and `k` are cut points. They belong to none of the four subarrays. - The index conditions make all four subarrays non-empty. - Return only whether such a triplet exists. If several triplets work, the answer is still `True`. ### Constraints - `1 <= len(nums) <= 2000` - `-10^6 <= nums[i] <= 10^6` ### Examples ```text Input: nums = [1, 2, 1, 2, 1, 2, 1] Output: True ``` Choose `i = 1`, `j = 3`, `k = 5`. The four subarrays are `[1]`, `[1]`, `[1]` and `[1]`, each summing to 1. ```text Input: nums = [1, 2, 1, 2, 1, 2, 1, 2] Output: False ``` No triplet `(i, j, k)` produces four equal-sum subarrays. For example, `i = 1`, `j = 3`, `k = 5` gives the parts `[1]`, `[1]`, `[1]` and `[1, 2]`, and the last part sums to 3. ```text Input: nums = [2, 9, 1, 1, -4, 3, -1, 8, 2] Output: True ``` Choose `i = 1`, `j = 4`, `k = 7`. The four subarrays are `[2]`, `[1, 1]`, `[3, -1]` and `[2]`, each summing to 2. The cut elements 9, -4 and 8 are left out.

Overview: Decide whether three cut indices can split an integer array into four non-empty parts with equal sums, where the cut elements themselves are left out. The problem tests prefix sums, careful index bounds and how to avoid a cubic search over all cut positions.

Alibaba logo
Alibaba
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums of length n, return True if there is a triplet (i, j, k) such that the following conditions are met, otherwise return False:

  • 0 < i, i + 1 < j, j + 1 < k < n - 1
  • The sums of the subarrays (0, i - 1) , (i + 1, j - 1) , (j + 1, k - 1) and (k + 1, n - 1) are all equal.

A subarray (l, r) is the slice of the original array from the element at index l to the element at index r, both included.

Function Signature

def split_into_four_equal_sums(nums: list[int]) -> bool:

Rules

  • The elements at indices i , j and k are cut points. They belong to none of the four subarrays.
  • The index conditions make all four subarrays non-empty.
  • Return only whether such a triplet exists. If several triplets work, the answer is still True .

Constraints

  • 1 <= len(nums) <= 2000
  • -10^6 <= nums[i] <= 10^6

Examples

Input: nums = [1, 2, 1, 2, 1, 2, 1]
Output: True

Choose i = 1, j = 3, k = 5. The four subarrays are [1], [1], [1] and [1], each summing to 1.

Input: nums = [1, 2, 1, 2, 1, 2, 1, 2]
Output: False

No triplet (i, j, k) produces four equal-sum subarrays. For example, i = 1, j = 3, k = 5 gives the parts [1], [1], [1] and [1, 2], and the last part sums to 3.

Input: nums = [2, 9, 1, 1, -4, 3, -1, 8, 2]
Output: True

Choose i = 1, j = 4, k = 7. The four subarrays are [2], [1, 1], [3, -1] and [2], each summing to 2. The cut elements 9, -4 and 8 are left out.