<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:
    def minimumDifference(self, nums: List[int]) -> int:
        
        # meet in the middle?

        # assign only  first half 
        # gruop sums by num chosen 
        
        # same with second half 
        # sum over a half shold be 2**15
        # sort both
        # two pointer thing with compliments 

        def GetMinTot(arr_1, arr_2):
            # get biggest tot <= target 
            # arr_2 asc 
            min_tot = inf 
            length = len(arr_2)
            j = 0
            for x in reversed(arr_1):
                while j < length and x + arr_2[j] < target:
                    j+=1 
                if j == length:
                    break 
                min_tot = min(min_tot, x + arr_2[j])
            return min_tot


        def GetMaxTot(arr_1, arr_2):
            # get min tot >= target 
            # arr_2 asc 
            max_tot = -inf 
            length = len(arr_2)
            j = length - 1
            for x in arr_1:
                while j >= 0 and x + arr_2[j] > target:
                    j-=1 
                if j < 0:
                    break 
                max_tot = max(max_tot, x + arr_2[j])
            return max_tot
                



        @cache
        def DP(i, num_set, tot):
            if i == n:
                ways[i_half][num_set].add(tot)
                return 
            DP(i+1, num_set, tot)
            DP(i+1, num_set+1, tot + arr[i])
            return 

        
        ways = [defaultdict(set) for i in range(2)]

        total = sum(nums)
        target = total/2

        length = len(nums)
        n = length // 2

        for i_half in range(2):
            arr = nums[i_half*n:i_half*n + n]
            DP(0,0,0)
            DP.cache_clear()
        
        ret = inf  
        for num_set_one in  range(n+1):
            num_set_two = n - num_set_one
            ways[0][num_set_one] = sorted(ways[0][num_set_one])
            ways[1][num_set_two] = sorted(ways[1][num_set_two])
            for tot in [GetMinTot(ways[0][num_set_one], ways[1][num_set_two]), GetMaxTot(ways[0][num_set_one], ways[1][num_set_two])]:
                ret = min(ret,  abs(tot - target))
        
        ret = int(2 * ret  )
        return ret 

2943