Leetcode 2085 Solution

This article provides solution to leetcode question 2085 (array-with-elements-not-equal-to-average-of-neighbors)

https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors

Solution

class Solution:
    def rearrangeArray(self, nums: List[int]) -> List[int]:
        nums.sort()

        ans = []

        l = 0
        r = len(nums) - 1

        while len(ans) < len(nums):
            ans.append(nums[l])
            l += 1

            if r >= l:
                ans.append(nums[r])
                r -= 1

        return ans