Top Tweets for #l4vs
@rpettiness slide 1&2 New
bisa oren

Clix fans talking about globals after coming 6th inna div cup

فاضيه كام
لايجوني الا الدفيعين

Editorial Day - 6
Problem: Partition Array Into Two Arrays to Minimize Sum Difference [LC - 2035]
The problem looks like a partition DP (similar to Subset Sum). However, the constraints make it impossible for us to generate all subsets(2^30 >> 1e8).
Let m = nums.size()/2;
Here, nums.size() <= 30, meaning there are 2^30 possible subsets, which would easily TLE. Moreover, we're required to pick exactly m elements in one partition, so simply generating all subsets isn't sufficient. We also need to keep track of how many elements each subset contains.
The key observation is that 30 is small enough to split into two halves of 15 elements each, allowing us to use the "Meet in the Middle" technique.
=> Step 1: Split the Array
Split the array into two halves of size m.
Now, instead of considering all 2^(2m) subsets together, we process each half independently.
For every subset of a half, compute:
The number of selected elements.
The sum of the selected elements.
Since each half contains at most 15 elements, this requires only 2^15 subsets, which is easily manageable.
=> Step 2: Group Subsets by Their Size
We're required to choose exactly m elements for the first partition.
Suppose we pick i elements from the first half.
Then we must pick m - i elements from the second half. So instead of storing all subset sums together, group them according to the number of selected elements.
For example,
part1[3] = all subset sums formed by choosing 3 elements from the first half.
Similarly, we build the same structure for the second half.
This guarantees that every valid combination contains exactly m selected elements.
=> Step 3: Which Second-Half Sum Do We Need?
Let,
S = total sum of the array.
x = chosen sum from the first half.
y = chosen sum from the second half.
Then the first partition has sum = x + y
The second partition automatically has sum = S - (x + y)
We want to minimize
|(x + y) - (S - (x + y))|
which is equivalent to making x + y as close as possible to S / 2.
So we get our partition target = S/2.
Therefore, for every subset sum x, we search for a subset sum y satisfying {y = S / 2 - x} closest.
=> Step 4: Binary Search the Best Sum from the Second Half
Each collection of subset sums is stored inside a sorted set.
For every sum x of size i in the first half, calculate req = target - x, and perform a lower_bound() in the corresponding set of size(m - i) of the second half.
The closest answer can only be the first value ≥ target, or the previous value.
Check both and update the minimum difference.
=> Final Answer
For every possible way of selecting i elements from the first half, and m - i elements from the second half, find the second-half subset sum closest to S / 2.
The smallest absolute difference obtained over all such combinations is the answer.
Overall Complexity:
TC: O(m · 2^m + 2^m log(2^m)) [m = n / 2]
SC: O(2^m)
A really nice Meet in the Middle problem that combines Bitmasking, Binary Search (lower_bound), and good implementation.
See the code below for better understanding :)
![who_shiv4m's tweet photo. Editorial Day - 6
Problem: Partition Array Into Two Arrays to Minimize Sum Difference [LC - 2035]
The problem looks like a partition DP (similar to Subset Sum). However, the constraints make it impossible for us to generate all subsets(2^30 >> 1e8).
Let m = nums.size()/2;
Here, nums.size() <= 30, meaning there are 2^30 possible subsets, which would easily TLE. Moreover, we're required to pick exactly m elements in one partition, so simply generating all subsets isn't sufficient. We also need to keep track of how many elements each subset contains.
The key observation is that 30 is small enough to split into two halves of 15 elements each, allowing us to use the "Meet in the Middle" technique.
=> Step 1: Split the Array
Split the array into two halves of size m.
Now, instead of considering all 2^(2m) subsets together, we process each half independently.
For every subset of a half, compute:
The number of selected elements.
The sum of the selected elements.
Since each half contains at most 15 elements, this requires only 2^15 subsets, which is easily manageable.
=> Step 2: Group Subsets by Their Size
We're required to choose exactly m elements for the first partition.
Suppose we pick i elements from the first half.
Then we must pick m - i elements from the second half. So instead of storing all subset sums together, group them according to the number of selected elements.
For example,
part1[3] = all subset sums formed by choosing 3 elements from the first half.
Similarly, we build the same structure for the second half.
This guarantees that every valid combination contains exactly m selected elements.
=> Step 3: Which Second-Half Sum Do We Need?
Let,
S = total sum of the array.
x = chosen sum from the first half.
y = chosen sum from the second half.
Then the first partition has sum = x + y
The second partition automatically has sum = S - (x + y)
We want to minimize
|(x + y) - (S - (x + y))|
which is equivalent to making x + y as close as possible to S / 2.
So we get our partition target = S/2.
Therefore, for every subset sum x, we search for a subset sum y satisfying {y = S / 2 - x} closest.
=> Step 4: Binary Search the Best Sum from the Second Half
Each collection of subset sums is stored inside a sorted set.
For every sum x of size i in the first half, calculate req = target - x, and perform a lower_bound() in the corresponding set of size(m - i) of the second half.
The closest answer can only be the first value ≥ target, or the previous value.
Check both and update the minimum difference.
=> Final Answer
For every possible way of selecting i elements from the first half, and m - i elements from the second half, find the second-half subset sum closest to S / 2.
The smallest absolute difference obtained over all such combinations is the answer.
Overall Complexity:
TC: O(m · 2^m + 2^m log(2^m)) [m = n / 2]
SC: O(2^m)
A really nice Meet in the Middle problem that combines Bitmasking, Binary Search (lower_bound), and good implementation.
See the code below for better understanding :)](https://pbs.twimg.com/media/HOj34pPbUAAac0G.jpg)
Trends for you
Most Popular Users

Elon Musk 
@elonmusk
239.3M followers

Barack Obama 
@barackobama
118.5M followers

Cristiano Ronaldo 
@cristiano
114.3M followers

Donald J. Trump 
@realdonaldtrump
111.7M followers

Narendra Modi 
@narendramodi
106.9M followers

Rihanna 
@rihanna
98.6M followers

NASA 
@nasa
92M followers

Justin Bieber 
@justinbieber
91.7M followers

KATY PERRY 
@katyperry
89.9M followers

Taylor Swift 
@taylorswift13
83.9M followers

Lady Gaga 
@ladygaga
75.4M followers

Virat Kohli 
@imvkohli
73.2M followers

Kim Kardashian 
@kimkardashian
70.8M followers

YouTube 
@youtube
68.6M followers

Neymar Jr 
@neymarjr
66.3M followers

Bill Gates 
@billgates
65.1M followers

Selena Gomez 
@selenagomez
63M followers

The Ellen Show
@theellenshow
62.1M followers

CNN 
@cnn
61.6M followers

X 
@x
60.6M followers








