Fair Division Problem - Problem

You need to divide N items with integer values between two people so that the absolute difference in their total values is minimized.

Given an array values where values[i] represents the value of the i-th item, return the minimum possible absolute difference between the two groups' totals.

Example: If items have values [1, 6, 11, 5], one optimal division is {1, 5} and {6, 11}, giving totals of 6 and 17, with absolute difference |6 - 17| = 11.

Input & Output

Example 1 — Basic Case
$ Input: values = [1, 6, 11, 5]
Output: 1
💡 Note: Optimal division: {6, 5} with sum 11 and {1, 11} with sum 12. Absolute difference = |11 - 12| = 1.
Example 2 — Perfect Split
$ Input: values = [1, 1, 3, 5]
Output: 0
💡 Note: Perfect division: {1, 5} and {1, 3} both have sum 6. Absolute difference = |6 - 6| = 0.
Example 3 — Single Item
$ Input: values = [100]
Output: 100
💡 Note: One person gets the item (100), the other gets nothing (0). Difference = |100 - 0| = 100.

Constraints

  • 1 ≤ values.length ≤ 20
  • 1 ≤ values[i] ≤ 100

Visualization

Tap to expand
INPUTALGORITHMRESULT16115Items: [1, 6, 11, 5]Total Sum = 23Target = 23/2 = 11.5Goal: Find subset sumclosest to 11.51Initialize DP[0] = true2Process each item value3Update achievable sums4Find sum closest to 11.5Achievable sums:{0, 1, 5, 6, 11, 12, 16, 17, 22, 23}Closest to 11.5 is 11Group 1: {11}Sum = 11Group 2: {1,6,5}Sum = 12|11 - 12| = 1Answer: 1Key Insight:Transform fair division into subset sum problem - find achievable sum closest to half the totalTutorialsPoint - Fair Division Problem | Dynamic Programming
Asked in
Google 15 Amazon 12 Microsoft 8
26.7K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen