Creating maxSubarrayXOR Function

First of all, let's create the structure.

static int maxSubarrayXOR(int[] arr, int n)
{
    int ans = 0;     // Initialize result

    return ans;
}

A Simple Solution is to use two loops to find XOR of all subarrays and return the maximum.

static int maxSubarrayXOR(int[] arr, int n)
{
    int ans = 0;     // Initialize result

    // Pick starting points of subarrays
    for (int i = 0; i < n; i++)
    {
        int curr_xor = 0; // to store xor of current subarray

        // Pick ending points of subarrays starting with i
        for (int j = i; j < n; j++)
        {
            curr_xor = curr_xor ^ arr[j];
            ans = Math.Max(ans, curr_xor);
        }
    }
    return ans;
}

results matching ""

    No results matching ""