VOOZH about

URL: https://www.geeksforgeeks.org/dsa/rearrange-array-even-index-elements-smaller-odd-index-elements-greater/

⇱ Rearrange array such that even index elements are smaller and odd index elements are greater - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Rearrange array such that even index elements are smaller and odd index elements are greater

Last Updated : 31 Mar, 2023

Given an array, rearrange the array such that : 

  1. If index i is even, arr[i] <= arr[i+1]
  2. If index i is odd, arr[i] >= arr[i+1]

Note: There can be multiple answers.

Examples:  

Input : arr[] = {2, 3, 4, 5} 
Output : arr[] = {2, 4, 3, 5}
Explanation : Elements at even indexes are
smaller and elements at odd indexes are greater
than their next elements

Note : Another valid answer
is arr[] = {3, 4, 2, 5}

Input :arr[] = {6, 4, 2, 1, 8, 3}
Output :arr[] = {4, 6, 1, 8, 2, 3}

This problem is similar to sorting an array in the waveform.

  • A simple solution is to sort the array in decreasing order, then starting from the second element, swap the adjacent elements.
  • An efficient solution is to iterate over the array and swap the elements as per the given condition. 

If we have an array of length n, then we iterate from index 0 to n-2 and check the given condition. 
At any point of time if i is even and arr[i] > arr[i+1], then we swap arr[i] and arr[i+1]. Similarly, if i is odd and 
arr[i] < arr[i+1], then we swap arr[i] and arr[i+1].
For the given example: 

Before rearrange, arr[] = {2, 3, 4, 5} 
Start iterating over the array till index 2 (as n = 4) 

First Step: 
At i = 0, arr[i] = 2 and arr[i+1] = 3. As i is even and arr[i] < arr[i+1], don't need to swap. 
Second step: 
At i = 1, arr[i] = 3 and arr[i+1] = 4. As i is odd and arr[i] < arr[i+1], swap them. 
Now arr[] = {2, 4, 3, 5} 
Third step: 
At i = 2, arr[i] = 3 and arr[i+1] = 5. So, don't need to swap them

After rearrange, arr[] = {2, 4, 3, 5} 

Flowchart

👁 Image
Flowchart

Implementation:


Output
Before rearranging: 
6 4 2 1 8 3 
After rearranging: 
4 6 1 8 2 3 

Time Complexity: O(N), as we are using a loop to traverse N times, 
Auxiliary Space: O(1), as we are not using any extra space.

Approach: Sort and Swap Adjacent Elements

The "Sort and Swap Adjacent Elements" approach for rearranging an array such that even index elements are smaller and odd index elements are greater can be summarized in the following steps:

  1. Sort the input array in non-decreasing order.
  2. Iterate over the array using a step of 2 to access the odd-indexed elements.
  3. For each odd-indexed element, swap it with the next even-indexed element to meet the required condition.
  4. Return the rearranged array.
     

Here are the detailed steps with an example:

Input: arr = [2, 3, 4, 5]

  1. Sort the input array in non-decreasing order. The sorted array is: [2, 3, 4, 5].
  2. Iterate over the array using a step of 2 to access the odd-indexed elements. The odd-indexed elements are arr[1] and arr[3].
  3. For each odd-indexed element, swap it with the next even-indexed element to meet the required condition. We swap arr[1] with arr[2] to get the array [2, 4, 3, 5].
  4. Return the rearranged array [2, 4, 3, 5].

Output
[2, 4, 3, 5]

The time complexity of the "Sort and Swap Adjacent Elements" approach for rearranging an array such that even index elements are smaller and odd index elements are greater is O(n log n),

The auxiliary space of this approach is O(1).

Comment
Article Tags: