Last time I found an interesting problem, and I got stuck on it.
For n numbers a [1], ..., a [n] in increasing order and the number k (1 <= n, k <= 10 ^ 5).
Let's say that we sort all possible subsets of a given sequence by sum. We must find the sum of the kth such subset.
For instance:
n = 4, k = 8
a = {2, 7, 8, 15}
1: {2}, sum = 2
2: {7}, sum = 7
3: {8}, sum = 8
4: {2, 7}, sum = 9
5: {2, 8}, sum = 10
6: {7, 8}, sum = 15
7: {15}, sum = 15
8: {2, 15}, sum = 17
...
k = 8, so the answer = 17 (a subset of {2,15}).
Of course, we can generate all possible subsets, and the whole solution works in O (2 ^ n * n), but I'm looking for something more intelligent - linear or at least O (nk).
source share