Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    1968 - Array With Elements Not Equal to Average of Neighbors\.

    Medium

    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, numsi = 2, and the average of its neighbors is (1+4) / 2 = 2.5.

    When i=2, numsi = 4, and the average of its neighbors is (2+5) / 2 = 3.5.

    When i=3, numsi = 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, numsi = 7, and the average of its neighbors is (9+6) / 2 = 7.5.

    When i=2, numsi = 6, and the average of its neighbors is (7+2) / 2 = 4.5.

    When i=3, numsi = 2, and the average of its neighbors is (6+0) / 2 = 3.

    Constraints:

    • <code>3 <= nums.length <= 10<sup>5</sup></code>

    • <code>0 <= numsi<= 10<sup>5</sup></code>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final IntArray rearrangeArray(IntArray nums)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait