简体   繁体   English

给定一个数组; 我需要子阵列; 其元素的XOR值等于某个给定值

[英]Given an array; I need the sub-arrays; whose elements' XOR value is equal to some given value

Say I have : arr = {4, 2, 2, 6, 4} and m = 6 (I need to check which sub-arrays' XOR give 6) 说我有:arr = {4,2,2,6,4}和m = 6(我需要检查哪些子数组的XOR给出6)

So the sub-arrays will be: {4, 2}, {4, 2, 2, 6, 4}, {2, 2, 6}, and {6} (total = 4) 因此,子数组将是:{4,2},{4、2、2、6、4},{2、2、6}和{6}(总计= 4)

WHAT I NEED: Index of start and end of each of these sub-arrays (or their individual length(s)) - in O(n) time. 我需要:每个子数组的开始和结束的索引(或它们各自的长度),以O(n)时间为单位。

I needed a O(n) method - so I tried the following code I found on a website. 我需要一个O(n)方法-因此我尝试了在网站上找到的以下代码。 The problem with it is that it gives the "number" of such sub-arrays. 问题在于它给出了此类子数组的“数量”。

def subarrayXor(arr, n, m):   
    ans = 0 
    # Create a prefix xor-sum array such that 
    # xorArr[i] has value equal to XOR 
    # of all elements in arr[0 ..... i] 
    xorArr =[0 for _ in range(n)] 

    # Create map that stores number of prefix array 
    # elements corresponding to a XOR value 
    mp = dict() 

    # Initialize first element  
    # of prefix array 
    xorArr[0] = arr[0] 

    # Computing the prefix array. 
    for i in range(1, n): 
        xorArr[i] = xorArr[i - 1] ^ arr[i] 

    # Calculate the answer 
    for i in range(n): 

        # Find XOR of current prefix with m. 
        tmp = m ^ xorArr[i] 

        # If above XOR exists in map, then there 
        # is another previous prefix with same 
        # XOR, i.e., there is a subarray ending 
        # at i with XOR equal to m. 
        if tmp in mp.keys(): 
            ans = ans + (mp[tmp]) 

        # If this subarray has XOR  
        # equal to m itself. 
        if (xorArr[i] == m): 
            ans += 1

        # Add the XOR of this subarray to the map 
        mp[xorArr[i]] = mp.get(xorArr[i], 0) + 1

    return ans 

I have a solution of O(n**2). 我有一个O(n ** 2)的解决方案。 If this can be modified in some way to decrease time to O(n) it will be appreciated. 如果可以通过某种方式对此进行修改以将时间减少到O(n),将不胜感激。

n = int(input())
a = [int(i) for i in input().split()]
suff = list(a) #suffix xor
for i in range(1,n):
     suff[i] = suff[i-1]^a[i]
    #print(suff)
m = int(input())
out = list()
for i in range(0,n-1):
    for j in range(i+1,n):
        if i != 0:
            B = suff[i-1]^suff[j]
            if B == m:
                out.append([i,j])
        else:
            if suff[j] == m:
                out.append([i,j])

print(out)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从4个给定的数组(未排序)中,找到每个数组的元素,其总和等于某个数字X. - From 4 given arrays(not sorted), find the elements from each array whose sum is equal to some number X 如何将 Numpy 数组沿轴 0 划分为大小相等的 numpy arrays 列表,其中子数组的目标数量可以是任何值? - How do I divide a Numpy array along axis-0 into a list of equal sized numpy arrays where the goal number of sub-arrays can be any value? 将给定数组拆分为 K 个子数组,使得所有子 arrays 的最大总和最小 - Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum Python 列表中的一个或多个元素的和或值等于给定条件 - Python one or more elements in a list whose sum or value is equal to a given condition 在n个数组中找到一个等于给定值的总和? - Find a sum in n arrays equal to a given value? 将Numpy数组拆分为等长子数组 - Split Numpy array into equal-length sub-arrays 如何在整数数组中查找其总和在给定值范围内的所有有序元素对 - How to find all ordered pairs of elements in array of integers whose sum lies in a given range of value 当序列两端均具有特定值时,将numpy数组拆分为子数组 - Split numpy array into sub-arrays when a sequence has a particular value on either end 给定一个由 N 个整数组成的数组和一个整数 K,找出该数组中总和等于 K 的元素对的数量 - Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K 仅保留在 position 0 处具有唯一值的子数组 - Keep only sub-arrays with one unique value at position 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM