FastPrepSort Adjacent Pairs

Sort Adjacent Pairs

Capital One logoCapital One● EasyNEW GRADFULLTIMEOA
Learn

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

More Capital One problems

See Capital One hiring insights
public int[] sortAdjacentPairs(int[] values) {
  // write your code here
}
values[5,4,2,3,7]
expected[4,5,2,3,7]
Checking account…