📜  具有严格正XOR的最长子数组

📅  最后修改于: 2021-05-07 05:13:45             🧑  作者: Mango

给定N个非负整数的数组arr [] 。任务是找到最长子数组的长度,以使该子数组所有元素的XOR严格为正。如果不存在这样的子数组,则打印-1

例子:

方法:

  • 如果完整数组的XOR为正,则答案等于N。
  • 如果所有元素均为零,则答案为-1 ,因为不可能获得严格的正XOR。
  • 否则,假设第一个正数的索引为l ,最后一个正数的索引为r
  • 现在,索引范围[l,r]的所有元素的XOR必须为零,因为l之前和r之后的元素均为0 ,这不会对XOR值起作用,并且原始数组的XOR为0
  • 考虑子阵列A 1 ,A 1 ,…,A r-1A l + 1 ,A l + 2 ,…,A N。
  • 第一个子数组的XOR值等于A [r] ,第二个子数组的XOR值A [l]为正。
  • 返回这两个子数组中较大的子数组的长度。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the length of the
// longest sub-array having positive XOR
int StrictlyPositiveXor(int A[], int N)
{
  
    // To store the XOR
    // of all the elements
    int allxor = 0;
  
    // To check if all the
    // elements of the array are 0s
    bool checkallzero = true;
  
    for (int i = 0; i < N; i += 1) {
  
        // Take XOR of all the elements
        allxor ^= A[i];
  
        // If any positive value is found
        // the make the checkallzero false
        if (A[i] > 0)
            checkallzero = false;
    }
  
    // If complete array is the answer
    if (allxor != 0)
        return N;
  
    // If all elements are equal to zero
    if (checkallzero)
        return -1;
  
    // Initialize l and r
    int l = N, r = -1;
  
    for (int i = 0; i < N; i += 1) {
  
        // First positive value of the array
        if (A[i] > 0) {
            l = i + 1;
            break;
        }
    }
    for (int i = N - 1; i >= 0; i -= 1) {
  
        // Last positive value of the array
        if (A[i] > 0) {
            r = i + 1;
            break;
        }
    }
  
    // Maximum length among
    // these two subarrays
    return max(N - l, r - 1);
}
  
// Driver code
int main()
{
  
    int A[] = { 1, 0, 0, 1 };
  
    int N = sizeof(A) / sizeof(A[0]);
  
    cout << StrictlyPositiveXor(A, N);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
  
// Function to return the length of the
// longest sub-array having positive XOR
static int StrictlyPositiveXor(int []A, int N)
{
  
    // To store the XOR
    // of all the elements
    int allxor = 0;
  
    // To check if all the
    // elements of the array are 0s
    boolean checkallzero = true;
  
    for (int i = 0; i < N; i += 1) 
    {
  
        // Take XOR of all the elements
        allxor ^= A[i];
  
        // If any positive value is found
        // the make the checkallzero false
        if (A[i] > 0)
            checkallzero = false;
    }
  
    // If complete array is the answer
    if (allxor != 0)
        return N;
  
    // If all elements are equal to zero
    if (checkallzero)
        return -1;
  
    // Initialize l and r
    int l = N, r = -1;
  
    for (int i = 0; i < N; i += 1)
    {
  
        // First positive value of the array
        if (A[i] > 0) 
        {
            l = i + 1;
            break;
        }
    }
    for (int i = N - 1; i >= 0; i -= 1) 
    {
  
        // Last positive value of the array
        if (A[i] > 0) 
        {
            r = i + 1;
            break;
        }
    }
  
    // Maximum length among
    // these two subarrays
    return Math.max(N - l, r - 1);
}
  
// Driver code
public static void main (String[] args) 
{
    int A[] = { 1, 0, 0, 1 };
  
    int N = A.length;
  
    System.out.print(StrictlyPositiveXor(A, N));
}
}
  
// This code is contributed by anuj_67..


Python3
# Python3 implementation of the approach 
  
# Function to return the length of the 
# longest sub-array having positive XOR 
def StrictlyPositiveXor(A, N) :
  
    # To store the XOR 
    # of all the elements 
    allxor = 0; 
  
    # To check if all the 
    # elements of the array are 0s 
    checkallzero = True; 
  
    for i in range(N) :
  
        # Take XOR of all the elements 
        allxor ^= A[i]; 
  
        # If any positive value is found 
        # the make the checkallzero false 
        if (A[i] > 0) :
            checkallzero = False; 
  
    # If complete array is the answer 
    if (allxor != 0) :
        return N; 
  
    # If all elements are equal to zero 
    if (checkallzero) :
        return -1; 
  
    # Initialize l and r 
    l = N; r = -1; 
  
    for i in range(N) : 
  
        # First positive value of the array 
        if (A[i] > 0) :
            l = i + 1; 
            break; 
              
    for i in range(N - 1, -1, -1) :
  
        # Last positive value of the array 
        if (A[i] > 0) :
            r = i + 1; 
            break; 
  
    # Maximum length among 
    # these two subarrays 
    return max(N - l, r - 1); 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    A= [ 1, 0, 0, 1 ];
    N = len(A);
    print(StrictlyPositiveXor(A, N)); 
  
    # This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
// Function to return the length of the
// longest sub-array having positive XOR
static int StrictlyPositiveXor(int []A, int N)
{
  
    // To store the XOR
    // of all the elements
    int allxor = 0;
  
    // To check if all the
    // elements of the array are 0s
    bool checkallzero = true;
  
    for (int i = 0; i < N; i += 1) 
    {
  
        // Take XOR of all the elements
        allxor ^= A[i];
  
        // If any positive value is found
        // the make the checkallzero false
        if (A[i] > 0)
            checkallzero = false;
    }
  
    // If complete array is the answer
    if (allxor != 0)
        return N;
  
    // If all elements are equal to zero
    if (checkallzero)
        return -1;
  
    // Initialize l and r
    int l = N, r = -1;
  
    for (int i = 0; i < N; i += 1)
    {
  
        // First positive value of the array
        if (A[i] > 0) 
        {
            l = i + 1;
            break;
        }
    }
    for (int i = N - 1; i >= 0; i -= 1) 
    {
  
        // Last positive value of the array
        if (A[i] > 0) 
        {
            r = i + 1;
            break;
        }
    }
  
    // Maximum length among
    // these two subarrays
    return Math.Max(N - l, r - 1);
}
  
// Driver code
public static void Main () 
{
    int []A = { 1, 0, 0, 1 };
  
    int N = A.Length;
  
    Console.WriteLine(StrictlyPositiveXor(A, N));
}
}
  
// This code is contributed by anuj_67..


输出:
3

时间复杂度: O(N)