Skip to content

Commit 469775b

Browse files
authored
feat: add solutions to lc problem: No.2997 (doocs#2204)
No.2997.Minimum Number of Operations to Make Array XOR Equal to K
1 parent de625ff commit 469775b

File tree

7 files changed

+78
-114
lines changed

7 files changed

+78
-114
lines changed

solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README.md

+28-38
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:位运算**
58+
59+
我们可以将数组 $nums$ 中的所有元素进行异或运算,判断得到的结果与 $k$ 的二进制表示中有多少位不同,这个数就是最少操作次数。
60+
61+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
@@ -63,13 +69,7 @@
6369
```python
6470
class Solution:
6571
def minOperations(self, nums: List[int], k: int) -> int:
66-
ans = 0
67-
for i in range(20):
68-
v = 0
69-
for x in nums:
70-
v ^= x >> i & 1
71-
ans += (k >> i & 1) != v
72-
return ans
72+
return reduce(xor, nums, k).bit_count()
7373
```
7474

7575
### **Java**
@@ -79,15 +79,10 @@ class Solution:
7979
```java
8080
class Solution {
8181
public int minOperations(int[] nums, int k) {
82-
int ans = 0;
83-
for (int i = 0; i < 20; ++i) {
84-
int v = 0;
85-
for (int x : nums) {
86-
v ^= (x >> i & 1);
87-
}
88-
ans += k >> i & 1 ^ v;
82+
for (int x : nums) {
83+
k ^= x;
8984
}
90-
return ans;
85+
return Integer.bitCount(k);
9186
}
9287
}
9388
```
@@ -98,15 +93,10 @@ class Solution {
9893
class Solution {
9994
public:
10095
int minOperations(vector<int>& nums, int k) {
101-
int ans = 0;
102-
for (int i = 0; i < 20; ++i) {
103-
int v = 0;
104-
for (int x : nums) {
105-
v ^= (x >> i & 1);
106-
}
107-
ans += k >> i & 1 ^ v;
96+
for (int x : nums) {
97+
k ^= x;
10898
}
109-
return ans;
99+
return __builtin_popcount(k);
110100
}
111101
};
112102
```
@@ -115,30 +105,30 @@ public:
115105
116106
```go
117107
func minOperations(nums []int, k int) (ans int) {
118-
for i := 0; i < 20; i++ {
119-
v := 0
120-
for _, x := range nums {
121-
v ^= x >> i & 1
122-
}
123-
ans += k>>i&1 ^ v
108+
for _, x := range nums {
109+
k ^= x
124110
}
125-
return
111+
return bits.OnesCount(uint(k))
126112
}
127113
```
128114

129115
### **TypeScript**
130116

131117
```ts
132118
function minOperations(nums: number[], k: number): number {
133-
let ans = 0;
134-
for (let i = 0; i < 20; ++i) {
135-
let v = 0;
136-
for (const x of nums) {
137-
v ^= (x >> i) & 1;
138-
}
139-
ans += ((k >> i) & 1) ^ v;
119+
for (const x of nums) {
120+
k ^= x;
140121
}
141-
return ans;
122+
return bitCount(k);
123+
}
124+
125+
function bitCount(i: number): number {
126+
i = i - ((i >>> 1) & 0x55555555);
127+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
128+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
129+
i = i + (i >>> 8);
130+
i = i + (i >>> 16);
131+
return i & 0x3f;
142132
}
143133
```
144134

solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md

+28-38
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,31 @@ It can be shown that we cannot make the XOR equal to k in less than 2 operations
4848

4949
## Solutions
5050

51+
**Solution 1: Bit Manipulation**
52+
53+
We can perform a bitwise XOR operation on all elements in the array $nums$. The number of bits that differ from the binary representation of $k$ in the result is the minimum number of operations.
54+
55+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**
5460

5561
```python
5662
class Solution:
5763
def minOperations(self, nums: List[int], k: int) -> int:
58-
ans = 0
59-
for i in range(20):
60-
v = 0
61-
for x in nums:
62-
v ^= x >> i & 1
63-
ans += (k >> i & 1) != v
64-
return ans
64+
return reduce(xor, nums, k).bit_count()
6565
```
6666

6767
### **Java**
6868

6969
```java
7070
class Solution {
7171
public int minOperations(int[] nums, int k) {
72-
int ans = 0;
73-
for (int i = 0; i < 20; ++i) {
74-
int v = 0;
75-
for (int x : nums) {
76-
v ^= (x >> i & 1);
77-
}
78-
ans += k >> i & 1 ^ v;
72+
for (int x : nums) {
73+
k ^= x;
7974
}
80-
return ans;
75+
return Integer.bitCount(k);
8176
}
8277
}
8378
```
@@ -88,15 +83,10 @@ class Solution {
8883
class Solution {
8984
public:
9085
int minOperations(vector<int>& nums, int k) {
91-
int ans = 0;
92-
for (int i = 0; i < 20; ++i) {
93-
int v = 0;
94-
for (int x : nums) {
95-
v ^= (x >> i & 1);
96-
}
97-
ans += k >> i & 1 ^ v;
86+
for (int x : nums) {
87+
k ^= x;
9888
}
99-
return ans;
89+
return __builtin_popcount(k);
10090
}
10191
};
10292
```
@@ -105,30 +95,30 @@ public:
10595
10696
```go
10797
func minOperations(nums []int, k int) (ans int) {
108-
for i := 0; i < 20; i++ {
109-
v := 0
110-
for _, x := range nums {
111-
v ^= x >> i & 1
112-
}
113-
ans += k>>i&1 ^ v
98+
for _, x := range nums {
99+
k ^= x
114100
}
115-
return
101+
return bits.OnesCount(uint(k))
116102
}
117103
```
118104

119105
### **TypeScript**
120106

121107
```ts
122108
function minOperations(nums: number[], k: number): number {
123-
let ans = 0;
124-
for (let i = 0; i < 20; ++i) {
125-
let v = 0;
126-
for (const x of nums) {
127-
v ^= (x >> i) & 1;
128-
}
129-
ans += ((k >> i) & 1) ^ v;
109+
for (const x of nums) {
110+
k ^= x;
130111
}
131-
return ans;
112+
return bitCount(k);
113+
}
114+
115+
function bitCount(i: number): number {
116+
i = i - ((i >>> 1) & 0x55555555);
117+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
118+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
119+
i = i + (i >>> 8);
120+
i = i + (i >>> 16);
121+
return i & 0x3f;
132122
}
133123
```
134124

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
class Solution {
22
public:
33
int minOperations(vector<int>& nums, int k) {
4-
int ans = 0;
5-
for (int i = 0; i < 20; ++i) {
6-
int v = 0;
7-
for (int x : nums) {
8-
v ^= (x >> i & 1);
9-
}
10-
ans += k >> i & 1 ^ v;
4+
for (int x : nums) {
5+
k ^= x;
116
}
12-
return ans;
7+
return __builtin_popcount(k);
138
}
149
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
func minOperations(nums []int, k int) (ans int) {
2-
for i := 0; i < 20; i++ {
3-
v := 0
4-
for _, x := range nums {
5-
v ^= x >> i & 1
6-
}
7-
ans += k>>i&1 ^ v
2+
for _, x := range nums {
3+
k ^= x
84
}
9-
return
5+
return bits.OnesCount(uint(k))
106
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
class Solution {
22
public int minOperations(int[] nums, int k) {
3-
int ans = 0;
4-
for (int i = 0; i < 20; ++i) {
5-
int v = 0;
6-
for (int x : nums) {
7-
v ^= (x >> i & 1);
8-
}
9-
ans += k >> i & 1 ^ v;
3+
for (int x : nums) {
4+
k ^= x;
105
}
11-
return ans;
6+
return Integer.bitCount(k);
127
}
138
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
class Solution:
22
def minOperations(self, nums: List[int], k: int) -> int:
3-
ans = 0
4-
for i in range(20):
5-
v = 0
6-
for x in nums:
7-
v ^= x >> i & 1
8-
ans += (k >> i & 1) != v
9-
return ans
3+
return reduce(xor, nums, k).bit_count()
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
function minOperations(nums: number[], k: number): number {
2-
let ans = 0;
3-
for (let i = 0; i < 20; ++i) {
4-
let v = 0;
5-
for (const x of nums) {
6-
v ^= (x >> i) & 1;
7-
}
8-
ans += ((k >> i) & 1) ^ v;
2+
for (const x of nums) {
3+
k ^= x;
94
}
10-
return ans;
5+
return bitCount(k);
6+
}
7+
8+
function bitCount(i: number): number {
9+
i = i - ((i >>> 1) & 0x55555555);
10+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
11+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
12+
i = i + (i >>> 8);
13+
i = i + (i >>> 16);
14+
return i & 0x3f;
1115
}

0 commit comments

Comments
 (0)