Candy Store

You are given two arrays of length n:

  • candies[i] = total number of candies of type i
  • costs[i] = cost of one candy of type i

For each index i, choose an integer d[i] such that candies[i] is divisible by d[i].

Then the cost of one pack of type i becomes: packCost[i] = costs[i] * d[i]

This forms an array: [packCost[0], packCost[1], ..., packCost[n - 1]}

One price tag can cover a contiguous subarray only if all values in that subarray are equal.

Return the minimum number of price tags needed to cover the entire packCost array after choosing valid values of d[i].

Example 1:

Input: candies = [6, 8, 4], costs = [2, 3, 6]

Output: 1

Explanation:

Choose d = [6, 4, 2].

Then:

  • packCost[0] = 2 * 6 = 12
  • packCost[1] = 3 * 4 = 12
  • packCost[2] = 6 * 2 = 12

So packCost = [12, 12, 12].

All values are equal, so only 1 price tag is needed.

Example 2:

Input: candies = [5, 6, 9, 4], costs = [2, 2, 1, 3]

Output: 2

Explanation:

Choose d = [1, 1, 2, 1].

Then:

  • packCost[0] = 2
  • packCost[1] = 2
  • packCost[2] = 2
  • packCost[3] = 3

So packCost = [2, 2, 2, 3].

The first three indices can be covered by one price tag, and the last index needs one more.

Now Your Turn!

Pick the correct output for the given input

Input: candies = [4, 9, 6], costs = [3, 2, 4]

Still unsure what the problem is asking ?

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

Constraints:

  • 2 <= n <= 2 * 105
  • 1 <= candies[i] <= 109
  • 1 <= costs[i] <= 104
  • The sum of n over all test cases does not exceed 2 * 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int candyStore(vector<int>& candies, vector<int>& costs) {
// Your code goes here
}
};
 
Test Case

Input:

Candies
Costs