[英]Finding Max/Min Consecutive XOR value
I have following problem : given an integer array (maximum size 50000) i have to find the maximum X such that, 我有以下问题:给定一个整数数组(最大大小为50000),我必须找到最大X,这样,
X = a[p] ^ a[p+1] ^ ... ... ^ a[q] for some p,q (p<=q)
Also i have to find the minimum value of X. 我也必须找到X的最小值。
I have tried this process , 我已经尝试过这个过程,
sum[i] = a[0] ^ a[1] ^ ... ... ^ a[i] for some i .
i pre-calculated it in O(n) and 我在O(n)中预先计算了它
then the value of X for some p,q(p<=q)
is , 那么对于某些
p,q(p<=q)
的X值为,
X = sum[q] ^ sum[p-1]
MaxAns = Max of X for every pair of p,q (p<=q)
MinAns = Min of X for every pair of p,q (p<=q)
But this process is O(n^2). 但是这个过程是O(n ^ 2)。
How can i do that without O(n^2) algorithm , something more efficient ? 没有O(n ^ 2)算法怎么办呢?
This algorithm works only for unsigned integers with limited bit width. 该算法仅适用于位宽受限的无符号整数。
sum[q]
and adding it to the radix tree, search sum[q]
in the partially built radix tree (to get a minimum value of X). sum[q]
并将其添加到基数树之间,在部分构建的基数树中搜索 sum[q]
(以获得X的最小值)。 For maximum value of X, search ~sum[q]
. ~sum[q]
。 sum[q]
(or ~sum[q]
) is missing from the tree, toggle this bit in the min/max value of X and continue search down the tree. sum[q]
(或~sum[q]
)的任何位,请在X的最小值/最大值中切换此位,然后继续在树中向下搜索。 Time complexity is O(N log M), where M is the maximum value of array's elements. 时间复杂度为O(N log M),其中M是数组元素的最大值。
This is plain wrong - not sure why but a bit of testing seems to show it's wrong. 这是完全错误的-不知道为什么,但是经过一些测试似乎表明这是错误的。
I think you can get some inspiration from the column 8 of "Programming Pearls" where the problem is basically : "Given the real vector x[n], compute the maximum sum found in any contiguous subvector". 我认为您可以从“ Programming Pearls”的第8列中获得一些启发,该问题基本上是:“给出实向量x [n],计算在任何连续子向量中找到的最大和”。
I think you can reuse the different algorithms replacing additions and subtractions by exclusive-or (most of the interesting properties are kept during the process : 0 is still the neutral elements, exclusive-or is its own inverse, commutativity). 我认为您可以重用不同的算法,用“异或”替换加法和减法(在此过程中,大多数有趣的属性都将保留:0仍然是中性元素,“异或”是其自身的逆,可交换性)。
You can find the slides : http://cs.bell-labs.com/cm/cs/pearls/s08.pdf but I definitely recommend the book. 您可以找到幻灯片: http : //cs.bell-labs.com/cm/cs/pearls/s08.pdf,但我绝对推荐这本书。
Algorithm : 算法:
Initialize: 初始化:
ans=a[0]
cur=a[0]
Loop for each element of the array: 为数组的每个元素循环:
(a) cur = max(a[i], cur^a[i])
(b) ans = max(cur, ans)
return ans
Example : Let array be 1 2 3 5 8 10 示例:令数组为1 2 3 5 8 10
ans=cur=1
for i=1:
cur = max(2,3) = 3
ans = max(1,3) = 3
for i=2:
cur = max(3,0) = 3
ans = max(3,3) = 3
for i=3:
cur = max(5,6) = 6
ans = max(6,3) = 6
for i=4:
cur = max(8,14) = 14
ans = max(6,14) = 14
for i=5:
cur = max(10,4) = 10
anx = max(14,10) = 14
So, ans = 14 因此,ans = 14
Here is my implentation in C++ 这是我对C ++的追求
int maxXOR(int a[], int n)
{
int ans = a[0],cur=a[0];
for(int i=1;i<n;i++)
{
cur=std::max(a[i],cur^a[i]);
ans=std::max(ans,cur);
}
return ans;
}
Analysis : 分析:
Time Complexity : O(n)
Space Complexity : O(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.