Leaderboard
Showing:
Rearrange array such that even positioned are greater than odd
Easy Accuracy: 38.03% Submissions: 856 Points: 2

Given an array A of n elements, rearrange the array according to the following relations :
 A[i] >= A[i-1] , if i is even.
 A[i] <= A[i-1] , if i is odd.[Considering 1-indexed array]
Return the resultant array.

Example:

Input 1:
A[] = {1, 2, 2, 1}
Output:
1 2 1 2
Explanation:
Both 2 are at even positions and 1 at odd satisfying 
given condition 

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

 

Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function assign() that takes an array (arr), sizeOfArray (n), and return the resultant array. The driver code takes care of the printing.

Expected Time Complexity: O(NLOG(N)).
Expected Auxiliary Space: O(1).


Constraints:

1 ≤ N ≤ 105
1 ≤ arr[i] ≤ 106

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Rearrange array such that even positioned are greater than odd

Output Window