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.
Xor-sequence
Xor-sequence
Sort by
recency
|
152 Discussions
|
Please Login in order to post a comment
The same modulus 8 solution in JavaScript/Node.js:
function getXor(x) { let n = x % 8; if (n === 0 || n === 1) return x; if (n === 2 || n === 3) return 2; if (n === 4 || n === 5) return x + 2; return 0; }
function xorSequence(l, r) { return BigInt(getXor(r)) ^ BigInt(getXor(l - 1)); }
There is a pattern of 4 consecutive elements in the array A, for example: A[0] -> A[3], and so on. The first number of the sequence = it's index, the 2nd number is 1, the third = index of the fourth and the fourth = 0. And, when you xor all 4 together, you always get 2. When you xor 2 sequences, you get 0, so it doesn't affect the remaining xor. Base on that, you can just find the effective xor, which are from left to the next start sequence index and from right back to the index after the last 8-element sequence.
Haskell
So, if you look at the sequence of running XORs over the XORs, there's a pattern. I didn't give it much thought after that -- just implement the lookup function.
Hi there! Here is my Java solution for the challenge. If you'd like to discuss programming or any other topics, don't hesitate to reach out!
https://github.com/eduardocintra/hacker-rank-solutions/blob/master/src/br/com/eduardocintra/medium/xorsequence/XorSequence.java
c++ optimal one