Ctrl K

The k-th Largest Pick Total

Hard
You are given an integer array values and an integer k. You may pick any set of elements from the array (including the empty one, which totals 0, or the whole array) and add them up. There are 2ⁿ picks, each with its own total. List the totals of all picks in decreasing order, keeping repeats: picks with the same total are still different picks. Return the k-th total in that list (1-based).
Example 1
values=[3, -1, 2], k=4 → 2

All subset totals in decreasing order: 5, 4, 3, 2, 2, 1, 0, -1. The fourth one is 2.

Example 2
values=[-5, -7], k=1 → 0

The best choice is to take nothing: the empty pick totals 0.

Example 3
values=[4, 4, 4], k=3 → 8

Totals in decreasing order: 12, 8, 8, 8, 4, … Equal totals count separately, so the third one is also 8.

Constraints
  • 1 ≤ n = len(values) ≤ 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
  • 1 ≤ k ≤ min(2000, 2ⁿ)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsGrowing Capital Project by Project HardMost Orders by Their Deadlines HardNarrowest Range Touching Every List HardCrew Paid by Skill HardThe Slowest Sets the Squad's Pace Hard
loading Python…
Input
values=[3, -1, 2], k=4
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.