Problem · Array

Find Optimal Level

Learn this problem
HardAmazonOA
See Amazon hiring insights

Problem statement

Amazon is dedicated to leveraging advanced methods to streamline stock movement across its distribution centers. In this scenario, you are assigned a challenge involving a specific process with a sequence of stock quantities.

More specifically, you are provided with an array of positive numbers, stockArr, containing n elements. Additionally, you are given two positive numbers, incVal and decVal, where incVal is less than or equal to decVal. You may execute the stock adjustment operation on stockArr any number of times, including not performing it at all.

The stock adjustment operation is defined as picking two distinct positions p and q in the array (0 ≤ p, q < n), increasing the value at stockArr[p] by incVal, and decreasing the value at stockArr[q] by decVal.

Our goal is to compute the highest achievable value of the minimum stock quantity in stockArr after completing all desired operations.

Note: During the adjustment process, some elements may become negative. However, once all operations have been completed, every number in stockArr must be strictly greater than 0.

Function

findOptimalLevel(stockArr: int[], incVal: int, decVal: int) → int

Examples

Example 1

stockArr = [11, 1, 2]incVal = 2decVal = 3return = 3
Example 1 illustration

Example 2

stockArr = [1, 5, 9]incVal = 2decVal = 2return = 5
Example 2 illustration

Constraints

  • 2 ≤ n ≤ 2*10^5
  • 1 ≤ stockArr[i] ≤ 10^9
  • 1 ≤ incValdecVal ≤ 10^9

More Amazon problems

drafts saved locally
public int findOptimalLevel(int[] stockArr, int incVal, int decVal) {
  // write your code here
}
stockArr[11, 1, 2]
incVal2
decVal3
expected3
checking account