League Spotlight Separation
Count fixed-size nonadjacent selections within a signed sum range
A football league has an ordered slate of matches. It wants to select exactly k matches for a special spotlight package.
Selecting match i changes the package's projected revenue by adjustments[i]. Adjustments may be negative. To leave room for regular coverage, two consecutive matches in the slate cannot both be selected.
Return the number of selections whose total adjustment is between low and high, inclusive.
Matches are distinguished by their zero-based positions, even when their adjustments are equal. A selection is an unordered set of positions and is counted once.
Selecting no matches has total adjustment zero.
Examples
Example 1
Input: adjustments = [4,-2,5,1,3], k = 2, low = 5, high = 7 Output: 2
The qualifying selections are positions {0, 3} and {0, 4}. Every other pair either contains consecutive positions or has a total outside the requested interval.
Example 2
Input: adjustments = [2,2,2,2], k = 2, low = 4, high = 4 Output: 3
Equal adjustments do not make matches interchangeable. The qualifying pairs are {0, 2}, {0, 3}, and {1, 3}.
Example 3
Input: adjustments = [-8,3,5], k = 0, low = -1, high = 0 Output: 1
There is one empty selection. Its total is zero, which lies in the requested interval.
Constraints
- 0 <= adjustments.length <= 56
- -10^9 <= adjustments[i] <= 10^9
- 0 <= k <= ceil(adjustments.length / 2)
- -10^12 <= low <= high <= 10^12
- The answer fits exactly in a JavaScript Number integer.
Use meet-in-the-middle backtracking. Within each half, enumerate only selections with no consecutive positions, recording their sum, cardinality, and selection status at the split boundary. Reject joins that select both boundary positions. For each compatible pair of cardinality buckets, subtract the number of sum pairs at most low - 1 from the number at most high. Sorted buckets support each count with two pointers. If M is the number of valid half-selections generated, the time is O(M log M) and auxiliary space is O(M). In the worst case M = O(phi^ceil(n/2)), where phi is the golden ratio. All sums and counts are exact integers in both reference languages.
Hints
Show hint 1Hint 1
Split the slate into two contiguous halves. Enumerate valid selections within each half rather than enumerating selections over the entire slate.
Show hint 2Hint 2
Group half-selections by their selected count and whether they select the position next to the split. Compatible groups can be joined by counting pairs of sorted sums in an interval.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you adapt the boundary information if every two selected positions had to be at least three positions apart?
- How would the join change if the slate were circular, so its first and last positions were also consecutive?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem