Make Array Empty

You are given an integer array nums containing distinct numbers, and you can perform the following operations until the array is empty:

  • If the first element has the smallest value, remove it.
  • Otherwise, put the first element at the end of the array.

Return an integer denoting the number of operations it takes to make nums empty.

Example 1
Inputnums = [3,4,-1]
Output5
Following the required operations transforms the array through rotations and removals until it becomes empty in 5 operations.
Example 2
Inputnums = [1,2,4,3]
Output5
The array becomes empty after 5 operations following the rule of removing the current smallest first element or rotating otherwise.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • All values in nums are distinct.

Asked at 2 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