Challenge 10: Rearrange Sorted Array in Max/Min Form
Arrange elements in such a way that the maximum element appears first, then the minimum second, then second maximum at the third position, the second minimum at fourth, and so on.
We'll cover the following
Problem statement
Implement a function maxMin(int arr[], int size)
, which takes a sorted array arr
and its size and will rearrange the elements of a sorted array such that the first position will have the largest number, the second will have the smallest, the third will have second largest, and so on. In other words, all the even-numbered indices will have the largest numbers in the array in descending order, and the odd-numbered indices will have the smallest numbers in ascending order.
The given array is sorted in ascending order.
Input
A sorted array and its size is given.
Output
The output is a list with elements stored in max/min form.
Sample input
arr = [1,2,3,4,5]
Sample output
arr = [5,1,4,2,3]
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.