java.lang.Object
g1901_2000.s1968_array_with_elements_not_equal_to_average_of_neighbors.Solution

public class Solution extends Object
1968 - Array With Elements Not Equal to Average of Neighbors.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <strong>distinct</strong> integers. You want to rearrange the elements in the array such that every element in the rearranged array is <strong>not</strong> equal to the <strong>average</strong> of its neighbors.</p> <p>More formally, the rearranged array should have the property such that for every <code>i</code> in the range <code>1 <= i < nums.length - 1</code>, <code>(nums[i-1] + nums[i+1]) / 2</code> is <strong>not</strong> equal to <code>nums[i]</code>.</p> <p>Return <em><strong>any</strong> rearrangement of</em> <code>nums</code> <em>that meets the requirements</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4,5]</p> <p><strong>Output:</strong> [1,2,4,5,3]</p> <p><strong>Explanation:</strong></p> <p>When i=1, nums[i] = 2, and the average of its neighbors is (1+4) / 2 = 2.5.</p> <p>When i=2, nums[i] = 4, and the average of its neighbors is (2+5) / 2 = 3.5.</p> <p>When i=3, nums[i] = 5, and the average of its neighbors is (4+3) / 2 = 3.5.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [6,2,0,9,7]</p> <p><strong>Output:</strong> [9,7,6,2,0]</p> <p><strong>Explanation:</strong></p> <p>When i=1, nums[i] = 7, and the average of its neighbors is (9+6) / 2 = 7.5.</p> <p>When i=2, nums[i] = 6, and the average of its neighbors is (7+2) / 2 = 4.5.</p> <p>When i=3, nums[i] = 2, and the average of its neighbors is (6+0) / 2 = 3.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 <= nums.length <= 10<sup>5</sup></code></li> <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • rearrangeArray

      public int[] rearrangeArray(int[] nums)