Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: nums = [1,2,3,4,5] Output: [1,2,4,5,3] Explanation: When i=1, nums[i] = 2, and the average of its neighbors is (1+4) / 2 = 2.5. When i=2, nums[i] = 4, and the average of its neighbors is (2+5) / 2 = 3.5. When i=3, nums[i] = 5, and the average of its neighbors is (4+3) / 2 = 3.5.
Example 2:
Input: nums = [6,2,0,9,7] Output: [9,7,6,2,0] Explanation: When i=1, nums[i] = 7, and the average of its neighbors is (9+6) / 2 = 7.5. When i=2, nums[i] = 6, and the average of its neighbors is (7+2) / 2 = 4.5. When i=3, nums[i] = 2, and the average of its neighbors is (6+0) / 2 = 3. Note that the original array [6,2,0,9,7] also satisfies the conditions.
Constraints:
3 <= nums.length <= 1050 <= nums[i] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,2,3,4,5]
[6,2,0,9,7]
wiggle-sort)wiggle-sort-ii)design-neighbor-sum-service)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1968: Array With Elements Not Equal to Average of Neighbors
class Solution {
public int[] rearrangeArray(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int m = (n + 1) >> 1;
int[] ans = new int[n];
for (int i = 0, j = 0; i < n; i += 2, j++) {
ans[i] = nums[j];
if (j + m < n) {
ans[i + 1] = nums[j + m];
}
}
return ans;
}
}
// Accepted solution for LeetCode #1968: Array With Elements Not Equal to Average of Neighbors
func rearrangeArray(nums []int) (ans []int) {
sort.Ints(nums)
n := len(nums)
m := (n + 1) >> 1
for i := 0; i < m; i++ {
ans = append(ans, nums[i])
if i+m < n {
ans = append(ans, nums[i+m])
}
}
return
}
# Accepted solution for LeetCode #1968: Array With Elements Not Equal to Average of Neighbors
class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
nums.sort()
n = len(nums)
m = (n + 1) // 2
ans = []
for i in range(m):
ans.append(nums[i])
if i + m < n:
ans.append(nums[i + m])
return ans
// Accepted solution for LeetCode #1968: Array With Elements Not Equal to Average of Neighbors
/**
* [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.
*
* Example 1:
*
* Input: nums = [1,2,3,4,5]
* Output: [1,2,4,5,3]
* Explanation:
* When i=1, nums[i] = 2, and the average of its neighbors is (1+4) / 2 = 2.5.
* When i=2, nums[i] = 4, and the average of its neighbors is (2+5) / 2 = 3.5.
* When i=3, nums[i] = 5, and the average of its neighbors is (4+3) / 2 = 3.5.
*
* Example 2:
*
* Input: nums = [6,2,0,9,7]
* Output: [9,7,6,2,0]
* Explanation:
* When i=1, nums[i] = 7, and the average of its neighbors is (9+6) / 2 = 7.5.
* When i=2, nums[i] = 6, and the average of its neighbors is (7+2) / 2 = 4.5.
* When i=3, nums[i] = 2, and the average of its neighbors is (6+0) / 2 = 3.
* Note that the original array [6,2,0,9,7] also satisfies the conditions.
*
* Constraints:
*
* 3 <= nums.length <= 10^5
* 0 <= nums[i] <= 10^5
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/
// discuss: https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn rearrange_array(nums: Vec<i32>) -> Vec<i32> {
vec![]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1968_example_1() {
let nums = vec![1, 2, 3, 4, 5];
let result = vec![1, 2, 4, 5, 3];
assert_eq!(Solution::rearrange_array(nums), result);
}
#[test]
#[ignore]
fn test_1968_example_2() {
let nums = vec![6, 2, 0, 9, 7];
let result = vec![9, 7, 6, 2, 0];
assert_eq!(Solution::rearrange_array(nums), result);
}
}
// Accepted solution for LeetCode #1968: Array With Elements Not Equal to Average of Neighbors
function rearrangeArray(nums: number[]): number[] {
nums.sort((a, b) => a - b);
const n = nums.length;
const m = (n + 1) >> 1;
const ans: number[] = [];
for (let i = 0; i < m; i++) {
ans.push(nums[i]);
if (i + m < n) {
ans.push(nums[i + m]);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.