Array With Elements Not Equal to Average of Neighbors LeetCode solution in Python
1class Solution:2def rearrangeArray(self, nums: List[int]) -> List[int]:3for i in range(1, len(nums) - 1):4pre = nums[i - 1]5current = nums[i]6next = nums[i + 1]78# If block will run when we meet 1 2 3 or 6 4 29if (pre < current < next) or (pre > current > next):10# Swap next and current11# For example:12# 1 2 3 -> 1 3 213# 6 4 2 -> 6 2 414nums[i + 1], nums[i] = nums[i], nums[i + 1]1516return nums