Mid/SeniorArraySorting

Reduction Operations to Make the Array Elements Equal

Given an integer array nums, your goal is to make all elements in nums equal. To complete one operation, follow these steps:

  • Find the largest value in nums. Let its index be i (0-indexed) and its value be largest. If there are multiple elements with the largest value, pick the smallest i.
  • Find the next largest value in nums strictly smaller than largest. Let its value be nextLargest.
  • Reduce nums[i] to nextLargest.

Return the number of operations to make all elements in nums equal.

Example 1
Inputnums = [5,1,3]
Output3
It takes 3 operations to reduce 5 to 3, then both 3s to 1, making all elements equal.
Example 2
Inputnums = [1,1,1]
Output0
All elements in nums are already equal.

Constraints

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

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