Split a circular array into k clusters to minimize the sum of squared cluster sums

Quick Overview

Split a circular array of positive sector values into exactly k contiguous clusters so that the sum of the squared cluster sums is as small as possible. Tests dynamic programming over prefix sums, handling wrap-around on a circular array, and reasoning about where optimal cuts can fall.

Split a circular array into k clusters to minimize the sum of squared cluster sums

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given a circular array of `n` sectors, where each sector has a positive integer value. You need to select exactly `k` split points between adjacent sectors to divide the circular array into `k` clusters. The cost of a cluster is the square of the sum of the sector values in that cluster. Choose the `k` split points so that the total cost, the sum of the costs of all clusters, is as small as possible. Return the minimum total cost. ### Function Signature ```python def min_split_cost(sectors: list[int], k: int) -> int: ``` ### Rules - The array is circular: the last sector is adjacent to the first, so a cluster may wrap around from the end of the array to the start. - There are `n` possible split points, one between each pair of adjacent sectors, including the pair formed by the last and the first sector. The `k` chosen split points are distinct, so every cluster is a non-empty run of consecutive sectors. - With `k = 1`, the single split point opens the circle and all sectors form one cluster. ### Constraints - `1 <= n <= 400` - `1 <= k <= n` - `1 <= sectors[i] <= 100` ### Examples ```text Input: sectors = [1,2,3,4], k = 2 Output: 50 ``` The clusters `{4, 1}` (wrapping around the end) and `{2, 3}` cost `(4+1)^2 + (2+3)^2 = 25 + 25 = 50`. Splitting into `{1, 2}` and `{3, 4}` costs `9 + 49 = 58`, and no two sums adding to `10` can have squares totaling less than `5^2 + 5^2 = 50`. ```text Input: sectors = [5,1,1,5], k = 2 Output: 72 ``` The clusters `{5, 1}` and `{1, 5}` cost `36 + 36 = 72`. Splitting into `{5, 1, 1}` and `{5}` costs `49 + 25 = 74`, which is worse. ```text Input: sectors = [3,1,4,1,5], k = 3 Output: 66 ``` The clusters `{3, 1}`, `{4, 1}` and `{5}` have sums `4`, `5` and `5`, costing `16 + 25 + 25 = 66`. Three integers that add up to `14` cannot have a smaller sum of squares, so no other split beats it.

Overview: Split a circular array of positive sector values into exactly k contiguous clusters so that the sum of the squared cluster sums is as small as possible. Tests dynamic programming over prefix sums, handling wrap-around on a circular array, and reasoning about where optimal cuts can fall.

Airbnb logo
Airbnb
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given a circular array of n sectors, where each sector has a positive integer value. You need to select exactly k split points between adjacent sectors to divide the circular array into k clusters.

The cost of a cluster is the square of the sum of the sector values in that cluster. Choose the k split points so that the total cost, the sum of the costs of all clusters, is as small as possible.

Return the minimum total cost.

Function Signature

def min_split_cost(sectors: list[int], k: int) -> int:

Rules

  • The array is circular: the last sector is adjacent to the first, so a cluster may wrap around from the end of the array to the start.
  • There are n possible split points, one between each pair of adjacent sectors, including the pair formed by the last and the first sector. The k chosen split points are distinct, so every cluster is a non-empty run of consecutive sectors.
  • With k = 1 , the single split point opens the circle and all sectors form one cluster.

Constraints

  • 1 <= n <= 400
  • 1 <= k <= n
  • 1 <= sectors[i] <= 100

Examples

Input: sectors = [1,2,3,4], k = 2
Output: 50

The clusters {4, 1} (wrapping around the end) and {2, 3} cost (4+1)^2 + (2+3)^2 = 25 + 25 = 50. Splitting into {1, 2} and {3, 4} costs 9 + 49 = 58, and no two sums adding to 10 can have squares totaling less than 5^2 + 5^2 = 50.

Input: sectors = [5,1,1,5], k = 2
Output: 72

The clusters {5, 1} and {1, 5} cost 36 + 36 = 72. Splitting into {5, 1, 1} and {5} costs 49 + 25 = 74, which is worse.

Input: sectors = [3,1,4,1,5], k = 3
Output: 66

The clusters {3, 1}, {4, 1} and {5} have sums 4, 5 and 5, costing 16 + 25 + 25 = 66. Three integers that add up to 14 cannot have a smaller sum of squares, so no other split beats it.