Solution: Rearrange Sorted List in Max/Min Form

Let’s solve the Rearrange Sorted List in Max/Min Form problem.

Statement

We're given a sorted list, nums, containing positive integers only. We have to rearrange the list so that when returned, the 0th0^{th} index of the list will have the largest number, the 1st1^{st} index will have the smallest number, the 2nd2^{nd} index will have the second largest number, the 3rd3^{rd} index will have the second smallest number, and so on.

In the end, we’ll have the largest remaining numbers in descending order and the smallest in ascending order at even and odd positions, respectively.

Constraints:

  • 00 \leq nums.length 103\leq 10^3

  • 11 \leq nums[i] 105\leq 10^5

Solution 1: Create a new list

In this solution, we start by initializing an empty list, result, where we populate elements from the given input list, nums, in a specific order before returning it. The solution involves iterating from the start of nums up to its midpoint, where we calculate this midpoint as such:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.