QUESTION

Text
Image


4. Purchase Optimization Alex is shopping at Ozone Galleria Mall. There is a dedicated cubicle for a type of product at the shopping center. All the products sold at the $i^{\text {th }}$ cubicle are priced the same, denoted by prices[i]. The cubicles are arranged such that the price of the products sold at each cubicle are in non-decreasing order. Several queries would be asked in the problem. In each query, the cubicle number Alex is initially standing at, and the amount of money Alex has is given, and Alex can travel in the right direction visiting from the current cubicle to the last cubicle. Alex may buy at most one item from any cubicle visited, but the total cost of the purchase must not exceed the amount Alex has. Report the maximum number of products that can be purchased for each query. More formally, given an array of $n$ integers, prices, where prices[i] denotes the price of the product sold in the th cubicle. The array prices are in non-decreasing order (i.e., price $[i] \leq$ price $[i+1]$, and $q$ queries need to be processed. For each query, two integers are given: - pos:Alex's initial position - amount: the amount of money Alex has Alex needs to visit each cubicle from number pos to $n$, purchasing at most one product


Wore to mal yigen an array of $n$ integers, prces, where priceshll denotes the price of the (l.e., pricell $\mathrm{s}$ price $[i+1])$, and $q$ queries need to be processed. For each query, two integers are given: - pos: Alex's initial position - amount: the amount of money Alex has Alex needs to visit each cubicle from number pos to $n$, purchasing at most one product from each cubicle. For each query, the goal is to find the maximum number of products that Alex can buy. Example For example, let: \[ \begin{array}{l} n=5 \\ \text { prices }=[3,4,5,5,7] \\ q=3 \end{array} \] The cubicle at the convention would be ordered like this: The arrows between the cubicles denote the next cubicle that Alex can visit, the cubicle to the right. Let the following queries be: Querv 1: pos = 2, amount $=10$


Query 1: pos $=2$, amount $=10$ Alex needs to visit the cubicles from cubicle no. 2 to cubicle no. 5. Alex can purchase at most two products from these cubicles either by making one of these possible purchases: - Cubicle No. 2 and cubicle No. 3. The total cost is $4+5=9$ ( $\leq 10$ ). - Cubicle No. 2 and cubicle No. 4. The total cost is $4+5=9$ ( $\leq 10$ ). - Cubicle No. 3 and cubicle No. 4 . The total cost is $5+5=10$ ( $\leq 10$ ). Hence, the answer is 2. Query 2: pos = 1, amount $=24$ Alex needs to visit the cubicles from cubicle no. 1 to cubicle no. 5. With the given amount of money, Alex can make purchases at every cubicle visited since the total cost is $3+4+5+5+7=24(\leq 24)$. Hence, the answer is 5. Query 3: pos = 5, amount $=5$ Alex can only visit cubicle no. 5 , but cannot make any purchase there since the total amount Alex has is less than the product price. Hence, the answer is 0 . Function Description Complete the function findMaximumValue in the editor below.

Public Answer

GD2TEO The First Answerer