Java Program to Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i

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 towards 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:Â
Â
Java
// Java program to rearrange the array// as per the given conditionimport 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 */ |
Output:Â
Â
4 5 3 6 2 7 1
Time Complexity: O(N*logN), as we are using a sort function.
Auxiliary Space: O(N), as we are using  extra space.
Â
Please refer complete article on Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i for more details!
Â



