Tap here to switch tabs
Problem
Submissions

Problem: Minimum Adjacent Swaps to Make a Valid Array

Minimum Adjacent Swaps to Make a Valid Array asks you to rearrange an integer array so a smallest value is first and a largest value is last, returning the fewest adjacent swaps.

Statement

Given a 00-indexed integer array nums, you may perform swaps on adjacent elements any number of times.

An array is considered valid if it satisfies both of the following conditions:

  • The smallest element (or any one of the smallest elements, if duplicates exist) is located at the leftmost position of the array.

  • The largest element (or any one of the largest elements, if duplicates exist) is located at the rightmost position of the array.

Return the minimum number of adjacent swaps needed to make nums a valid array.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Adjacent Swaps to Make a Valid Array

Minimum Adjacent Swaps to Make a Valid Array asks you to rearrange an integer array so a smallest value is first and a largest value is last, returning the fewest adjacent swaps.

Statement

Given a 00-indexed integer array nums, you may perform swaps on adjacent elements any number of times.

An array is considered valid if it satisfies both of the following conditions:

  • The smallest element (or any one of the smallest elements, if duplicates exist) is located at the leftmost position of the array.

  • The largest element (or any one of the largest elements, if duplicates exist) is located at the rightmost position of the array.

Return the minimum number of adjacent swaps needed to make nums a valid array.

Constraints:

  • 1≤1 \leq nums.length ≤105\leq 10^5

  • 1≤1 \leq nums[i] ≤105\leq 10^5