Alternative Sorting
Basic Accuracy: 70.6% Submissions: 2922 Points: 1

Given an array arr[] of N distinct integers, output the array in such a way that the first element is first maximum and the second element is the first minimum, and so on.
 

Example 1:

Input: N = 7, arr[] = {7, 1, 2, 3, 4, 
                                5, 6}
Output: 7 1 6 2 5 3 4
Explanation:The first element is first 
maximum and second element is first 
minimum and so on.

Example 2:

Input: N = 8, arr[] = {1, 6, 9, 4, 3, 
                             7, 8, 2}
Output: 9 1 8 2 7 3 6 4​


Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function alternateSort() that takes array arr and integer N as parameters and returns the desired array as output.

 

Expected Time Complexity: O(NlogN).
Expected Auxiliary Space: O(N).

 

Constraints:
1 ≤ N ≤ 106

 

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


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.

Alternative Sorting

Output Window