<p>You are given an integer array <code>nums</code> of <code>2 * n</code> integers. You need to partition <code>nums</code> into <strong>two</strong> arrays of length <code>n</code> to <strong>minimize the absolute difference</strong> of the <strong>sums</strong> of the arrays. To partition <code>nums</code>, put each element of <code>nums</code> into <strong>one</strong> of the two arrays.</p>

<p>Return <em>the <strong>minimum</strong> possible absolute difference</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="example-1" src="https://assets.leetcode.com/uploads/2021/10/02/ex1.png" style="width: 240px; height: 106px;" />
<pre>
<strong>Input:</strong> nums = [3,9,7,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> One optimal partition is: [3,9] and [7,3].
The absolute difference between the sums of the arrays is abs((3 + 9) - (7 + 3)) = 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [-36,36]
<strong>Output:</strong> 72
<strong>Explanation:</strong> One optimal partition is: [-36] and [36].
The absolute difference between the sums of the arrays is abs((-36) - (36)) = 72.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="example-3" src="https://assets.leetcode.com/uploads/2021/10/02/ex3.png" style="width: 316px; height: 106px;" />
<pre>
<strong>Input:</strong> nums = [2,-1,0,4,-2,-9]
<strong>Output:</strong> 0
<strong>Explanation:</strong> One optimal partition is: [2,4,-9] and [-1,0,-2].
The absolute difference between the sums of the arrays is abs((2 + 4 + -9) - (-1 + 0 + -2)) = 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 15</code></li>
	<li><code>nums.length == 2 * n</code></li>
	<li><code>-10<sup>7</sup> &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
</ul>

class Solution:

    # Meet in the middle approach
    def minimumDifference(self, nums: List[int]) -> int:

        mid = len(nums) // 2
        leftSplit = nums[0 : mid]
        rightSplit = nums[mid :]

        def buildSums(numsTaken, ind, splitList, storeIn):
            
            if ind == len(splitList):
                # Only keep sums where atleast 1 element was used
                if len(numsTaken) > 0:
                    # Insert the numbers in sorted order so they can be searched faster later 
                    bisect.insort_left(storeIn[len(numsTaken)], sum(numsTaken))
                return

            numsTaken.append(splitList[ind])
            buildSums(numsTaken, ind + 1, splitList, storeIn)
            # Backtrack and not use this num for sum
            numsTaken.pop()
            buildSums(numsTaken, ind + 1, splitList, storeIn)


        leftSplit_sums = collections.defaultdict(list)
        rightSplit_sums = collections.defaultdict(list)

        # Use bactrack to try out all possible sums in list 1 and list 2 
        # and store them in a map. The whole list size is 30 max, which will
        # give you 2^30 subsets which is a lot. 2^15 subsets max is much more 
        # managable without giving TLE
        buildSums([], 0, leftSplit, leftSplit_sums)
        buildSums([], 0, rightSplit, rightSplit_sums)

    
        total = sum(nums) # The toatal of all elements 
        # To get minimum difference between 2 lists we need the sum to be 
        # equal to total / 2, so that's our target
        target = total // 2

        # If we use the left and right split as it is 
        min_diff = abs(sum(leftSplit) - sum(rightSplit))

        # Take 1 to n-1 elements from left list to build sum
        # Since we considered the case of taking 0 or n elements from leftList above 
        # At 0 from left, we use all from right and the 2 lists get filpped 
        # At n from left it is the same split we had before
        for i in range(1, mid):

            leftSums = leftSplit_sums[i]
            rightSums = rightSplit_sums[mid-i]

            for leftSum in leftSums:
                remaining = target - leftSum
                # Binary seach the number we need to meet target in right sums list 
                # Faster than linear seach 
                ind = bisect.bisect_left(rightSums, remaining)
                if ind == len(rightSums):
                    ind -= 1
                rightSum = rightSums[ind]
                n_sum = leftSum + rightSum
                compliment_sum = total - n_sum
                diff = abs(n_sum - compliment_sum)
                min_diff = min(diff, min_diff)

        return min_diff
3894