FastPrepDeterministic Zigzag Sort

Deterministic Zigzag Sort

SambaNova Systems logoSambaNova Systems● EasyNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Return a deterministic rearrangement of nums that satisfies a[0] <= a[1] >= a[2] <= a[3] ....

For a unique answer, sort ascending and swap each adjacent pair at indices (1,2), (3,4), and so on. Preserve every input occurrence.

Function

wiggleSort(nums: int[]) → int[]

Examples

Example 1

nums = [1,2,3,4,5]return = [1,3,2,5,4]

Swap the two alternating adjacent pairs.

Example 2

nums = [3,1,2]return = [1,3,2]

Sorting then one swap forms a peak at index one.

Example 3

nums = [2,2,1,1]return = [1,2,1,2]

Duplicates still satisfy non-strict inequalities.

Constraints

  • 1 <= nums.length <= 100000.
  • -10^9 <= nums[i] <= 10^9.

More SambaNova Systems problems

See SambaNova Systems hiring insights
public int[] wiggleSort(int[] nums) {
    // write your code here
}
nums[1,2,3,4,5]
expected[1,3,2,5,4]
Checking account…