📌  相关文章
📜  按位XOR为K的最长子数组的长度

📅  最后修改于: 2021-05-25 02:25:29             🧑  作者: Mango

给定大小为N的数组arr []和整数K ,任务是找到所有元素的按位XOR等于K的最长子数组的长度。

例子:

方法:可以使用哈希和前缀和技术解决该问题。以下是观察结果:

请按照以下步骤解决问题:

  • 初始化一个变量,例如prefixXOR ,以存储所有元素的按位XOR直到给定数组的i索引。
  • 初始化一个映射,例如mp ,以存储数组的已计算前缀XOR的索引。
  • 初始化一个变量maxLen ,以存储其按位XOR等于K的最长子数组的长度。
  • 使用变量i遍历数组arr [] 。对于每个i索引,更新prefixXOR = prefixXOR ^ arr [i]并检查Map中是否存在(prefixXOR ^ K) 。如果发现为真,则更新maxLen = max(maxLen,i – mp [prefixXOR ^ K])
  • 如果Map中不存在prefixXOR ,则将prefixXOR插入Map中。
  • 最后,输出maxLen的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the length of the longest
// subarray whose bitwise XOR is equal to K
int LongestLenXORK(int arr[], int N, int K)
{
     
    // Stores prefix XOR
    // of the array
    int prefixXOR = 0;
 
    // Stores length of longest subarray
    // having bitwise XOR equal to K
    int maxLen = 0;
 
    // Stores index of prefix
    // XOR of the array
    map mp;
     
     
    // Insert 0 into the map
    mp[0] = -1;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update prefixXOR
        prefixXOR ^= arr[i];
 
        // If (prefixXOR ^ K) present
        // in the map
        if (mp.count(prefixXOR ^ K)) {
 
            // Update maxLen
            maxLen = max(maxLen,
               (i - mp[prefixXOR ^ K]));
        }
         
        // If prefixXOR not present
        // in the Map
        if (!mp.count(prefixXOR)) {
 
            // Insert prefixXOR
            // into the map
            mp[prefixXOR] = i;
        }
    }
     
    return maxLen;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 4, 7, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 1;
    cout<< LongestLenXORK(arr, N, K);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the length of the longest
// subarray whose bitwise XOR is equal to K
static int LongestLenXORK(int arr[],
                          int N, int K)
{
     
    // Stores prefix XOR
    // of the array
    int prefixXOR = 0;
 
    // Stores length of longest subarray
    // having bitwise XOR equal to K
    int maxLen = 0;
 
    // Stores index of prefix
    // XOR of the array
    HashMap mp = new HashMap();
                                       
    // Insert 0 into the map
    mp.put(0, -1);
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Update prefixXOR
        prefixXOR ^= arr[i];
 
        // If (prefixXOR ^ K) present
        // in the map
        if (mp.containsKey(prefixXOR ^ K))
        {
             
            // Update maxLen
            maxLen = Math.max(maxLen,
               (i - mp.get(prefixXOR ^ K)));
        }
         
        // If prefixXOR not present
        // in the Map
        if (!mp.containsKey(prefixXOR))
        {
             
            // Insert prefixXOR
            // into the map
            mp.put(prefixXOR, i);
        }
    }
    return maxLen;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 4, 7, 2 };
    int N = arr.length;
    int K = 1;
     
    System.out.print(LongestLenXORK(arr, N, K));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to find the length of the longest
# subarray whose bitwise XOR is equal to K
def LongestLenXORK(arr, N, K):
     
    # Stores prefix XOR
    # of the array
    prefixXOR = 0
 
    # Stores length of longest subarray
    # having bitwise XOR equal to K
    maxLen = 0
 
    # Stores index of prefix
    # XOR of the array
    mp = {}
 
    # Insert 0 into the map
    mp[0] = -1
 
    # Traverse the array
    for i in range(N):
         
        # Update prefixXOR
        prefixXOR ^= arr[i]
 
        # If (prefixXOR ^ K) present
        # in the map
        if (prefixXOR ^ K) in mp:
             
            # Update maxLen
            maxLen = max(maxLen,
                        (i - mp[prefixXOR ^ K]))
 
        # If prefixXOR not present
        # in the Map
        else:
             
            # Insert prefixXOR
            # into the map
            mp[prefixXOR] = i
       
    return maxLen
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 1, 2, 4, 7, 2 ]
    N = len(arr)
    K = 1
     
    print(LongestLenXORK(arr, N, K))
 
# This code is contributed by AnkThon


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
// Function to find the length of the longest
// subarray whose bitwise XOR is equal to K
static int longestLenXORK(int []arr,
                          int N, int K)
{
     
    // Stores prefix XOR
    // of the array
    int prefixXOR = 0;
 
    // Stores length of longest subarray
    // having bitwise XOR equal to K
    int maxLen = 0;
 
    // Stores index of prefix
    // XOR of the array
    Dictionary mp = new Dictionary();
                                       
    // Insert 0 into the map
    mp.Add(0, -1);
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Update prefixXOR
        prefixXOR ^= arr[i];
 
        // If (prefixXOR ^ K) present
        // in the map
        if (mp.ContainsKey(prefixXOR ^ K))
        {
             
            // Update maxLen
            maxLen = Math.Max(maxLen,
               (i - mp[prefixXOR ^ K]));
        }
         
        // If prefixXOR not present
        // in the Map
        if (!mp.ContainsKey(prefixXOR))
        {
             
            // Insert prefixXOR
            // into the map
            mp.Add(prefixXOR, i);
        }
    }
    return maxLen;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = {1, 2, 4, 7, 2};
    int N = arr.Length;
    int K = 1;
     
    Console.Write(longestLenXORK(arr, N, K));
}
}
 
// This code is contributed by shikhasingrajput


输出:

3

时间复杂度: O(N)
辅助空间: O(N)