To solve the Minimum Moves to Make Array Complementary problem, we use a difference array technique to efficiently track the minimum moves needed across all possible target sums. For each pair of elements (left, right) in the array, we identify four key ranges: [2, min(left,right)+1] requires 2 moves, [min(left,right)+1, left+right] requires 1 move, [left+right+1, max(left,right)+limit] requires 1 move, and [max(left,right)+limit+1, 2*limit] requires 2 moves. By building a difference array and computing prefix sums, we can find the minimum moves in O(N + L) time complexity, where N is the array length and L is the limit.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Minimum Moves to Make Array Complementary | LeetCode 1674 - PythonAdded:
Hello everyone. I'm Paul and this is problem 1,674.
Minimum moves to make array complementary. This is a medium problem that might be very tricky. So, next I will show you how to solve it step by step. We are given an integer array nums of even length and an integer limit. So, let's say that we're given this input and we have to return the minimum number of moves to make nums complementary.
Here is the requirement for a complementary array. For all indices, nums at the current index plus nums at the opposite index equals the same number. For example, in this array, if we do 1 + 4, we get 5 and 2 + 3 is also 5. Even that in both cases we get 5, this is a complementary array. So, we need to get the minimum number of moves to make nums complementary.
Here, if we do 1 + 3, the result is 4. 2 + 4 is 6. These two numbers are different, so we need to use one or more moves. Here, we can replace this four with a two because two is in between one and the limit. Here, the limit is four.
Which means that now we can do 1 + 3.
This is 4 and 2 + 2 is 4 also. Now, these two numbers are equal and the minimum number of moves that we need to use in order to reach this is one. So, the result here is one.
The main observation to solve this problem is here. What if we take this pair, 1 3? Let's say that we use zero operations. The total sum that we can get in this scenario is 1 + 3, which is 4. So, now we are here in this scenario.
We take left plus right. We don't change anything. Here, we use zero moves. As you can see, here m is equal to zero.
Now, what's the minimum possible number that we can get? Well, we can replace this with the minimum possible number, so this stays at one. We replace this with one, and we do 1 + 1, this is equal to two. Now, we are in this scenario. In order to do this, we need to use two operations. So, here we use two operations in this scenario.
Now, what's the maximum possible number that we can get? Well, we can replace this number with the limit, this is four. We do the same with this number, and 4 + 4 is equal to eight. This is the same as limit * 2, so now we are in this scenario, and we need two operations to reach this. So, here the number of moves is two.
Now, what if we want to use only one move? What's the minimum possible sum that we can get? Well, we can replace the maximum number between one and three with the minimum possible number, which is one, so now we are using only one operation, and we do 1 + 1, which is equal to two. Now, we are in this scenario. We are taking the minimum number between one and three plus one because of this operation. Now, we are here, and in this scenario, we use one move.
Now, what if we use one operation, but we want to get the maximum possible sum?
Well, we can do something very similar.
We take the maximum number between one and three, which is three, plus the limit, which means that we replace this number with the limit four.
4 + 3 is seven. So, now we are here. We take the maximum between left and right plus the limit, and here we are using one operation.
And as you can see here, we have ranges.
This means that anything that goes in between two and the minimum between left and right plus one requires two moves.
Anything that goes in between the minimum between left and right plus one and left and right requires one move and so on. These are the four possible ranges. So, we know that the target to get the minimum number of moves in order to make this array complementary is somewhere in this line. So, we can perform this into an array and fill the corresponding moves at each index.
For example, let's say that we take the same here one three. What if the current target is something in between two and four? Let's say that it's three. We know that this requires only one move and this makes sense because we can replace this three with a two. Now we are changing only one element and we do one plus two. This is equal to three. So, we require only one move. And let's say that we pick something in between four and seven. Let's say that we pick six for example. Well, we can change only one element. We can replace this with three and now we do three plus three.
This is equal to six. Finally, let's say that we pick something that's on the edge. Let's say that we pick seven. We can do three plus four. Now we are changing only one element and four plus three is equal to seven and we require only one move to do this.
So, now we know how this array works, but the question is how can we build it?
And for this you should be familiar with something called difference array. So, let's see how this works. Let's say that we have this array and we are trying to add a one to everything in this range and everything in this range. As you can see here, we have overlapping ranges.
The result should be 0 1 1 2 1. Now the question is how can we get this?
Well, for this we can build a difference array where every value is zero initially. And now we can go through these indices from left to right. First, we are here and this is not the starting point of a range. So, the effect at this point is zero. We can just continue with the next index. Next, we are here and this is a starting point of this range, which means that from here to the right, we need to add one to every index. So, we can store this starting point of the effect here in the difference array. We can add one.
Next, we are here and this is not the starting point of any effect. As you can see, this index is in between left and right bounds of this range. So, we can just continue with the next index and keep zero. Next, we are here and from here to the right, we start the effect of this range. And how do we start this effect? Well, we can add one here in the difference array.
Next, we continue and we know that from here to the right, we need to remove the effect of this range. So, if here we start the effect with one, how do we remove it? Well, we can subtract one, so this is now minus one. Now, with this difference array, we can build a prefix sum array. And remember how we do this.
We are storing the accumulated sum at every index. First, we are here, the sum is zero. Next, we are here, so we do 1 + 0. Next here, 0 + 1 is 1. Here, 1 + 1 is 2. And here, 2 + -1 is 1.
Now, this is the final array. For example, if we look at the index zero, the result is zero. For indices 1 and 2, the results are 1 and 1. Here, we have overlapping ranges and remember that we need to add one to this index for every range. So, the result is two.
Next here, the result is one. We have only one range.
So, this is it and by doing this, we are solving the problem in big O of n time complexity. So, even with overlapping ranges, this is very efficient. Now, going back to the problem, we can have a difference array corresponding to this line and now we can iterate pair by pair. We start with one free. And let's say that we start with two moves. We start by replacing these two numbers with one and one. What are the corresponding effects at each point in the line? Well, if we start from two moves, we know that at the minimum between left and right plus one, meaning at two, meaning here, we need to remove the effect of one of the moves. So, here we subtract one.
Next, here at left plus right, meaning index four, we need to subtract the effect of another move. So, we go to index four and replace this with minus one.
Now, what happens here at left plus right plus one? Well, now we are in this range. We are using one move. So, we should add this move. We go to index five now, here, and add one move.
And what happens here at the maximum between left and right plus the limit plus one? Well, now we are at index eight and from here to the right, we need to add the effect of a new move.
So, we replace this with one. Now, we are looking at this pair, two and four, and we do the same. We start from two moves. And what are the corresponding effects at each point? Well, here, we are in this range. We are using only one move. So, we should subtract one move.
We go to the minimum between left and right plus one, meaning three. We go here and replace this with minus one.
Next here at six, we should subtract the effect of another move. So, we go here and subtract one.
Next at left plus right plus one from here to the right, we need to add the effect of a new move. So, we go to seven and add one. And finally here, meaning nine, we need to add the effect of a new move. So, here we add one. Now, we can continue with either a prefix sum array or a variable current. In the code, we'll use current for simplicity, but here I will show you the prefix sum approach with an array because of learning purposes. So, we start from the maximum possible number of moves, which means we replace every number here. So, we need four moves as a maximum n moves.
Here, n is four, so we start with four.
And now we build a prefix sum array as usual. We do 4 + 0, then 4 + -1, 3 + -1, 2 + -1, 1 + 1, 2 + -1, 1 + 1, 2 + 1, and 3 + 1.
So, now where do we have the minimum number of moves to make nums complementary? Well, we have them either here or here. The result is the minimum number in this array, which is one. And just to be clear, this means that we can pick either four or six as targets. If we pick four, we can replace this with two, and now we do 1 + 3, which is equal to four. 2 + 2 + 2 is equal to four, also. And if we pick six, we replace this with three, and we can do 3 + 3, which is equal to six, and 2 + 4 is also equal to six. So, these are the two valid scenarios. This is the final result.
Overall time complexity here is big O of N plus L. Why? Well, because we are going through N divided by two pairs, so we can say that this is overall N. And then this array these two arrays have a size of roughly two times limit. So, we can say that this is overall L. Overall time complexity is N plus L. And overall space complexity is big O of L because of this difference array. So, with this in mind, now let's go with the code.
First, N is the length of nums and we need a difference array. This is an array with zero times and here we take two times limit plus two.
Now, we can say for every index in range and we are going through every pair, so we can take N divided by two. Here we need to round down this number.
Every time we have a left and right.
Left is equal to nums I and right is the number at the opposite index. So, right is equal to nums at N minus one minus I.
Now, we can set the corresponding effects. So, difference at and here we take the minimum between left and right plus one. Here we need to subtract one move. Then we go to difference at left plus right and subtract another move.
Then we go to difference at left plus right plus one. And here we add one move. And finally, we go to difference at the maximum between left and right plus the limit plus one. And here we need to add one move. So, these are the four effects. Now, with this we can build our result. We can say that result and current are initially in n and we start from the maximum possible number of moves. And now we say for I in range and we go from two to two times limit plus one.
Every time we add to current, this is our prefix sum basically. We add difference I. And every time the result is the minimum between what we already have in the result and current. And finally we can return result.
So, this is the entire code. Let's submit this and see if it works.
Here we have an extra F. So, let's remove this and try again. As you can see this works and it's very efficient.
So, if you found this useful, please drop a like, subscribe, and see you in the next one. Bye-bye.
Related Videos
OpenHuman VS Hermes AI: Who Wins?
JulianGoldieSEO
285 viewsβ’2026-05-29
BREAKING: Microsoftβs New Image Generating Model Beat Out GPT 1.5 and Nano Banana 2
aimmediahouse
122 viewsβ’2026-06-03
Long-Running Agents β Build an Agent That Never Forgets with Google ADK
suryakunju
142 viewsβ’2026-05-30
This computer is made from real human brain cells. And you can buy it.
Talktmsmedia
3K viewsβ’2026-05-28
I Made the Same Anime Fight Scene in Every AI Video Generator
NobleGooseAnime
295 viewsβ’2026-05-30
Nvidia Bets Big On AI PCs | New Chip To Power Windows Laptops | Technology | AI Updates | N18S
cnnnews18
3K viewsβ’2026-06-01
I Tested NEW Opus 4.8 on Four Projects (Updated LLM Leaderboard)
AICodingDaily
298 viewsβ’2026-05-29
3D Platformer Update - NO CAPES
SolarLune
294 viewsβ’2026-05-30











