Sansa and XOR : Challenge | Bit Manipulation | Algorithms | HackerRank
Sansa has an array. She wants to find the value obtained by XOR-ing the contiguous subarrays, followed by XOR-ing the values thus obtained. Can you help her in this task?
If it has odd number of elements, only need to XOR it’s odd index elements.
Read full article from Sansa and XOR : Challenge | Bit Manipulation | Algorithms | HackerRank
Sansa has an array. She wants to find the value obtained by XOR-ing the contiguous subarrays, followed by XOR-ing the values thus obtained. Can you help her in this task?
Test case #00:
1⊕2⊕3⊕(1⊕2)⊕(2⊕3)⊕(1⊕2⊕3)=2
1⊕2⊕3⊕(1⊕2)⊕(2⊕3)⊕(1⊕2⊕3)=2
Test case #01:
4⊕5⊕7⊕5⊕(4⊕5)⊕(5⊕7)⊕(7⊕5)⊕(4⊕5⊕7)⊕(5⊕7⊕5)⊕(4⊕5⊕7⊕5)=0
Note: if the array have even number of elements, the result must be 0.4⊕5⊕7⊕5⊕(4⊕5)⊕(5⊕7)⊕(7⊕5)⊕(4⊕5⊕7)⊕(5⊕7⊕5)⊕(4⊕5⊕7⊕5)=0
If it has odd number of elements, only need to XOR it’s odd index elements.
As we know ``` ``` So, When we write all substrings, We have to check how many numbers are coming even number of time and how many numbers coming odd number of times. So, If the list is 0 indexed. i.e. numbers are indexed as ``` ``` Number at ``` ``` index will come ``` ``` number of times.
So, if ``` ``` is even, either ``` ``` or ``` ``` will be even, So each number will come even number of times. So, answer will be ``` ```. If ``` ``` is odd, and ``` ``` is even number, the give product will be odd. So, answer will be XOR of even indexed numbers.
t
=
int
(
raw_input
())
for
i
in
xrange
(t):
n
=
int
(
raw_input
())
nums
=
[
int
(x)
for
x
in
raw_input
().split()]
if
n
%
2
=
=
0
:
print
0
else
:
result
=
0
for
j
in
xrange
(n):
if
(j
+
1
)
%
2
=
=
1
:
result
=
result ^ nums[j]
print
result