Tap here to switch tabs
Problem
Submissions

Problem: Sort Array by Moving Items to Empty Space

med
30 min
Try to solve the Sort Array by Moving Items to Empty Space problem.

Statement

You are given an integer array nums of size n containing distinct integers from 00 to n−1n - 1. The number 00 represents an empty space.

In a single operation, you can choose any element and move it to the empty space, swapping that element with 00.

The array nums is considered sorted if the numbers from 11 to n−1n - 1 are in ascending order, with 00 placed either at index 00 or at index n−1n - 1.

Return the minimum number of operations needed to sort nums.

Constraints:

  • n == nums.length

  • 2≤2 \leq n ≤105\leq 10^5

  • 0≤0 \leq nums[i] << n

  • All the values of nums are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Sort Array by Moving Items to Empty Space

med
30 min
Try to solve the Sort Array by Moving Items to Empty Space problem.

Statement

You are given an integer array nums of size n containing distinct integers from 00 to n−1n - 1. The number 00 represents an empty space.

In a single operation, you can choose any element and move it to the empty space, swapping that element with 00.

The array nums is considered sorted if the numbers from 11 to n−1n - 1 are in ascending order, with 00 placed either at index 00 or at index n−1n - 1.

Return the minimum number of operations needed to sort nums.

Constraints:

  • n == nums.length

  • 2≤2 \leq n ≤105\leq 10^5

  • 0≤0 \leq nums[i] << n

  • All the values of nums are unique.