1968. Array With Elements Not Equal to Average of Neighbors

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1968

class Solution:
    def rearrangeArray(self, nums: List[int]) -> List[int]:
        nums.sort()
        n = len(nums)
        m = (n + 1) >> 1
        res = []
        for i in range(m):
            res.append(nums[i])
            if i + m < n:
                res.append(nums[i + m])
        return res