Since number of subsets can be 2402^{40} we cannot generate all of the subsets. But if we divide the input into two halves then, generate all subset of the halves, then number of subsets will be 2202^{20}, so we try a meet-in-the-middle approach.

When divided into two halves, a subset of the original input is created by merging subsets from the two groups. so if we generate two lists AA, BB of all subset sums of the two halves. then subset sum of original input is created by adding two vectos from A,BA, B. Now we can binary search for the kkth lexicographically smallest vector. First we binary search on x coordinate (similar to two sum problem), then after fixing x, we can binary search for y coordinate (again similar to two sum problem). a naive implementation would be 2n/2n22^{n/2}n^2but by using two pointer approach it can be made into 2n/2n2^{n/2}n

Statistics

60% Solution Ratio
ArcturusEarliest, Dec '21
nusuBotFastest, 2.6s
nusuBotLightest, 50 MB
ArcturusShortest, 4065B
Toph uses cookies. By continuing you agree to our Cookie Policy.