Open in App
Not now

# Rearrange array such that even positioned are greater than odd

• Difficulty Level : Easy
• Last Updated : 04 Jan, 2023

Given an array A of n elements, sort the array according to the following relations :

• , if i is even,  âˆ€ 1 <= i <= n
• , if i is odd ,  âˆ€ 1 <= i <= n

Print the resultant array.

Examples :

```Input : A[] = {1, 2, 2, 1}
Output :  1 2 1 2
Explanation :
For 1st element, 1  1, i = 2 is even.
3rd element, 1  1, i = 4 is even.

Input : A[] = {1, 3, 2}
Output : 1 3 2
Explanation :
Here, the array is also sorted as per the conditions.
1  1 and 2 < 3.```

Method 1:

Observe that array consists of [n/2] even positioned elements. If we assign the largest [n/2] elements to the even positions and the rest of the elements to the odd positions, our problem is solved. Because element at the odd position will always be less than the element at the even position as it is the maximum element and vice versa. Sort the array and assign the first [n/2] elements at even positions.

Below is the implementation of the above approach:

## C++

 `// C++ program to rearrange the elements in array such that` `// even positioned are greater than odd positioned elements` `#include ` `using` `namespace` `std;`   `void` `assign(``int` `a[], ``int` `n)` `{` `    ``// Sort the array` `    ``sort(a, a + n);`   `    ``int` `ans[n];` `    ``int` `p = 0, q = n - 1;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Assign even indexes with maximum elements` `        ``if` `((i + 1) % 2 == 0)` `            ``ans[i] = a[q--];` `        ``// Assign odd indexes with remaining elements` `        ``else` `            ``ans[i] = a[p++];` `    ``}`   `    ``// Print result` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``cout << ans[i] << ``" "``;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `A[] = { 1, 3, 2, 2, 5 };` `    ``int` `n = ``sizeof``(A) / ``sizeof``(A[0]);` `    ``assign(A, n);` `    ``return` `0;` `}`

## C

 `// C program to rearrange the elements in array such that` `// even positioned are greater than odd positioned elements` `#include ` `#include `   `// Compare function for qsort` `int` `cmpfunc(``const` `void``* a, ``const` `void``* b)` `{` `    ``return` `(*(``int``*)a - *(``int``*)b);` `}`   `void` `assign(``int` `a[], ``int` `n)` `{` `    ``// Sort the array` `    ``qsort``(a, n, ``sizeof``(``int``), cmpfunc);`   `    ``int` `ans[n];` `    ``int` `p = 0, q = n - 1;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Assign even indexes with maximum elements` `        ``if` `((i + 1) % 2 == 0)` `            ``ans[i] = a[q--];` `        ``// Assign odd indexes with remaining elements` `        ``else` `            ``ans[i] = a[p++];` `    ``}`   `    ``// Print result` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``printf``(``"%d "``, ans[i]);` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `A[] = { 1, 3, 2, 2, 5 };` `    ``int` `n = ``sizeof``(A) / ``sizeof``(A[0]);` `    ``assign(A, n);` `    ``return` `0;` `}`   `// This code is contributed by Sania Kumari Gupta`

## Java

 `// Java program to rearrange the elements` `// in array such that even positioned are` `// greater than odd positioned elements` `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG {`   `    ``static` `void` `assign(``int` `a[], ``int` `n)` `    ``{`   `        ``// Sort the array` `        ``Arrays.sort(a);`   `        ``int` `ans[] = ``new` `int``[n];` `        ``int` `p = ``0``, q = n - ``1``;` `        ``for` `(``int` `i = ``0``; i < n; i++) {`   `            ``// Assign even indexes with maximum elements` `            ``if` `((i + ``1``) % ``2` `== ``0``)` `                ``ans[i] = a[q--];`   `            ``// Assign odd indexes with remaining elements` `            ``else` `                ``ans[i] = a[p++];` `        ``}`   `        ``// Print result` `        ``for` `(``int` `i = ``0``; i < n; i++)` `            ``System.out.print(ans[i] + ``" "``);` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``int` `A[] = { ``1``, ``3``, ``2``, ``2``, ``5` `};` `        ``int` `n = A.length;` `        ``assign(A, n);` `    ``}` `}`   `// This code is contributed by Nikita Tiwari.`

## Python3

 `# Python3 code to rearrange the` `# elements in array such that` `# even positioned are greater` `# than odd positioned elements`   `def` `assign(a, n):` `    `  `    ``# Sort the array` `    ``a.sort()` `    `  `    ``ans ``=` `[``0``] ``*` `n` `    ``p ``=` `0` `    ``q ``=` `n ``-` `1` `    ``for` `i ``in` `range``(n):` `        `  `        ``# Assign even indexes with` `        ``# maximum elements` `        ``if` `(i ``+` `1``) ``%` `2` `=``=` `0``:` `            ``ans[i] ``=` `a[q]` `            ``q ``=` `q ``-` `1` `        `  `        ``# Assign odd indexes with` `        ``# remaining elements` `        ``else``:` `            ``ans[i] ``=` `a[p]` `            ``p ``=` `p ``+` `1` `            `  `    ``# Print result` `    ``for` `i ``in` `range``(n):` `        ``print``(ans[i], end ``=` `" "``)`   `# Driver Code` `A ``=` `[ ``1``, ``3``, ``2``, ``2``, ``5` `]` `n ``=` `len``(A)` `assign(A, n)`   `# This code is contributed by "Sharad_Bhardwaj".`

## C#

 `// C# program to rearrange the elements` `// in array such that even positioned are` `// greater than odd positioned elements` `using` `System;`   `class` `GFG {`   `    ``static` `void` `assign(``int``[] a, ``int` `n)` `    ``{` `        ``// Sort the array` `        ``Array.Sort(a);`   `        ``int``[] ans = ``new` `int``[n];` `        ``int` `p = 0, q = n - 1;` `        ``for` `(``int` `i = 0; i < n; i++) {`   `            ``// Assign even indexes with maximum elements` `            ``if` `((i + 1) % 2 == 0)` `                ``ans[i] = a[q--];`   `            ``// Assign odd indexes with remaining elements` `            ``else` `                ``ans[i] = a[p++];` `        ``}`   `        ``// Print result` `        ``for` `(``int` `i = 0; i < n; i++)` `            ``Console.Write(ans[i] + ``" "``);` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int``[] A = { 1, 3, 2, 2, 5 };` `        ``int` `n = A.Length;` `        ``assign(A, n);` `    ``}` `}`   `// This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output

`1 5 2 3 2 `

Time Complexity: O(n * log n)
Auxiliary Space: O(n), since n extra space has been taken.

Method 2:

One other approach is to traverse the array from the second element and swap the element with the previous one if the condition is not satisfied. This is implemented as follows:

## C++

 `// C++ program to rearrange the elements` `// in the array such that even positioned are` `// greater than odd positioned elements` `#include ` `using` `namespace` `std;`   `// swap two elements` `void` `swap(``int``* a, ``int``* b)` `{` `    ``int` `temp = *a;` `    ``*a = *b;` `    ``*b = temp;` `}`   `void` `rearrange(``int` `arr[], ``int` `n)` `{` `    ``for` `(``int` `i = 1; i < n; i++) {` `        ``// if index is even` `        ``if` `(i % 2 == 0) {` `            ``if` `(arr[i] > arr[i - 1])` `                ``swap(&arr[i - 1], &arr[i]);` `        ``}` `        ``// if index is odd` `        ``else` `{` `            ``if` `(arr[i] < arr[i - 1])` `                ``swap(&arr[i - 1], &arr[i]);` `        ``}` `    ``}` `}`   `int` `main()` `{` `    ``int` `n = 5;` `    ``int` `arr[] = { 1, 3, 2, 2, 5 };` `    ``rearrange(arr, n);` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``cout << arr[i] << ``" "``;` `    ``cout << ``"\n"``;` `    ``return` `0;` `}`

## Java

 `// Java program to rearrange the elements` `// in the array such that even positioned are` `// greater than odd positioned elements` `import` `java.io.*;`   `class` `GFG {` `    ``public` `static` `void` `rearrange(``int``[] arr, ``int` `n)` `    ``{` `        ``for` `(``int` `i = ``1``; i < n; i++) {`   `            ``// if index is even` `            ``if` `(i % ``2` `== ``0``) {` `                ``if` `(arr[i] > arr[i - ``1``]) {`   `                    ``// swap two elements` `                    ``int` `temp = arr[i];` `                    ``arr[i] = arr[i - ``1``];` `                    ``arr[i - ``1``] = temp;` `                ``}` `            ``}`   `            ``// if index is odd` `            ``else` `{` `                ``if` `(arr[i] < arr[i - ``1``]) {`   `                    ``// swap two elements` `                    ``int` `temp = arr[i];` `                    ``arr[i] = arr[i - ``1``];` `                    ``arr[i - ``1``] = temp;` `                ``}` `            ``}` `        ``}`   `        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``System.out.print(arr[i] + ``" "``);` `        ``}` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `n = ``5``;` `        ``int` `arr[] = { ``1``, ``3``, ``2``, ``2``, ``5` `};` `        ``rearrange(arr, n);` `    ``}` `}`   `// This code is contributed by avanitrachhadiya2155`

## Python3

 `# Python3 program to rearrange ` `# the elements in the array ` `# such that even positioned are` `# greater than odd positioned elements` `def` `rearrange(arr, n):`   `    ``for` `i ``in` `range` `(``1``, n):` `      `  `        ``# if index is even` `        ``if` `(i ``%` `2` `=``=` `0``):` `            ``if` `(arr[i] > arr[i ``-` `1``]):` `                ``arr[i ``-` `1``], arr[i] ``=` `arr[i], arr[i ``-` `1``]` `        `  `        ``# if index is odd` `        ``else``:` `            ``if` `(arr[i] < arr[i ``-` `1``]):` `                ``arr[i ``-` `1``], arr[i] ``=` `arr[i] , arr[i ``-` `1``]`   `if` `__name__ ``=``=` `"__main__"``:          ` `    ``n ``=` `5` `    ``arr ``=` `[``1``, ``3``, ``2``, ``2``, ``5``]` `    ``rearrange(arr, n);` `    ``for` `i ``in` `range` `(n):` `        ``print` `(arr[i], end ``=` `" "``)` `    ``print` `()` `   `  `# This code is contributed by Chitranayal`

## C#

 `// C# program to rearrange the elements` `// in the array such that even positioned are` `// greater than odd positioned elements` `using` `System;` `class` `GFG` `{` `public` `static` `void` `rearrange(``int``[] arr, ``int` `n)` `{` `    ``for``(``int` `i = 1; i < n; i++)` `    ``{`   `      ``// if index is even` `      ``if``(i % 2 == 0)` `      ``{` `          ``if``(arr[i] > arr[i - 1])` `          ``{`   `          ``// swap two elements` `          ``int` `temp = arr[i];` `          ``arr[i] = arr[i - 1];` `          ``arr[i - 1] = temp;` `          ``}` `      ``}`   `      ``// if index is odd` `      ``else` `      ``{` `          ``if` `(arr[i] < arr[i - 1])` `          ``{`   `            ``// swap two elements` `            ``int` `temp = arr[i];` `            ``arr[i] = arr[i - 1];` `            ``arr[i - 1] = temp;` `          ``}` `      ``}` `    ``}`   `  ``for` `(``int` `i = 0; i < n; i++)` `  ``{` `    ``Console.Write(arr[i] + ``" "``);` `  ``}` `}`   `// Driver code` `public` `static` `void` `Main(String []args)` `{` `    ``int` `n = 5;` `    ``int` `[]arr = {1, 3, 2, 2, 5};` `    ``rearrange(arr, n);` `}` `}`   `// This code is contributed by shivanisinghss2110`

## Javascript

 ``

Output

`1 3 2 5 2 `

Time Complexity: O(n)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles