Given an array of n elements. Our task is to write a program to rearrange the array such that elements at even positions are greater than all elements before it and elements at odd positions are less than all elements before it.
Examples:
Input : arr[] = {1, 2, 3, 4, 5, 6, 7}
Output : 4 5 3 6 2 7 1
Input : arr[] = {1, 2, 1, 4, 5, 6, 8, 8}
Output : 4 5 2 6 1 8 1 8
The idea to solve this problem is to first create an auxiliary copy of the original array and sort the copied array. Now total number of even position in array with n elements will be floor(n/2) and remaining is the number of odd positions. Now fill the odd and even positions in the original array using the sorted array in the below manner:
- Total odd positions will be n – floor(n/2). Start from (n-floor(n/2))th position in the sorted array and copy the element to 1st position of sorted array. Start traversing the sorted array from this position towards left and keep filling the odd positions in the original array towrds right.
- Start traversing the sorted array starting from (n-floor(n/2)+1)th position towards right and keep filling the original array starting from 2nd position.
Below is the implementation of above idea:
C++
// C++ program to rearrange the array // as per the given condition #include <bits/stdc++.h> using namespace std; // function to rearrange the array void rearrangeArr(int arr[], int n) { // total even positions int evenPos = n / 2; // total odd positions int oddPos = n - evenPos; int tempArr[n]; // copy original array in an // auxiliary array for (int i = 0; i < n; i++) tempArr[i] = arr[i]; // sort the auxiliary array sort(tempArr, tempArr + n); int j = oddPos - 1; // fill up odd position in original // array for (int i = 0; i < n; i += 2) { arr[i] = tempArr[j]; j--; } j = oddPos; // fill up even positions in original // array for (int i = 1; i < n; i += 2) { arr[i] = tempArr[j]; j++; } // display array for (int i = 0; i < n; i++) cout << arr[i] << " "; } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int size = sizeof(arr) / sizeof(arr[0]); rearrangeArr(arr, size); return 0; } |
Java
// Java program to rearrange the array // as per the given condition import java.util.*; import java.lang.*; public class GfG{ // function to rearrange the array public static void rearrangeArr(int arr[], int n) { // total even positions int evenPos = n / 2; // total odd positions int oddPos = n - evenPos; int[] tempArr = new int [n]; // copy original array in an // auxiliary array for (int i = 0; i < n; i++) tempArr[i] = arr[i]; // sort the auxiliary array Arrays.sort(tempArr); int j = oddPos - 1; // fill up odd position in // original array for (int i = 0; i < n; i += 2) { arr[i] = tempArr[j]; j--; } j = oddPos; // fill up even positions in // original array for (int i = 1; i < n; i += 2) { arr[i] = tempArr[j]; j++; } // display array for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Driver function public static void main(String argc[]){ int[] arr = new int []{ 1, 2, 3, 4, 5, 6, 7 }; int size = 7; rearrangeArr(arr, size); } } /* This code is contributed by Sagar Shukla */ |
Python3
# Python3 code to rearrange the array # as per the given condition import array as a import numpy as np # function to rearrange the array def rearrangeArr(arr, n): # total even positions evenPos = int(n / 2) # total odd positions oddPos = n - evenPos # intialising empty array in python tempArr = np.empty(n, dtype = object) # copy original array in an # auxiliary array for i in range(0, n): tempArr[i] = arr[i] # sort the auxiliary array tempArr.sort() j = oddPos - 1 # fill up odd position in original # array for i in range(0, n, 2): arr[i] = tempArr[j] j = j - 1 j = oddPos # fill up even positions in original # array for i in range(1, n, 2): arr[i] = tempArr[j] j = j + 1 # display array for i in range(0, n): print (arr[i], end = ' ') # Driver code arr = a.array('i', [ 1, 2, 3, 4, 5, 6, 7 ]) rearrangeArr(arr, 7) # This code is contributed by saloni1297 |
C#
// C# program to rearrange the array // as per the given condition using System; public class GfG { // Function to rearrange the array public static void rearrangeArr(int []arr, int n) { // total even positions int evenPos = n / 2; // total odd positions int oddPos = n - evenPos; int[] tempArr = new int [n]; // copy original array in an // auxiliary array for (int i = 0; i < n; i++) tempArr[i] = arr[i]; // sort the auxiliary array Array.Sort(tempArr); int j = oddPos - 1; // Fill up odd position in // original array for (int i = 0; i < n; i += 2) { arr[i] = tempArr[j]; j--; } j = oddPos; // Fill up even positions in // original array for (int i = 1; i < n; i += 2) { arr[i] = tempArr[j]; j++; } // display array for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Driver Code public static void Main() { int[] arr = new int []{ 1, 2, 3, 4, 5, 6, 7 }; int size = 7; rearrangeArr(arr, size); } } /* This code is contributed by vt_m */ |
PHP
<?php // PHP program to rearrange the array // as per the given condition // function to rearrange the array function rearrangeArr(&$arr, $n) { // total even positions $evenPos = intval($n / 2); // total odd positions $oddPos = $n - $evenPos; $tempArr = array_fill(0, $n, NULL); // copy original array in an // auxiliary array for ($i = 0; $i < $n; $i++) $tempArr[$i] = $arr[$i]; // sort the auxiliary array sort($tempArr); $j = $oddPos - 1; // fill up odd position in // original array for ($i = 0; $i < $n; $i += 2) { $arr[$i] = $tempArr[$j]; $j--; } $j = $oddPos; // fill up even positions in // original array for ($i = 1; $i < $n; $i += 2) { $arr[$i] = $tempArr[$j]; $j++; } // display array for ($i = 0; $i < $n; $i++) echo $arr[$i] ." "; } // Driver code $arr = array(1, 2, 3, 4, 5, 6, 7 ); $size = sizeof($arr); rearrangeArr($arr, $size); // This code is contributed // by ChitraNayal ?> |
Output:
4 5 3 6 2 7 1
Time Complexity: O( n logn )
Auxiliary Space: O(n)
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.
Recommended Posts:
- Rearrange array such that all even-indexed elements in the Array is even
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Rearrange array such that even positioned are greater than odd
- Rearrange array by interchanging positions of even and odd elements in the given array
- Even numbers at even index and odd numbers at odd index
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Rearrange an array such that every odd indexed element is greater than it previous
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Number of permutations such that sum of elements at odd index and even index are equal
- Minimize swaps to rearrange array such that parity of index and corresponding element is same
- Rearrange array to maximize count of triplets (i, j, k) such that arr[i] > arr[j] < arr[k] and i < j < k
- Rearrange an array such that 'arr[j]' becomes 'i' if 'arr[i]' is 'j' | Set 1
- Rearrange numbers in an array such that no two adjacent numbers are same
- Rearrange array such that difference of adjacent elements is in descending order
- Rearrange an array such that product of every two consecutive elements is a multiple of 4
- Rearrange an array such that arr[i] = i
- Rearrange array such that sum of same indexed elements is atmost K
- Rearrange a given list such that it consists of alternating minimum maximum elements
- Rearrange two given arrays such that sum of same indexed elements lies within given range
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : chitranayal

