Sort Adjacent Pairs
Problem statement
Given an integer array values, independently sort each adjacent pair in nondecreasing order: indices 0 and 1 form the first pair, indices 2 and 3 form the second pair, and so on.
If the array has odd length, leave its final unpaired value unchanged. Return the resulting array without modifying the input array.
Function
sortAdjacentPairs(values: int[]) → int[]Examples
Example 1
values = [5,4,2,3,7]return = [4,5,2,3,7]The pair [5,4] becomes [4,5], [2,3] is already sorted, and the final 7 is unpaired.
Example 2
values = [3,1,4,2]return = [1,3,2,4]Sort the two independent pairs [3,1] and [4,2].
Constraints
1 <= values.length <= 10^5-10^9 <= values[i] <= 10^9