Split 2n integers into two arrays of n elements each with the smallest sum difference

Quick Overview

Split an array of 2n integers, which may be negative, into two arrays of exactly n elements each so the absolute difference of their sums is as small as possible, with n up to 15. It tests splitting an exponential search into halves, grouping subset sums by size, and binary search over sorted sums.

Split 2n integers into two arrays of n elements each with the smallest sum difference

Company: Zomato

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given an integer array `nums` of `2 * n` integers. Partition `nums` into two arrays of length `n` so that the absolute difference of the sums of the two arrays is as small as possible. To partition `nums`, put each element of `nums` into exactly one of the two arrays. Return the minimum possible absolute difference. ### Function Signature ```python def minimum_difference(nums: list[int]) -> int: ``` ### Rules - Each element goes into exactly one of the two arrays, and each array gets exactly `n` elements. Order inside the arrays does not matter. - Return only the minimum absolute difference of the two sums, as an integer. ### Constraints - `1 <= n <= 15` - `len(nums) == 2 * n` - `-10^7 <= nums[i] <= 10^7` ### Examples ```text Input: nums = [3, 9, 7, 3] Output: 2 ``` One optimal partition is `[3, 9]` and `[7, 3]`, with sums 12 and 10. The other choices are `[3, 7]` with `[9, 3]` (difference 2) and `[3, 3]` with `[9, 7]` (difference 10). ```text Input: nums = [-36, 36] Output: 72 ``` The only partition is `[-36]` and `[36]`, with sums -36 and 36. ```text Input: nums = [1, 2, 3, 4, 5, 100] Output: 91 ``` The array holding 100 also needs two more elements, and the smallest pair is 1 and 2, so the best split is `[100, 1, 2]` (sum 103) against `[3, 4, 5]` (sum 12). Putting 100 alone on one side would give 85, but that violates the equal-length rule.

Overview: Split an array of 2n integers, which may be negative, into two arrays of exactly n elements each so the absolute difference of their sums is as small as possible, with n up to 15. It tests splitting an exponential search into halves, grouping subset sums by size, and binary search over sorted sums.

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

You are given an integer array nums of 2 * n integers. Partition nums into two arrays of length n so that the absolute difference of the sums of the two arrays is as small as possible. To partition nums, put each element of nums into exactly one of the two arrays.

Return the minimum possible absolute difference.

Function Signature

def minimum_difference(nums: list[int]) -> int:

Rules

  • Each element goes into exactly one of the two arrays, and each array gets exactly n elements. Order inside the arrays does not matter.
  • Return only the minimum absolute difference of the two sums, as an integer.

Constraints

  • 1 <= n <= 15
  • len(nums) == 2 * n
  • -10^7 <= nums[i] <= 10^7

Examples

Input: nums = [3, 9, 7, 3]
Output: 2

One optimal partition is [3, 9] and [7, 3], with sums 12 and 10. The other choices are [3, 7] with [9, 3] (difference 2) and [3, 3] with [9, 7] (difference 10).

Input: nums = [-36, 36]
Output: 72

The only partition is [-36] and [36], with sums -36 and 36.

Input: nums = [1, 2, 3, 4, 5, 100]
Output: 91

The array holding 100 also needs two more elements, and the smallest pair is 1 and 2, so the best split is [100, 1, 2] (sum 103) against [3, 4, 5] (sum 12). Putting 100 alone on one side would give 85, but that violates the equal-length rule.