1968. Array With Elements Not Equal to Average of Neighbors ¶ Time: $O(\texttt{sort})$ Space: $O(\texttt{sort})$ C++JavaPython 1 2 3 4 5 6 7 8 9class Solution { public: vector<int> rearrangeArray(vector<int>& nums) { ranges::sort(nums); for (int i = 1; i < nums.size(); i += 2) swap(nums[i], nums[i - 1]); return nums; } }; 1 2 3 4 5 6 7 8 9 10 11class Solution { public int[] rearrangeArray(int[] nums) { Arrays.sort(nums); for (int i = 1; i < nums.length; i += 2) { final int temp = nums[i]; nums[i] = nums[i - 1]; nums[i - 1] = temp; } return nums; } } 1 2 3 4 5 6class Solution: def rearrangeArray(self, nums: list[int]) -> list[int]: nums.sort() for i in range(1, len(nums), 2): nums[i], nums[i - 1] = nums[i - 1], nums[i] return nums