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

Given an array, rearrange the array such that :

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

**Note :** There can be multiple answers.**Examples:**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

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 elementsNote :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 sort an array in wave form.

A **simple solution** is to sort the array in decreasing order, then starting from 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}

## C++

`// CPP code to rearrange an array such that` `// even index elements are smaller and odd` `// index elements are greater than their` `// next.` `#include <iostream>` `using` `namespace` `std;` `void` `rearrange(` `int` `* arr, ` `int` `n)` `{` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) {` ` ` `if` `(i % 2 == 0 && arr[i] > arr[i + 1])` ` ` `swap(arr[i], arr[i + 1]);` ` ` `if` `(i % 2 != 0 && arr[i] < arr[i + 1])` ` ` `swap(arr[i], arr[i + 1]);` ` ` `}` `}` `/* Utility that prints out an array in` ` ` `a line */` `void` `printArray(` `int` `arr[], ` `int` `size)` `{` ` ` `for` `(` `int` `i = 0; i < size; i++)` ` ` `cout << arr[i] << ` `" "` `;` ` ` `cout << endl;` `}` `/* Driver function to test above functions */` `int` `main()` `{` ` ` `int` `arr[] = { 6, 4, 2, 1, 8, 3 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << ` `"Before rearranging: \n"` `;` ` ` `printArray(arr, n);` ` ` `rearrange(arr, n);` ` ` `cout << ` `"After rearranging: \n"` `;` ` ` `printArray(arr, n);` ` ` `return` `0;` `}` |

## Java

`// Java code to rearrange an array such` `// that even index elements are smaller` `// and odd index elements are greater` `// than their next.` `class` `GFG {` ` ` `static` `void` `rearrange(` `int` `arr[], ` `int` `n)` ` ` `{` ` ` `int` `temp;` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `1` `; i++) {` ` ` `if` `(i % ` `2` `== ` `0` `&& arr[i] > arr[i + ` `1` `]) {` ` ` `temp = arr[i];` ` ` `arr[i] = arr[i + ` `1` `];` ` ` `arr[i + ` `1` `] = temp;` ` ` `}` ` ` `if` `(i % ` `2` `!= ` `0` `&& arr[i] < arr[i + ` `1` `]) {` ` ` `temp = arr[i];` ` ` `arr[i] = arr[i + ` `1` `];` ` ` `arr[i + ` `1` `] = temp;` ` ` `}` ` ` `}` ` ` `}` ` ` `/* Utility that prints out an array in` ` ` `a line */` ` ` `static` `void` `printArray(` `int` `arr[], ` `int` `size)` ` ` `{` ` ` `for` `(` `int` `i = ` `0` `; i < size; i++)` ` ` `System.out.print(arr[i] + ` `" "` `);` ` ` `System.out.println();` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `arr[] = { ` `6` `, ` `4` `, ` `2` `, ` `1` `, ` `8` `, ` `3` `};` ` ` `int` `n = arr.length;` ` ` `System.out.print(` `"Before rearranging: \n"` `);` ` ` `printArray(arr, n);` ` ` `rearrange(arr, n);` ` ` `System.out.print(` `"After rearranging: \n"` `);` ` ` `printArray(arr, n);` ` ` `}` `}` `// This code is contributed by Anant Agarwal.` |

## Python3

`# Python code to rearrange` `# an array such that` `# even index elements` `# are smaller and odd ` `# index elements are` `# greater than their ` `# next.` `def` `rearrange(arr, n):` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `):` ` ` `if` `(i ` `%` `2` `=` `=` `0` `and` `arr[i] > arr[i ` `+` `1` `]):` ` ` ` ` `temp ` `=` `arr[i]` ` ` `arr[i]` `=` `arr[i ` `+` `1` `]` ` ` `arr[i ` `+` `1` `]` `=` `temp` ` ` ` ` `if` `(i ` `%` `2` `!` `=` `0` `and` `arr[i] < arr[i ` `+` `1` `]):` ` ` ` ` `temp ` `=` `arr[i]` ` ` `arr[i]` `=` `arr[i ` `+` `1` `]` ` ` `arr[i ` `+` `1` `]` `=` `temp` ` ` ` ` `# Utility that prints out an array in` `# a line ` `def` `printArray(arr, size):` ` ` `for` `i ` `in` `range` `(size):` ` ` `print` `(arr[i], ` `" "` `, end ` `=` `"")` ` ` ` ` `print` `()` `# Driver code` `arr ` `=` `[ ` `6` `, ` `4` `, ` `2` `, ` `1` `, ` `8` `, ` `3` `]` `n ` `=` `len` `(arr)` ` ` `print` `(` `"Before rearranging: "` `)` `printArray(arr, n)` ` ` `rearrange(arr, n)` ` ` `print` `(` `"After rearranging:"` `)` `printArray(arr, n);` `# This code is contributed` `# by Anant Agarwal.` |

## C#

`// C# code to rearrange an array such` `// that even index elements are smaller` `// and odd index elements are greater` `// than their next.` `using` `System;` `class` `GFG {` ` ` `static` `void` `rearrange(` `int` `[] arr, ` `int` `n)` ` ` `{` ` ` `int` `temp;` ` ` `for` `(` `int` `i = 0; i < n - 1; i++) {` ` ` `if` `(i % 2 == 0 && arr[i] > arr[i + 1])` ` ` `{` ` ` `temp = arr[i];` ` ` `arr[i] = arr[i + 1];` ` ` `arr[i + 1] = temp;` ` ` `}` ` ` ` ` `if` `(i % 2 != 0 && arr[i] < arr[i + 1])` ` ` `{` ` ` `temp = arr[i];` ` ` `arr[i] = arr[i + 1];` ` ` `arr[i + 1] = temp;` ` ` `}` ` ` `}` ` ` `}` ` ` `/* Utility that prints out an array in` ` ` `a line */` ` ` `static` `void` `printArray(` `int` `[] arr, ` `int` `size)` ` ` `{` ` ` `for` `(` `int` `i = 0; i < size; i++)` ` ` `Console.Write(arr[i] + ` `" "` `);` ` ` `Console.WriteLine();` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `[] arr = { 6, 4, 2, 1, 8, 3 };` ` ` `int` `n = arr.Length;` ` ` `Console.WriteLine(` `"Before rearranging: "` `);` ` ` `printArray(arr, n);` ` ` `rearrange(arr, n);` ` ` `Console.WriteLine(` `"After rearranging: "` `);` ` ` `printArray(arr, n);` ` ` `}` `}` `// This code is contributed by vt_m.` |

## PHP

`<?php ` `// PHP code to rearrange an array such ` `// that even index elements are smaller ` `// and odd index elements are greater ` `// than their next.` `function` `swap(&` `$a` `, &` `$b` `)` `{` ` ` `$temp` `= ` `$a` `;` ` ` `$a` `= ` `$b` `;` ` ` `$b` `= ` `$temp` `;` `}` `function` `rearrange(&` `$arr` `, ` `$n` `)` `{` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `- 1; ` `$i` `++) ` ` ` `{` ` ` `if` `(` `$i` `% 2 == 0 && ` ` ` `$arr` `[` `$i` `] > ` `$arr` `[` `$i` `+ 1])` ` ` `swap(` `$arr` `[` `$i` `], ` `$arr` `[` `$i` `+ 1]);` ` ` `if` `(` `$i` `% 2 != 0 && ` ` ` `$arr` `[` `$i` `] < ` `$arr` `[` `$i` `+ 1])` ` ` `swap(` `$arr` `[` `$i` `], ` `$arr` `[` `$i` `+ 1]);` ` ` `}` `}` `/* Utility that prints out ` `an array in a line */` `function` `printArray(&` `$arr` `, ` `$size` `)` `{` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$size` `; ` `$i` `++)` ` ` `echo` `$arr` `[` `$i` `] . ` `" "` `;` ` ` `echo` `"\n"` `;` `}` `// Driver Code` `$arr` `= ` `array` `(6, 4, 2, 1, 8, 3 );` `$n` `= sizeof(` `$arr` `);` `echo` `"Before rearranging: \n"` `;` `printArray(` `$arr` `, ` `$n` `);` `rearrange(` `$arr` `, ` `$n` `);` `echo` `"After rearranging: \n"` `;` `printArray(` `$arr` `, ` `$n` `);` `// This code is contributed ` `// by ChitraNayal` `?>` |

## Javascript

`<script>` `// JavaScript code to rearrange an array such that` `// even index elements are smaller and odd` `// index elements are greater than their` `// next.` `let rearrange = (arr, n)=>{` ` ` `for` `(let i = 0; i < n - 1; i++) {` ` ` `if` `(i % 2 == 0 && arr[i] > arr[i + 1]){` ` ` `// swap(arr[i], arr[i + 1]);` ` ` `let temp = arr[i];` ` ` `arr[i] = arr[i+1];` ` ` `arr[i+1] = temp;` ` ` `}` ` ` `if` `(i % 2 != 0 && arr[i] < arr[i + 1]){` ` ` `// swap(arr[i], arr[i + 1]);` ` ` `let temp = arr[i];` ` ` `arr[i] = arr[i+1];` ` ` `arr[i+1] = temp;` ` ` `}` ` ` `}` `}` `// function to print array` `let printArray = (arr, size)=>{` ` ` `ans = ` `''` `;` ` ` `for` `(let i = 0; i < size; i++){` ` ` `ans += arr[i] + ` `" "` `;` ` ` `}` ` ` `document.write(ans);` `}` `// Driver Code` `let arr = [ 6, 4, 2, 1, 8, 3 ];` `let n = arr.length;` `document.write(` `"Before rearranging: "` `);` `printArray(arr, n);` `rearrange(arr, n);` `document.write(` `"<br>"` `);` `document.write(` `"After rearranging: "` `);` `printArray(arr, n);` `</script>` |

**Output:**

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

**Time Complexity :**O(n)

**Auxiliary Space: **O(1)**Reference :**

discuss.codechef.com/questions/43062/anuund-editorial

This article is contributed by **Sukanta Nandi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.