Menu

1 / 3

1674. Minimum Moves to Make Array Complementary

DEV Community·MD ARIFUL HAQUE·6 months ago
#WvgDaLy5
#php#leetcode#algorithms#programming#nums
Reading 0:00
15s threshold

1674. Minimum Moves to Make Array Complementary

Difficulty: Medium

Topics: Senior Staff , Array , Hash Table , Prefix Sum , Weekly Contest 217

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 .

nums[0] + nums[3] = 1 + 3 = 4.

nums[1] + nums[2] = 2 + 2 = 4.

nums[2] + nums[1] = 2 + 2 = 4.

nums[3] + nums[0] = 3 + 1 = 4.

Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.

Example 2:

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

Output:…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.