The Book Seller

You are in a book shop which sells n different books. You know the price and number of pages of each book, represented by the arrays price and pages respectively.

You have decided that the total price of your purchases will be at most x. What is the maximum number of pages you can buy? You can buy each book at most once.

Example 1:

Input: n = 4, x = 10, price = [4, 8, 5, 3], pages = [5, 12, 8, 1]

Output: 13

Explanation: There are 4 books. The price of each book is given in the price array and the

number of pages in each book is given in the pages array.

The total money we can spend is at most 10.

Possible useful choices:

- Book 1 → price = 4, pages = 5

- Book 3 → price = 5, pages = 8

Total price = 4 + 5 = 9 (within budget)

Total pages = 5 + 8 = 13

No other combination of books with total price ≤ 10 gives more pages,

so the maximum number of pages we can obtain is 13.

Example 2:

Input: n = 3, x = 5, price = [2, 2, 2], pages = [3, 3, 3]

Output: 6

Explanation: We can buy at most two books because: 2 + 2 = 4 ≤ 5

Buying any two books gives: pages = 3 + 3 = 6

Therefore, the maximum number of pages we can obtain is 6.

Now Your Turn!

Pick the correct output for the given input

Input: n = 5, x = 11, price = [3, 4, 5, 9, 4], pages = [4, 5, 7, 10, 3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ n ≤ 1000
  • 1 ≤ x ≤ 105
  • 1 ≤ prices[i] , pages[i] ​≤ 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int maxPages(int n, int x, vector<int>& prices, vector<int>& pages) {
}
};
Test Case

Input:

Pages
Prices
X