Minimum K to Reduce Array Within Limit

You are given a positive integer array nums.

For a positive integer k, define nonPositive(nums, k) as the minimum number of operations needed to make every element of nums non-positive. In one operation, you can choose an index i and reduce nums[i] by k.

Return an integer denoting the minimum value of k such that nonPositive(nums, k) <= k^2.

Example 1
Inputnums = [3,7,5]
Output3
When k = 3, reducing 3 once, 7 three times, and 5 twice makes every element non-positive using 6 operations, and 6 <= 3^2.
Example 2
Inputnums = [1]
Output1
When k = 1, reducing the only element once makes it 0, so nonPositive(nums, k) = 1 <= 1^2.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate