0

Given an array of size 2n, I need to divide it into two equal subarrays of length n so that their difference is minimum. The maximum length of the array is 200 where each element is within 1 to 100. But I would want a more general solution if possible I have been trying to do this problem with dynamic programming but couldn't come out with any satisfactory results. The problems I am facing

  1. Can't figure out a proper memorization method. Brute force approach would take the worst case number of operations to 200c100, which is not plausible.
  2. The problem is not exactly minimization or maximization of sum of a subset. For example, I need to get my sum close to 500 using 40 elements, suppose I have taken 25 elements and sum is 290, now I need to get a value as close to 210 possible. This is neither the minimization of the remaining 15 elements, nor the maximization. Again, looping through the numbers close to 210 doesn't seem plausible either in terms of time complexity (i.e: 209, 211, 208, 212, ...).
  • Does this answer your question? [Use dynamic programming to find a subset of numbers whose sum is closest to given number M](https://stackoverflow.com/questions/33717186/use-dynamic-programming-to-find-a-subset-of-numbers-whose-sum-is-closest-to-give) – Raymond Chen Feb 13 '22 at 21:13
  • 2
    Note that you're using the wrong term in the problem description. A subarray is a contiguous part of the source. Therefore there is no choice at all involved in choosing two subarrays of length n. You're apparently talking about subsets. – Gene Feb 13 '22 at 21:22
  • 1
    Since the problem and constraints match a [question on Leetcode](https://leetcode.com/problems/partition-equal-subset-sum/), you might want to look at [some](https://stackoverflow.com/questions/66290598/partition-equal-subset-sum) of the [previous posts](https://stackoverflow.com/questions/54395409/solution-performance-dp-hashtable-for-partition-equal-subset-sum) about this problem. – kcsquared Feb 13 '22 at 21:58

0 Answers0