Skip to content

Commit fb3a12e

Browse files
committed
New Problem Solution - "1835. Find XOR Sum of All Pairs Bitwise AND"
1 parent 738145a commit fb3a12e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1835|[Find XOR Sum of All Pairs Bitwise AND](https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/) | [C++](./algorithms/cpp/findXorSumOfAllPairsBitwiseAnd/FindXorSumOfAllPairsBitwiseAnd.cpp)|Hard|
1213
|1834|[Single-Threaded CPU](https://leetcode.com/problems/single-threaded-cpu/) | [C++](./algorithms/cpp/singleThreadedCpu/SingleThreadedCpu.cpp)|Medium|
1314
|1833|[Maximum Ice Cream Bars](https://leetcode.com/problems/maximum-ice-cream-bars/) | [C++](./algorithms/cpp/maximumIceCreamBars/MaximumIceCreamBars.cpp)|Medium|
1415
|1832|[Check if the Sentence Is Pangram](https://leetcode.com/problems/check-if-the-sentence-is-pangram/) | [C++](./algorithms/cpp/checkIfTheSentenceIsPangram/CheckIfTheSentenceIsPangram.cpp)|Easy|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Source : https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/
2+
// Author : Hao Chen
3+
// Date : 2021-04-20
4+
5+
/*****************************************************************************************************
6+
*
7+
* The XOR sum of a list is the bitwise XOR of all its elements. If the list only contains one
8+
* element, then its XOR sum will be equal to this element.
9+
*
10+
* For example, the XOR sum of [1,2,3,4] is equal to 1 XOR 2 XOR 3 XOR 4 = 4, and the XOR sum
11+
* of [3] is equal to 3.
12+
*
13+
* You are given two 0-indexed arrays arr1 and arr2 that consist only of non-negative integers.
14+
*
15+
* Consider the list containing the result of arr1[i] AND arr2[j] (bitwise AND) for every (i, j) pair
16+
* where 0 <= i < arr1.length and 0 <= j < arr2.length.
17+
*
18+
* Return the XOR sum of the aforementioned list.
19+
*
20+
* Example 1:
21+
*
22+
* Input: arr1 = [1,2,3], arr2 = [6,5]
23+
* Output: 0
24+
* Explanation: The list = [1 AND 6, 1 AND 5, 2 AND 6, 2 AND 5, 3 AND 6, 3 AND 5] = [0,1,2,0,2,1].
25+
* The XOR sum = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0.
26+
*
27+
* Example 2:
28+
*
29+
* Input: arr1 = [12], arr2 = [4]
30+
* Output: 4
31+
* Explanation: The list = [12 AND 4] = [4]. The XOR sum = 4.
32+
*
33+
* Constraints:
34+
*
35+
* 1 <= arr1.length, arr2.length <= 10^5
36+
* 0 <= arr1[i], arr2[j] <= 10^9
37+
******************************************************************************************************/
38+
39+
class Solution {
40+
public:
41+
int getXORSum(vector<int>& arr1, vector<int>& arr2) {
42+
int x = arr1[0];
43+
for(int i = 1; i < arr1.size(); i++) {
44+
x ^= arr1[i];
45+
}
46+
int y = arr2[0];
47+
for(int i = 1; i< arr2.size(); i++) {
48+
y ^= arr2[i];
49+
}
50+
return x&y;
51+
}
52+
};

0 commit comments

Comments
 (0)