We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Notice that 1 occurs an even number of times, as does 2, 3, and 4. XORing any number an even number of times produces 0 as a result.
When the number of values is odd, the resulting subarrays are as follows:
Test case #2: {1,2,3}
1⊕2⊕3⊕(1⊕2)⊕(2⊕3)⊕(1⊕2⊕3)
Notice that every even index (0, 2) occurs an odd number of times and that every odd index (1) occurs an even number of times. Recall that XORing a number by itself produces 0 for any even number of XOR operations.
SPOILER
If vector<int>'s size() is even, output 0
Else XOR the vector for every even index, starting from 0
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sansa and XOR
You are viewing a single comment's thread. Return to all comments →
Problem Logic Breakdown
When the number of values is even, the resulting subarrays are as follows:
Test case #1:
{1,2,3,4}
1⊕2⊕3⊕4⊕(1⊕2)⊕(2⊕3)⊕(3⊕4)⊕(1⊕2⊕3)⊕(2⊕3⊕4)⊕(1⊕2⊕3⊕4)
1
occurs an even number of times, as does2
,3
, and4
. XORing any number an even number of times produces0
as a result.When the number of values is odd, the resulting subarrays are as follows:
{1,2,3}
1⊕2⊕3⊕(1⊕2)⊕(2⊕3)⊕(1⊕2⊕3)
(0, 2)
occurs an odd number of times and that every odd index(1)
occurs an even number of times. Recall that XORing a number by itself produces0
for any even number of XOR operations.SPOILER
vector<int>
'ssize()
is even, output0
0