This is a verified interview question from Deutsche-bank. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "Maximum Number of Products Alex Can Buy - Deutsche Bank Online Assessment IIT BHU" covers key patterns like Arrays.
"Alex is in a shop that sells **N products**. You are given an array `A` of `N` positive integers, indexed from `0` to `N-1`. `A[K]` represents the price of the `K`-th product. Alex can spend at most **B** in total. He wants to buy **as many products as possible**. He can buy each product **at most once**, and the total price of all purchased products must be **less than or equal to B**. ## Function ```cpp int solution(int B, vector<int> &A); ``` Given an integer `B` and an array `A` of `N` integers, return the **maximum number of products** Alex can buy without exceeding his budget. ## Examples ### Example 1 **Input:** ```text B = 40 A = [9, 16, 4] ``` **Output:** ```text 3 ``` **Explanation:** Alex can buy all three products: ```text 9 + 16 + 4 = 29 ≤ 40 ``` Therefore, the maximum number of products is **3**. --- ### Example 2 **Input:** ```text B = 8 A = [3, 4, 2, 3, 1] ``` **Output:** ```text 3 ``` **Explanation:** Alex can buy the products with prices: ```text 3 + 3 + 1 = 7 ≤ 8 ``` Therefore, the maximum number of products is **3**. --- ### Example 3 **Input:** ```text B = 500 A = [1000, 2000] ``` **Output:** ```text 0 ``` **Explanation:** The cheapest product costs `1000`, which is already greater than the available budget of `500`. Therefore, Alex cannot buy any product. ## Constraints * `N` is an integer within the range `[2..100,000]`. * `B` is an integer within the range `[1..2,000,000,000]`. * Each element of array `A` is an integer within the range `[1..1,000,000,000]`."
Join thousands of developers practicing for Deutsche-bank.