This problem is mainly related to the Partition Problem optimization problem with the additional restriction of equal parts. I will prove that adding this constraint does not make the task easier.
NP-Hardness proof: Suppose that there is an algorithm A
that solves this problem in polynomial time, we can solve Partition-Problem in polynomial time.
Partition(S): for i in range(|S|): S += {0} result <- A(S\2,S\2) //arbitrary split S into 2 parts if result is a partition: //simple to check, since partition is NP. return true. return false //no partition
Correctness:
If there is a section denoted as (S1, S2) [it is assumed that S2 has more elements], at the iteration | S2 | - | S1 | [Those. when adding | S2 | - | S1 | zeros]. The entrance to A
will contain enough zeros, so we can return two arrays of equal length: S2, S1 + {0,0, ..., 0}, which will be a partition on S
, and the algorithm will give true,
If the algorithm gives true and iteration k
, we had two arrays: S2, S1, with the same number of elements and equal values. removing k
zeros from arrays, we get a partition with the original S, so S has a partition.
polynomial: Suppose A
takes time P(n)
, the algorithm we created will take time n*P(n)
, which is also a polynomial.
Conclusion:
If this problem is solvable in polynomial time, then the Partion problem, and, therefore, P = NP. based on this: this problem is NP-Hard.
Since this problem is NP-Hard, you probably need an exponential algorithm for an exact solution. One of them is simple backtracking [I leave it as a reading exercise to implement a backtracking solution)
EDIT : as @jpalecek mentioned: simply by creating the abbreviation: S->S+(0,0,...,0)
[k times 0], one can directly prove NP-hardness by reduction. the polynomial is trivial, and the correctness is very similar to the likelihood proof described above: [if there is a section, adding “balancing” zeros is possible; another direction just trims those zeros]