Solution: Rearrange Sorted Array in Max/Min Form
Let’s solve the Rearrange Sorted Array in Max/Min Form problem.
Statement
We're given a sorted array, nums
, containing positive integers only. We have to rearrange the array so that when returned, the
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:
nums.length
nums[i]
Solution 1: Create a new array
In this solution, we start by initializing an empty array, result
, where we populate elements from the given input array, 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.