An inventory array product contains only 0 and 1, where 0 represents variant A and 1 represents variant B.
In one operation, choose a subarray of length k. The cost of that operation is the sum of the values inside the chosen subarray. Then choose one index inside that subarray whose value is 1 and change it to 0.
Return the minimum total cost needed to convert every product to variant A.
Complete the function minCostToConvertAllToVariantA in the editor below.
minCostToConvertAllToVariantA has the following parameters:
int[] product: the product variantsint k: the fixed operation window length
Returns
int: the minimum total cost.
Examples
01 · Example 1
product = [1, 1, 1] k = 2 return = 4
Use window [0, 1] twice, then window [1, 2] once. The costs are 2 + 1 + 1 = 4.
02 · Example 2
product = [1, 0, 1] k = 2 return = 2
Choose the left window once to clear the first 1, and the right window once to clear the last 1.
Constraints
product[i]is either0or11 <= k <= product.length- Each operation must change exactly one
1inside the chosen window to0.
More Amazon problems
- Closest Version DateONSITE INTERVIEW · Seen Jul 2026
- Maximum Concurrent Processes (Bar Raiser Round)ONSITE INTERVIEW · Seen Jul 2026
- Maximum Product New RatingOA · Seen Jul 2026
- Permutation SorterOA · Seen Jul 2026
- Get Distinct Pairs (Also apply to AS intern)Seen Jul 2026
- Maximum Final ValueSeen Jul 2026
- Minimum Delivery Center InconvenienceOA · Seen Jun 2026
- Unfulfilled Customers by Inventory PriorityOA · Seen Jun 2026
public int minCostToConvertAllToVariantA(int[] product, int k) {
// write your code here
}
product[1, 1, 1]
k2
expected4
checking account