Skip to the code
+50 XP

#105 Bundles Where Cheapest and Priciest Fit

hard

A shop lets customers build a bundle from any non-empty group of items in prices. A bundle qualifies for a promotion when its cheapest item plus its most expensive item is at most limit. A bundle of one item counts its price twice.

Write countBundles(prices, limit) that returns how many different bundles qualify. Two bundles are different if they contain a different set of indexes, even when prices repeat. The count can be huge, so return it modulo 1000000007.

Example 1

Input: prices = [3,5,6,7], limit = 9
Output: 4

[3], [3, 5], [3, 6], and [3, 5, 6] qualify. For example [3, 6] has 3 + 6 = 9.

Example 2

Input: prices = [3,3,6,8], limit = 10
Output: 6

[3], [3], [3, 3], [3, 6], [3, 6], and [3, 3, 6].

Constraints

  • 0 <= prices.length <= 100000
  • 1 <= prices[i], limit <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).