Given an integer array, rearrange it such that every second element becomes greater than its left and right elements. Assume no duplicate elements are present in the array.

For example,

Input:  {1, 2, 3, 4, 5, 6, 7}
Output: {1, 3, 2, 5, 4, 7, 6}
 
Input:  {9, 6, 8, 3, 7}
Output: {6, 9, 3, 8, 7}
 
Input:  {6, 9, 2, 5, 1, 4}
Output: {6, 9, 2, 5, 1, 4}

Practice this problem

A simple solution would be to sort the array in ascending order first. Then take another auxiliary array and fill it with elements starting from the sorted array’s two endpoints in alternate order. Following is the complete algorithm:

RearrangeArray(arr[], n)

  1. Sort the array in ascending order.
  2. Take two index variables i and j to that point to two endpoints of the array (i.e., i = 0 and j = n-1).
  3. Create an auxiliary array A[] and initialize an index k with 0.
  4. Do while (i < j)
        A[k++] = arr[i++]
        A[k++] = arr[j–]
  5. Print A[].

The time complexity of this solution is O(n.log(n)) and doesn’t require any extra space, where n is the size of the input.

 
An efficient solution doesn’t involve sorting the array or the use of extra space. The idea is to start from the second array element and increment the index by 2 for each loop’s iteration. If the last element is greater than the current element, swap the elements. Similarly, if the next element is greater than the current element, swap both elements. At the end of the loop, we will get the desired array that satisfies given constraints.

Following is the implementation in C, Java, and Python based on the above idea:

C


Download  Run Code

Output:

6 9 3 8 7

Java


Download  Run Code

Output:

[6, 9, 3, 8, 7]

Python


Download  Run Code

Output:

[6, 9, 3, 8, 7]

The time complexity of the above solution is O(n) and doesn’t require any extra space.