Skip to content

Instantly share code, notes, and snippets.

@kamath7
Created September 25, 2020 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamath7/39c9c310daf35cf46b183b560962f4f2 to your computer and use it in GitHub Desktop.
Save kamath7/39c9c310daf35cf46b183b560962f4f2 to your computer and use it in GitHub Desktop.
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const int_bits = 32;
let lalle = 0;
rl.on('line',(line)=>{
let nums = line.split(' ');
if(lalle%2 === 0){
arr_length = parseInt(nums[0])
if(arr_length === 0){
return 0;
}
}else{
nums = nums.map((elem)=> parseInt(elem))
console.log(maxXorSum2(nums,nums.length))
}
lalle++
})
function maxXorSum2 (set,n){
let index = 0;
for (let i = int_bits - 1; i >= 0; i--)
{
let maxInd = index;
let maxEle = -2147483648;
for (let j = index; j < n; j++) {
if ((set[j] & (1 << i)) != 0 && set[j] > maxEle)
{
maxEle = set[j];
maxInd = j;
}
}
if (maxEle == -2147483648)
continue;
let temp = set[index];
set[index] = set[maxInd];
set[maxInd] = temp;
maxInd = index;
for (let j = 0; j < n; j++) {
if (j != maxInd && (set[j] & (1 << i)) != 0)
set[j] = set[j] ^ set[maxInd];
}
index++;
}
let res = 0;
for (let i = 0; i < n; i++)
res ^= set[i];
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment