#C8484. Minimum Subset Sum Difference

    ID: 52471 Type: Default 1000ms 256MiB

Minimum Subset Sum Difference

Minimum Subset Sum Difference

You are given an array of N integers. Your task is to partition the array into two subsets such that the absolute difference between the sums of the two subsets is minimized.

Let \( S \) be the total sum of all elements and \( s \) be the sum of one subset. The goal is to minimize \( |S - 2s| \). This problem can be solved efficiently using dynamic programming.

Example:

Input: N = 4, arr = [1, 6, 11, 5]
Output: 1
Explanation: One optimal partition is {1, 5, 6} and {11}, giving sums 12 and 11, respectively. Thus, the minimal difference is 1.

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  • The first line contains a single integer N, the number of elements in the array.
  • The second line contains N space-separated integers representing the array elements.

outputFormat

Output a single integer to standard output (stdout), which is the minimum absolute difference between the sums of the two subsets.

## sample
4
1 6 11 5
1

</p>