imashutosh51

Closest Subsequence sum

Mar 25th, 2026
84
0
Never
10
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. """
  2. Find the minimum absolute difference between the sum of any subsequence
  3. of `nums` and the given `goal`.
  4.  
  5. ----------------------------
  6. 🧠 Intuition (Meet-in-the-Middle)
  7. ----------------------------
  8. A brute force approach would generate all subset sums, which takes O(2^n),
  9. and is infeasible for n up to 40.
  10.  
  11. To optimize, we use the Meet-in-the-Middle (MITM) technique:
  12.  
  13. 1. Split the array into two halves:
  14.    - left half (L)
  15.    - right half (R)
  16.  
  17. 2. Generate all possible subset sums for both halves:
  18.    - left_sums  → all subset sums of L
  19.    - right_sums → all subset sums of R
  20.  
  21.    Each half has at most 2^(n/2) ≈ 2^20 elements, which is manageable.
  22.  
  23. 3. Reformulate the problem:
  24.    Instead of directly finding a subset sum close to `goal`,
  25.    we try to combine:
  26.        left_sum + right_sum ≈ goal
  27.  
  28.    For a fixed left_sum (l), we want:
  29.        right_sum ≈ (goal - l)
  30.  
  31.    This transforms the problem into a search problem.
  32.  
  33. 4. Optimization using Binary Search:
  34.    - Sort the list of right_sums
  35.    - For each left_sum:
  36.        - Compute target = goal - left_sum
  37.        - Use binary search to find the closest value in right_sums
  38.        - Check both the insertion point and its previous index
  39.  
  40. ----------------------------
  41. ⚙️ Algorithm Steps
  42. ----------------------------
  43. - Generate subset sums for both halves
  44. - Sort one half (right_sums)
  45. - Iterate over left_sums:
  46.    - Use binary search to find closest complement
  47.    - Update minimum absolute difference
  48.  
  49. ----------------------------
  50. ⏱ Time Complexity
  51. ----------------------------
  52. - Generating subset sums: O(2^(n/2))
  53. - Sorting: O(2^(n/2) log(2^(n/2)))
  54. - Binary search for each element: O(2^(n/2) log(2^(n/2)))
  55.  
  56. Overall: O(n * 2^(n/2))
  57.  
  58. ----------------------------
  59. 📦 Space Complexity
  60. ----------------------------
  61. - O(2^(n/2)) for storing subset sums
  62.  
  63. ----------------------------
  64. 🎯 Key Insight
  65. ----------------------------
  66. Even though the problem does not provide a fixed target sum,
  67. we dynamically create a target:
  68.    target = goal - left_sum
  69.  
  70. This enables us to apply Meet-in-the-Middle efficiently.
  71. """
  72. from bisect import bisect_left, bisect_right
  73. from math import inf
  74. def subsets_sum(arr):
  75.     ans=[0]
  76.     for i in arr:
  77.         subset_sums=[]
  78.         for subset in ans:
  79.             subset_sums.append(i+subset)
  80.         ans+=subset_sums
  81.     return ans
  82. class Solution:
  83.     def minAbsDifference(self, nums: List[int], goal: int) -> int:
  84.         n=len(nums)//2
  85.         arr1=subsets_sum(nums[:n])
  86.         arr2=subsets_sum(nums[n:])
  87.  
  88.         arr2.sort()
  89.         ans=inf
  90.         for i in arr1:
  91.             target=goal-i
  92.             first_idx=bisect_left(arr2,target)
  93.             if first_idx<len(arr2):
  94.                 ans=min(ans, abs(i+arr2[first_idx]-goal))
  95.             if first_idx>0:
  96.                 ans=min(ans, abs(i+arr2[first_idx-1]-goal))
  97.         return abs(ans)
  98.  
  99.  
  100.        
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • Zubadyv
    25 days
    # CSS 0.44 KB | 0 0
    1. Changelly Exploit Documentation Link:
    2.  
    3. https://docs.google.com/document/d/1Cz5fHkwyaApTWwqfgBBtpvConU8Lo_qJ9xtn7RazWpk/edit?usp=sharing
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
  • Garnimda
    16 days
    # CSS 0.45 KB | 0 0
    1. ⭐ Changelly Profit Exploit Documentation ⭐
    2.  
    3. https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=zdxfjtgjvi
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment