Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/d9dd6b90ee8508d7b26b4c7f51900584 to your computer and use it in GitHub Desktop.
Save SuryaPratapK/d9dd6b90ee8508d7b26b4c7f51900584 to your computer and use it in GitHub Desktop.
class Solution {
public:
vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {
int n=nums.size();
vector<int> prefix_xor(n);
prefix_xor[0]=nums[0];
for(int i=1;i<n;++i)
prefix_xor[i] = prefix_xor[i-1] ^ nums[i];
vector<int> ans;
int mask = (1<<maximumBit)-1;
for(int i=n-1;i>=0;--i){
ans.push_back(prefix_xor[i] ^ mask);
}
return ans;
}
};
/*
//JAVA
import java.util.ArrayList;
import java.util.List;
class Solution {
public int[] getMaximumXor(int[] nums, int maximumBit) {
int n = nums.length;
int[] prefixXor = new int[n];
prefixXor[0] = nums[0];
for (int i = 1; i < n; ++i) {
prefixXor[i] = prefixXor[i - 1] ^ nums[i];
}
int[] ans = new int[n];
int mask = (1 << maximumBit) - 1;
for (int i = n - 1; i >= 0; --i) {
ans[n - 1 - i] = prefixXor[i] ^ mask;
}
return ans;
}
}
#Python
from typing import List
class Solution:
def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:
n = len(nums)
prefix_xor = [0] * n
prefix_xor[0] = nums[0]
for i in range(1, n):
prefix_xor[i] = prefix_xor[i - 1] ^ nums[i]
ans = []
mask = (1 << maximumBit) - 1
for i in range(n - 1, -1, -1):
ans.append(prefix_xor[i] ^ mask)
return ans
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment