<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 dfsSubsetSums(self, count_sum_dict, nums, i, sum_till_now, count_till_now):
        if i >= len(nums):
            count_sum = count_sum_dict[count_till_now]
            count_sum.append(sum_till_now)
            # seen_min, seen_max = count_sum[0], count_sum[1]
            # if sum_till_now > seen_max:
            #     count_sum[1] = sum_till_now
            # if sum_till_now < seen_min:
            #     count_sum[0] = sum_till_now

            return

        self.dfsSubsetSums(count_sum_dict, nums, i+1, sum_till_now + nums[i], count_till_now + 1)
        self.dfsSubsetSums(count_sum_dict, nums, i+1, sum_till_now, count_till_now)

    def getSubsetSums(self, nums):
        count_sum_dict = dict([[i, []] for i in range(len(nums) + 1)])
        self.dfsSubsetSums(count_sum_dict, nums, 0, 0, 0)

        return count_sum_dict

    def twoPointerMaxBelowThreshold(self, nums1, nums2, threshold):
        i = 0
        j = len(nums2) - 1
        max_num = -2e9

        while i < len(nums1) and j >= 0:
            if nums1[i] + nums2[j] > threshold:
                j -= 1
            else:
                max_num = max(max_num, nums1[i] + nums2[j])
                i += 1

        return max_num

    def twoPointerMinAboveThreshold(self, nums1, nums2, threshold):
        i = 0
        j = len(nums2) - 1
        min_num = 2e9

        while i < len(nums1) and j >= 0:
            if nums1[i] + nums2[j] < threshold:
                i += 1
            else:
                min_num = min(min_num, nums1[i] + nums2[j])
                j -= 1

        return min_num

    def minimumDifference(self, nums: List[int]) -> int:
        cs_dict1 = self.getSubsetSums(nums[:len(nums)//2])
        cs_dict2 = self.getSubsetSums(nums[len(nums)//2:])

        # print(f"CSDict1\n{cs_dict1}")
        # print(f"\n")
        # print(f"CSDict2\n{cs_dict2}")

        min_difference = 2e9

        for count, count_sums in cs_dict1.items():
            req_count = len(nums)//2 - count
            other_count_sums = cs_dict2[req_count]
            s1 = sorted(count_sums)
            s2 = sorted(other_count_sums)
            sum1 = self.twoPointerMaxBelowThreshold(s1, s2, sum(nums)/2)
            sum2 = self.twoPointerMinAboveThreshold(s1, s2, sum(nums)/2)

            min_difference = min(min_difference, sum(nums) - 2*sum1, 2*sum2 - sum(nums))

        return min_difference
2138