Created
September 25, 2020 22:11
-
-
Save kamath7/39c9c310daf35cf46b183b560962f4f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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