Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    1674 - Minimum Moves to Make Array Complementary.

    Medium

    You are given an integer array nums of even length n and an integer limit. In one move, you can replace any integer from nums with another integer between 1 and limit, inclusive.

    The array nums is complementary if for all indices i ( 0-indexed ), nums[i] + nums[n - 1 - i] equals the same number. For example, the array [1,2,3,4] is complementary because for all indices i, nums[i] + nums[n - 1 - i] = 5.

    Return the minimum number of moves required to make nums complementary.

    Example 1:

    Input: nums = 1,2,4,3, limit = 4

    Output: 1

    Explanation: In 1 move, you can change nums to 1,2,2,3 (underlined elements are changed).

    nums0 + nums3 = 1 + 3 = 4.

    nums1 + nums2 = 2 + 2 = 4.

    nums2 + nums1 = 2 + 2 = 4.

    nums3 + nums0 = 3 + 1 = 4.

    Therefore, numsi + numsn-1-i = 4 for every i, so nums is complementary.

    Example 2:

    Input: nums = 1,2,2,1, limit = 2

    Output: 2

    Explanation: In 2 moves, you can change nums to 2,2,2,2. You cannot change any number to 3 since 3 > limit.

    Example 3:

    Input: nums = 1,2,1,2, limit = 2

    Output: 0

    Explanation: nums is already complementary.

    Constraints:

    • n == nums.length

    • <code>2 <= n <= 10<sup>5</sup></code>

    • <code>1 <= numsi<= limit <= 10<sup>5</sup></code>

    • n is even.

    • 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 Integer minMoves(IntArray nums, Integer limit)
      • Methods inherited from class java.lang.Object

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