-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Solution.java
44 lines (38 loc) · 1.54 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// github.com/RodneyShag
import java.util.Scanner;
// XOR properties:
// 1) x ^ x = 0
// 2) x ^ 0 = x
// We know that a number XORed with itself is 0. Instead of calculating the subarrays directly,
// we calculate the number of times each number appears in any subarray. If it appears an even
// number of times, then (x ^ x ... ^ x) where there is an even number of "x" values will give 0.
// If there are an odd number of "x" values, the result will be "x".
//
// *** Case 1: n is even ***
//
// Each element appears an even number of times throughout the subarrays, so the answer is 0.
//
// *** Case 2: n is odd ***
//
// We notice that the odd-indexed elements appear an even number of times throughout the
// subarrays, so xoring them together will give 0, so we can ignore the odd-indexed elements.
//
// The even-indexed elements in the original subarray will appear an odd number of times
// throughout the subarrays. We can go ahead and XOR the values of the even-indexed elements
// in the original array to get our final answer.
// Time Complexity: O(n)
// Space Complexity: O(1)
static int sansaXor(int[] array) {
if (array.length % 2 == 0) { // Case 1
return 0;
} else { // Case 2
int result = 0;
for (int i = 0; i < array.length; i++) {
if (i % 2 == 0) {
result ^= array[i];
}
}
return result;
}
}
// Discuss on HackerRank: https://www.hackerrank.com/challenges/sansa-and-xor/forum/comments/284330