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.
Note: Examples are based upon 1-based indexing
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.
Note: This solution is considering 1-based indexing
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void assign( int a[], int n)
{
sort(a, a + n);
int ans[n];
int p = 0, q = n - 1;
for ( int i = 0; i < n; i++) {
if ((i + 1) % 2 == 0)
ans[i] = a[q--];
else
ans[i] = a[p++];
}
for ( int i = 0; i < n; i++)
cout << ans[i] << " " ;
}
int main()
{
int A[] = { 1, 3, 2, 2, 5 };
int n = sizeof (A) / sizeof (A[0]);
assign(A, n);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
int cmpfunc( const void * a, const void * b)
{
return (*( int *)a - *( int *)b);
}
void assign( int a[], int n)
{
qsort (a, n, sizeof ( int ), cmpfunc);
int ans[n];
int p = 0, q = n - 1;
for ( int i = 0; i < n; i++) {
if ((i + 1) % 2 == 0)
ans[i] = a[q--];
else
ans[i] = a[p++];
}
for ( int i = 0; i < n; i++)
printf ( "%d " , ans[i]);
}
int main()
{
int A[] = { 1, 3, 2, 2, 5 };
int n = sizeof (A) / sizeof (A[0]);
assign(A, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void assign( int a[], int n)
{
Arrays.sort(a);
int ans[] = new int [n];
int p = 0 , q = n - 1 ;
for ( int i = 0 ; i < n; i++) {
if ((i + 1 ) % 2 == 0 )
ans[i] = a[q--];
else
ans[i] = a[p++];
}
for ( int i = 0 ; i < n; i++)
System.out.print(ans[i] + " " );
}
public static void main(String args[])
{
int A[] = { 1 , 3 , 2 , 2 , 5 };
int n = A.length;
assign(A, n);
}
}
|
Python3
def assign(a, n):
a.sort()
ans = [ 0 ] * n
p = 0
q = n - 1
for i in range (n):
if (i + 1 ) % 2 = = 0 :
ans[i] = a[q]
q = q - 1
else :
ans[i] = a[p]
p = p + 1
for i in range (n):
print (ans[i], end = " " )
A = [ 1 , 3 , 2 , 2 , 5 ]
n = len (A)
assign(A, n)
|
C#
using System;
class GFG {
static void assign( int [] a, int n)
{
Array.Sort(a);
int [] ans = new int [n];
int p = 0, q = n - 1;
for ( int i = 0; i < n; i++) {
if ((i + 1) % 2 == 0)
ans[i] = a[q--];
else
ans[i] = a[p++];
}
for ( int i = 0; i < n; i++)
Console.Write(ans[i] + " " );
}
public static void Main()
{
int [] A = { 1, 3, 2, 2, 5 };
int n = A.Length;
assign(A, n);
}
}
|
Javascript
<script>
function assign(a, n)
{
a.sort();
let ans = [];
let p = 0, q = n - 1;
for (let i = 0; i < n; i++) {
if ((i + 1) % 2 == 0)
ans[i] = a[q--];
else
ans[i] = a[p++];
}
for (let i = 0; i < n; i++)
document.write(ans[i] + " " );
}
let A = [ 1, 3, 2, 2, 5 ];
let n = A.length;
assign(A, n);
</script>
|
PHP
<?php
function assign( $a , $n )
{
sort( $a );
$p = 0; $q = $n - 1;
for ( $i = 0; $i < $n ; $i ++)
{
if (( $i + 1) % 2 == 0)
$ans [ $i ] = $a [ $q --];
else
$ans [ $i ] = $a [ $p ++];
}
for ( $i = 0; $i < $n ; $i ++)
echo ( $ans [ $i ] . " " );
}
$A = array ( 1, 3, 2, 2, 5 );
$n = sizeof( $A );
assign( $A , $n );
?>
|
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 first element till n-1 and swap the element with the next one if the condition is not satisfied. This is implemented as follows:
Note: This solution is considering 0-based indexing
C++
#include <bits/stdc++.h>
using namespace std;
void swap( int * a, int * b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void rearrange( int arr[], int n)
{
for ( int i = 0; i < n-1; i+=2) {
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
import java.io.*;
class GFG {
public static void rearrange( int [] arr, int n)
{
for ( int i = 1 ; i < n; i++) {
if (i % 2 == 0 ) {
if (arr[i] < arr[i - 1 ]) {
int temp = arr[i];
arr[i] = arr[i - 1 ];
arr[i - 1 ] = temp;
}
}
else {
if (arr[i] > arr[i - 1 ]) {
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] + " " );
}
}
public static void main(String[] args)
{
int n = 5 ;
int arr[] = { 1 , 3 , 2 , 2 , 5 };
rearrange(arr, n);
}
}
|
Python3
def rearrange(arr, n):
for i in range ( 1 , n):
if (i % 2 = = 0 ):
if (arr[i] < arr[i - 1 ]):
arr[i - 1 ], arr[i] = arr[i], arr[i - 1 ]
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 ()
|
C#
using System;
class GFG
{
public static void rearrange( int [] arr, int n)
{
for ( int i = 1; i < n; i++)
{
if (i % 2 == 0)
{
if (arr[i] < arr[i - 1])
{
int temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
}
}
else
{
if (arr[i] > arr[i - 1])
{
int temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
}
}
}
for ( int i = 0; i < n; i++)
{
Console.Write(arr[i] + " " );
}
}
public static void Main(String []args)
{
int n = 5;
int []arr = {1, 3, 2, 2, 5};
rearrange(arr, n);
}
}
|
Javascript
<script>
function rearrange(arr, n)
{
for (let i = 1; i < n; i++)
{
if (i % 2 == 0)
{
if (arr[i] < arr[i - 1])
{
let temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
}
}
else
{
if (arr[i] > arr[i - 1])
{
let temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
}
}
}
for (let i = 0; i < n; i++)
{
document.write(arr[i] + " " );
}
}
let n = 5;
let arr = [1, 3, 2, 2, 5];
rearrange(arr, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
03 Aug, 2023
Like Article
Save Article