Sort Each Adjacent Pair of an Array, Leaving an Odd Last Element in Place
Company: Capital One
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Given an array of integers, sort it by pairs. Split the array into consecutive pairs starting from the first element, `(nums[0], nums[1])`, `(nums[2], nums[3])`, and so on, and put the two numbers of every pair in ascending order. Pairs never move relative to one another, and no number moves from one pair into another.
If the array has an odd length, the last number has no partner and stays where it is.
### Function Signature
```python
def sort_pairs(nums: list[int]) -> list[int]:
```
### Rules
- For every even index `i` with `i + 1 < len(nums)`, the output holds the smaller of `nums[i]` and `nums[i + 1]` at index `i` and the larger at index `i + 1`.
- If `len(nums)` is odd, the last element of the output equals the last element of `nums`.
- A pair of equal values is left as it is.
- Return a list of the same length as `nums`. You may reorder `nums` in place and return it, or return a new list.
### Constraints
- `1 <= len(nums) <= 10^5`
- `-10^9 <= nums[i] <= 10^9`
### Examples
**Example 1**
```text
Input: nums = [5, 4, 2, 3, 7]
Output: [4, 5, 2, 3, 7]
```
The pair `(5, 4)` becomes `(4, 5)`, the pair `(2, 3)` is already in order, and `7` has no partner, so it does not move.
**Example 2**
```text
Input: nums = [9, 1, 8, 2]
Output: [1, 9, 2, 8]
```
Each pair is sorted on its own. The result as a whole is not sorted, and it should not be.
**Example 3**
```text
Input: nums = [6]
Output: [6]
```
Overview: Given an integer array, put each consecutive pair of elements in ascending order while keeping every pair in its original place, and leave the final element untouched when the length is odd. Tests precise reading of a short specification and careful index handling.
Given an array of integers `nums`, sort it by pairs. Split the array into consecutive pairs starting from the first element, `(nums[0], nums[1])`, `(nums[2], nums[3])`, and so on, and put the two numbers of every pair in ascending order. Pairs never move relative to one another, and no number moves from one pair into another.
If the array has an odd length, the last number has no partner and stays where it is.
Implement `sort_pairs(nums)` and return the resulting list:
- For every even index `i` with `i + 1 < len(nums)`, the output holds the smaller of `nums[i]` and `nums[i + 1]` at index `i` and the larger at index `i + 1`.
- If `len(nums)` is odd, the last element of the output equals the last element of `nums`.
- A pair of equal values is left as it is.
- Return a list of the same length as `nums`. You may reorder `nums` in place and return it, or return a new list.
The result as a whole is generally not sorted, and it should not be.
### Constraints
- `1 <= len(nums) <= 10^5`
- `-10^9 <= nums[i] <= 10^9`
- Every value fits in a signed 32-bit integer; no value can exceed 2^31 - 1 in magnitude.
### Examples
**Example 1**
```text
Input: nums = [5, 4, 2, 3, 7]
Output: [4, 5, 2, 3, 7]
```
The pair `(5, 4)` becomes `(4, 5)`, the pair `(2, 3)` is already in order, and `7` has no partner, so it does not move.
**Example 2**
```text
Input: nums = [9, 1, 8, 2]
Output: [1, 9, 2, 8]
```
Each pair is sorted on its own. The result as a whole is not sorted, and it should not be.
Constraints
- 1 <= len(nums) <= 10^5
- -10^9 <= nums[i] <= 10^9
- Every value fits in a signed 32-bit integer; no value can exceed 2^31 - 1 in magnitude.
Examples
Input: ([5, 4, 2, 3, 7],)
Expected Output: [4, 5, 2, 3, 7]
Explanation: Source example 1: (5, 4) is swapped, (2, 3) is already ascending, and the unpaired 7 stays last.
Input: ([9, 1, 8, 2],)
Expected Output: [1, 9, 2, 8]
Explanation: Source example 2: each pair is sorted on its own; a global sort or a bubble pass across pair boundaries gives a different list.
Hints
- A number's pair is fixed by its index: indices 0 and 1 form the first pair, 2 and 3 the second, and so on. Numbers from different pairs are never compared with each other.
- Think about what should happen to the last element when the length is odd, and to a pair whose two values are equal.
- The whole result is not expected to be sorted; check your idea against [9, 1, 8, 2], which must become [1, 9, 2, 8].