Skip to content

Commit de7d25e

Browse files
authored
Added task 1674.
1 parent ab58c60 commit de7d25e

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g1601_1700.s1674_minimum_moves_to_make_array_complementary;
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #2022_04_19_Time_29_ms_(22.22%)_Space_96.3_MB_(14.29%)
4+
5+
public class Solution {
6+
public int minMoves(int[] nums, int limit) {
7+
int[] cnt = new int[limit * 2 + 1];
8+
int[] min = new int[limit * 2 + 1];
9+
int[] max = new int[limit * 2 + 2];
10+
int n = nums.length;
11+
int res = Integer.MAX_VALUE;
12+
for (int i = 0; i < n / 2; i++) {
13+
cnt[nums[i] + nums[n - i - 1]]++;
14+
min[Math.min(nums[i], nums[n - i - 1])]++;
15+
max[Math.max(nums[i], nums[n - i - 1]) + limit + 1]++;
16+
}
17+
for (int i = limit * 2 - 1; i >= 2; i--) {
18+
min[i] += min[i + 1];
19+
}
20+
for (int i = 3; i < limit * 2 + 1; i++) {
21+
max[i] += max[i - 1];
22+
}
23+
for (int i = 2; i < limit * 2 + 1; i++) {
24+
res = Math.min(res, n / 2 - cnt[i] + min[i] + max[i]);
25+
}
26+
return res;
27+
}
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
1674\. Minimum Moves to Make Array Complementary
2+
3+
Medium
4+
5+
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.
6+
7+
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`.
8+
9+
Return the _**minimum** number of moves required to make_ `nums` _**complementary**_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,4,3], limit = 4
14+
15+
**Output:** 1
16+
17+
**Explanation:** In 1 move, you can change nums to [1,2,2,3] (underlined elements are changed).
18+
19+
nums[0] + nums[3] = 1 + 3 = 4.
20+
21+
nums[1] + nums[2] = 2 + 2 = 4.
22+
23+
nums[2] + nums[1] = 2 + 2 = 4.
24+
25+
nums[3] + nums[0] = 3 + 1 = 4.
26+
27+
Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,2,2,1], limit = 2
32+
33+
**Output:** 2
34+
35+
**Explanation:** In 2 moves, you can change nums to [2,2,2,2]. You cannot change any number to 3 since 3 > limit.
36+
37+
**Example 3:**
38+
39+
**Input:** nums = [1,2,1,2], limit = 2
40+
41+
**Output:** 0
42+
43+
**Explanation:** nums is already complementary.
44+
45+
**Constraints:**
46+
47+
* `n == nums.length`
48+
* <code>2 <= n <= 10<sup>5</sup></code>
49+
* <code>1 <= nums[i] <= limit <= 10<sup>5</sup></code>
50+
* `n` is even.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1601_1700.s1674_minimum_moves_to_make_array_complementary;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minMoves() {
11+
assertThat(new Solution().minMoves(new int[] {1, 2, 4, 3}, 4), equalTo(1));
12+
}
13+
14+
@Test
15+
void minMoves2() {
16+
assertThat(new Solution().minMoves(new int[] {1, 2, 2, 1}, 2), equalTo(2));
17+
}
18+
19+
@Test
20+
void minMoves3() {
21+
assertThat(new Solution().minMoves(new int[] {1, 2, 1, 2}, 2), equalTo(0));
22+
}
23+
}

0 commit comments

Comments
 (0)