How to divide a set into two sets such that the difference of the average is minimum?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Minimizing the difference between the averages of two subsets is not exactly the same as the classic equal-sum partition problem. The subset sizes matter too. A correct solution needs to consider both the number of chosen elements and their sum.
Write the objective carefully
Suppose the full set has total sum T and size n. If one subset has size k and sum s, then the other subset has size n-k and sum T-s.
The average difference is:
After algebra, this is equivalent to minimizing:
for some valid subset size k, while also accounting for the denominator k(n-k) in the original expression.
That is why simply minimizing the raw difference of subset sums is not always enough.
A dynamic programming strategy
A useful exact approach is dynamic programming over:
- how many elements were chosen
- what sum they produce
For each possible subset size k, you track which sums are achievable. Then you evaluate which reachable sum gives the smallest average difference.
A compact Python version for positive integers looks like this:
This gives you the best average difference along with the chosen subset size and sum.
Why subset size matters
Imagine two candidate subsets with similar sums but very different sizes. Their averages can be quite different. That is the key reason the problem is more subtle than ordinary equal-sum partitioning.
For example, compare:
- subset A with sum
10and size2, average5 - subset B with sum
10and size5, average2
Same sum, very different average. So the algorithm cannot ignore how many elements each subset contains.
Recovering the actual subsets
The DP above finds the best objective value, but not the exact chosen elements. To reconstruct the subsets, store predecessor information while filling the DP table, or backtrack from the winning (k, s) state.
That adds implementation detail, but the principle stays the same: track which choices created each reachable state.
If you only need the minimum difference value, the simpler state-tracking version is enough.
Complexity and practical limits
The DP approach is exact, but it depends on the sum range. If the numbers are large, the number of reachable sums can become expensive.
That makes the approach practical mainly when:
- the set is not too large
- the values are moderate enough for sum-based DP
For larger inputs, you may need heuristics, approximation, or problem-specific constraints.
Common Pitfalls
The biggest pitfall is reducing the problem to ordinary equal-sum partitioning. Equal sums do not guarantee equal averages unless the subset sizes are also controlled.
Another issue is forgetting that empty subsets are invalid for average computation. Both subsets must contain at least one element.
It is also easy to implement a DP that tracks only sums and not subset sizes. That loses crucial information and can produce the wrong answer.
Finally, for large value ranges, the exact DP may become too expensive. In those cases, a heuristic may be more realistic, but it should be labeled as approximate rather than exact.
Summary
- Minimizing average difference is not the same as minimizing sum difference alone.
- A correct exact method tracks both subset size and achievable sums.
- Dynamic programming works well for moderate input sizes and value ranges.
- Reconstructing the actual subsets requires storing predecessor information.
- If the input is large, consider whether an approximate strategy is acceptable.
Related reading
- How to divide number into integer pieces that are each a multiple of n?
- How to do a range update in Binary Indexed Tree or Fenwick Tree?
- How to do binary search by table with known data order in specific fields SQL
- how to do binary search in one lie model?
- How to divide an array into 3 parts with the sum of each part roughly equal
- How to do error handling with EasyNetQ / RabbitMQ
- How to divide a set into two subsets such that difference between the sum of numbers in two sets is minimal?
- How to dynamically freeze weights after compiling model in Keras?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.