java.lang.Object
g1601_1700.s1674_minimum_moves_to_make_array_complementary.Solution

public class Solution extends Object
1674 - Minimum Moves to Make Array Complementary.<p>Medium</p> <p>You are given an integer array <code>nums</code> of <strong>even</strong> length <code>n</code> and an integer <code>limit</code>. In one move, you can replace any integer from <code>nums</code> with another integer between <code>1</code> and <code>limit</code>, inclusive.</p> <p>The array <code>nums</code> is <strong>complementary</strong> if for all indices <code>i</code> ( <strong>0-indexed</strong> ), <code>nums[i] + nums[n - 1 - i]</code> equals the same number. For example, the array <code>[1,2,3,4]</code> is complementary because for all indices <code>i</code>, <code>nums[i] + nums[n - 1 - i] = 5</code>.</p> <p>Return the <em><strong>minimum</strong> number of moves required to make</em> <code>nums</code> <em><strong>complementary</strong></em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,4,3], limit = 4</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> In 1 move, you can change nums to [1,2,2,3] (underlined elements are changed).</p> <p>nums[0] + nums[3] = 1 + 3 = 4.</p> <p>nums[1] + nums[2] = 2 + 2 = 4.</p> <p>nums[2] + nums[1] = 2 + 2 = 4.</p> <p>nums[3] + nums[0] = 3 + 1 = 4.</p> <p>Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,2,1], limit = 2</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> In 2 moves, you can change nums to [2,2,2,2]. You cannot change any number to 3 since 3 > limit.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,2,1,2], limit = 2</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> nums is already complementary.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>2 <= n <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= limit <= 10<sup>5</sup></code></li> <li><code>n</code> is even.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minMoves

      public int minMoves(int[] nums, int limit)