1968. Array With Elements Not Equal to Average of Neighbors
You are given a 0-indexed array nums of distinct integers. You want to rearrange the elements in the array such that every element in the rearranged array is not equal to the average of its neighbors.
More formally, the rearranged array should have the property such that for every i in the range 1 <= i < nums.length - 1, (nums[i-1] + nums[i+1]) / 2 is not equal to nums[i].
Return any rearrangement of nums that meets the requirements.
- greedy solution
- Time : O(n)
- Space : O(1)
1 | class Solution { |
- wiggle sort solution (based on two pointer)
- Time : O(nlogn)
- Space : O(n)
1 | class Solution { |