#BS0000486. Chia mảng thành hai nhóm có độ chênh lệch tổng nhỏ nhất (Partition Array Into Two Arrays to Minimize Sum Difference)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chia mảng thành hai nhóm có độ chênh lệch tổng nhỏ nhất (Partition Array Into Two Arrays to Minimize Sum Difference)
Partition Array Into Two Arrays to Minimize Sum Difference
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
An array contains integers . Divide all its elements into two groups and , each containing exactly elements.
Each position in the array must be assigned to exactly one group, even when several positions hold equal values. Elements of a group need not be consecutive in the original array. The value of a group is the sum of its elements.
Among all valid partitions, find the minimum absolute difference between the two sums:
Input
- The first line contains an integer , the number of elements in each group after partitioning.
- The second line contains exactly integers in their original order.
Output
Print a nonnegative integer: the minimum absolute difference between the sums of the two groups of exactly elements each.
Subtasks
- Subtask 1 (20 points): ; for every ; the array has exactly elements.
- Subtask 2 (30 points): ; for every ; the array has exactly elements.
- Subtask 3 (50 points): ; for every ; the array has exactly elements.
Examples
Input 1
2
3 9 7 3
Output 1
2
Explanation 1
Here , so each group contains two elements. The first two positions give group a sum of . The remaining positions give group a sum of . The difference is .
The total is . A difference of would require both sums to be , but the possible sums of two distinct positions are . Every difference is even, so no difference smaller than is possible.

Input 2
1
-36 36
Output 2
72
Explanation 2
Since , each group takes one element. The sums are and , giving . Exchanging the group labels does not affect the result.
Input 3
3
2 -1 0 4 -2 -9
Output 3
0
Explanation 3
There are six elements and each group needs three. Choose for the first group, with sum . The remaining elements also sum to . Equal sums give a difference of , the smallest possible value.