Reorder the Original Array into Strict Alternating Peaks

Quick Overview

Reorder the Original Array into Strict Alternating Peaks. Rearrange values in place so each odd-position peak is strictly greater than its adjacent even-position valleys. Any valid arrangement is allowed.

Reorder the Original Array into Strict Alternating Peaks

Company: IBM

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Rearrange nums in place so `nums[0]<nums[1]>nums[2]<nums[3]` and so on. The source guarantees a valid rearrangement exists and accepts different valid arrangements. Preserve the `void wiggleSort(vector<int>&)` API and every input occurrence. ```cpp void wiggleSort(vector<int>& nums); ``` ### Examples ```text nums = [1,1,2,2,3,3] -> one valid resulting array [2,3,1,3,1,2] nums = [4] -> [4] ``` ### Constraints - Length is 1 through 50000. - Values lie in [0,5000]. - At least one strict wiggle arrangement exists. ### Follow-up Questions Can you achieve O(n) time and/or in-place O(1) extra space? ### What a Strong Answer Covers Place the largest floor(n/2) values in descending order at odd peak indices, then place the remaining values in descending order at even valley indices. The bounded value domain permits a frequency-table implementation without copying the array. Explain why every produced result satisfies the source contract and why no required result is omitted.

Overview: Reorder the Original Array into Strict Alternating Peaks. Rearrange values in place so each odd-position peak is strictly greater than its adjacent even-position valleys. Any valid arrangement is allowed.

IBM logo
IBM
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Rearrange nums in place so nums[0]<nums[1]>nums[2]<nums[3] and so on. The source guarantees a valid rearrangement exists and accepts different valid arrangements. Preserve the void wiggleSort(vector<int>&) API and every input occurrence.

void wiggleSort(vector<int>& nums);

Examples

nums = [1,1,2,2,3,3] -> one valid resulting array [2,3,1,3,1,2]
nums = [4] -> [4]

Constraints

  • Length is 1 through 50000.
  • Values lie in [0,5000].
  • At least one strict wiggle arrangement exists.

Follow-up Questions Guidance

Can you achieve O(n) time and/or in-place O(1) extra space?

What a Strong Answer Covers Guidance

Place the largest floor(n/2) values in descending order at odd peak indices, then place the remaining values in descending order at even valley indices. The bounded value domain permits a frequency-table implementation without copying the array. Explain why every produced result satisfies the source contract and why no required result is omitted.