<p>You are given an integer array <code>nums</code> and an integer <code>goal</code>.</p>

<p>You want to choose a subsequence of <code>nums</code> such that the sum of its elements is the closest possible to <code>goal</code>. That is, if the sum of the subsequence&#39;s elements is <code>sum</code>, then you want to <strong>minimize the absolute difference</strong> <code>abs(sum - goal)</code>.</p>

<p>Return <em>the <strong>minimum</strong> possible value of</em> <code>abs(sum - goal)</code>.</p>

<p>Note that a subsequence of an array is an array formed by removing some elements <strong>(possibly all or none)</strong> of the original array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,-7,3,5], goal = 6
<strong>Output:</strong> 0
<strong>Explanation:</strong> Choose the whole array as a subsequence, with a sum of 6.
This is equal to the goal, so the absolute difference is 0.
</pre>

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

<pre>
<strong>Input:</strong> nums = [7,-9,15,-2], goal = -5
<strong>Output:</strong> 1
<strong>Explanation:</strong> Choose the subsequence [7,-9,-2], with a sum of -4.
The absolute difference is abs(-4 - (-5)) = abs(1) = 1, which is the minimum.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3], goal = -7
<strong>Output:</strong> 7
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 40</code></li>
	<li><code>-10<sup>7</sup> &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= goal &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def minAbsDifference(self, nums: List[int], goal: int) -> int:
        def generate_subsequence_sums(arr):
            k = len(arr)
            sums = set()
            for i in range(1 << k):  # 2^n possible subsequences
                total = 0
                for j in range(k):
                    if i & (1 << j):
                        total += arr[j]
                sums.add(total)
            return sorted(sums)

        # Step 1: Split the nums array into two halves
        n = len(nums)
        left_part = nums[:n // 2]
        right_part = nums[n // 2:]

        # Step 2: Generate all subsequence sums for both halves
        left_sums = generate_subsequence_sums(left_part)
        right_sums = generate_subsequence_sums(right_part)

        # Step 3: Initialize result with large value
        result = float('inf')

        # Step 4: For each sum in left_sums, find the closest sum in right_sums
        for l_sum in left_sums:
            remaining = goal - l_sum

            # Binary search for the closest sum in right_sums
            idx = bisect_left(right_sums, remaining)

            # Check the closest values in right_sums
            if idx < len(right_sums):
                result = min(result, abs(l_sum + right_sums[idx] - goal))
            if idx > 0:
                result = min(result, abs(l_sum + right_sums[idx - 1] - goal))

        return result

151700