AMAZON Coding Question – Solved

12 Live
As an operations engineer at Amazon, you are responsible for organizing the distribution of n different items in the warehouse. The size of each product is provided in an array productSize, where productSize[i] represents the size of the i-th product. You construct a new array called variation, where each element variation[i] is the difference between the largest and smallest product sizes among the first i products. Mathematically, this is defined as: variation[i] = max(productSize[1], productSize[2], ..., productSize[i]) - min(productSize[1], productSize[2], ..., productSize[i]) Your goal is to arrange the products in a way that minimizes the total variation, i.e., the sum of variation[1] + variation[2] + ... + variation[n]. Determine the minimum possible value of this sum after you have reordered the products. Example: n = 3 productSize = [3, 1, 2] By reordering the products as productSize = [2, 3, 1]: - variation[0] = max(2) - min(2) = 2 - 2 = 0 - variation[1] = max(2, 3) - min(2, 3) = 3 - 2 = 1 - variation[2] = max(2, 3, 1) - min(2, 3, 1) = 3 - 1 = 2 The sum is variation[0] + variation[1] + variation[2] = 0 + 1 + 2 = 3. This is the minimum possible total variation after rearranging. Function Description: Complete the function minimizeVariation in the editor below. minimizeVariation has the following parameter(s): int productSize[n]: The size of each product. Returns: int: the minimum possible total variation after rearranging the array productSize. Constraints: 1 ≀ n ≀ 2000 1 ≀ productSize[i] ≀ 10^6 Input Format for Custom Testing: The first line contains an integer, n, the number of elements in productSize. Each of the next n lines contains an integer describing productSize[i]. Sample Case 0: Sample Input: 7 productSize = [4, 5, 4, 6, 2, 1, 1] Sample Output: 4

Asked in: AMAZON

Image of the Question

Question Image Question Image Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| Amazon Books is a retail store that sells the newly launched novel "The Story o… |
| In an Amazon analytics team, the Analysts collectively have a preference for th… |
| AWS provides scalable systems. A set of n servers are used for horizontally sca… |
| A foundry in Hackerland makes an alloy out of n different metals. In the manufa… |
| An array of integers is almost sorted if at most one element can be deleted fro… |
| The binary cardinality of a number is the count of 1's in its binary representa… |