📌  相关文章
📜  在数组中找到一个分区点以最大化其异或

📅  最后修改于: 2021-04-29 09:13:00             🧑  作者: Mango

给定大小为N的数组a 。任务是找到索引’i’(1 <= i <= N),使得(a [1] ^…^ a [i])+(a [i + 1] ^…^ a [N]) (x ^ y代表x和y的xor值)是最大可能的。

例子:

Input : arr[] = {1, 4, 6, 3, 8, 13, 34, 2, 21, 10}
Output : 2
Explanation : The maximum value is 68 at index 2
 
Input : arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Output : 4

天真的方法:天真的方法是使用嵌套循环。遍历数组并找到数组的xor直到第i个索引,然后从索引i + 1到元素的xor并计算可能的最大和。

下面是上述方法的实现:

C++
// CPP program to find partition point in 
// array to maximize xor sum
#include 
using namespace std;
   
// Function to find partition point in 
// array to maximize xor sum
int Xor_Sum(int arr[], int n)
{
    int sum = 0, index, left_xor = 0, right_xor = 0;
      
    // Traverse through the array
    for (int i = 0; i < n; i++) 
    {
        // Calculate xor of elements left of index i
        // including ith element
        left_xor = left_xor ^ arr[i];
        right_xor = 0;
          
        for (int j = i + 1; j < n; j++)
        {
            // Calculate xor of the elements right of
            // index i
            right_xor = right_xor ^ arr[j];
        }
          
        // Keep the maximum possible xor sum
        if (left_xor + right_xor > sum) 
        {
            sum = left_xor + right_xor;
            index = i;
        }
    }
      
    // Return the 1 based index of the array
    return index+1;
}
   
// Driver code
int main()
{
    int arr[] = { 1, 4, 6, 3, 8, 13, 34, 2, 21, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
      
    // Function call
    cout << Xor_Sum(arr, n);
      
    return 0;
}


Java
// Java program to find partition point in 
// array to maximize xor sum
class GFG
{
  
    // Function to find partition point in
    // array to maximize xor sum
    public static int Xor_Sum(int[] arr, int n) 
    {
        int sum = 0, index = -1;
        int left_xor = 0, right_xor = 0;
  
        // Traverse through the array
        for (int i = 0; i < n; i++)
        {
  
            // Calculate xor of elements left of index i
            // including ith element
            left_xor = left_xor ^ arr[i];
            right_xor = 0;
  
            for (int j = i + 1; j < n; j++)
            {
  
                // Calculate xor of the elements right of
                // index i
                right_xor = right_xor ^ arr[j];
            }
  
            // Keep the maximum possible xor sum
            if (left_xor + right_xor > sum) 
            {
                sum = left_xor + right_xor;
                index = i;
            }
        }
  
        // Return the 1 based index of the array
        return index + 1;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int[] arr = { 1, 4, 6, 3, 8, 
                      13, 34, 2, 21, 10 };
        int n = arr.length;
  
        // Function call
        System.out.println(Xor_Sum(arr, n));
  
    }
}
  
// This code is contributed by sanjeev2552


Python3
# Python3 program to find partition point in
# array to maximize xor sum
  
# Function to find partition point in
# array to maximize xor sum
def Xor_Sum(arr, n):
  
    sum = 0
    index, left_xor = 0, 0
    right_xor = 0
  
    # Traverse through the array
    for i in range(n):
  
        # Calculate xor of elements left of index i
        # including ith element
        left_xor = left_xor ^ arr[i]
        right_xor = 0
  
        for j in range(i + 1, n):
              
            # Calculate xor of the elements 
            # right of index i
            right_xor = right_xor ^ arr[j]
  
        # Keep the maximum possible xor sum
        if (left_xor + right_xor > sum):
            sum = left_xor + right_xor
            index = i
  
    # Return the 1 based index of the array
    return index + 1
  
# Driver code
arr = [ 1, 4, 6, 3, 8,
        13, 34, 2, 21, 10]
n = len(arr)
  
# Function call
print(Xor_Sum(arr, n))
  
# This code is contributed by Mohit Kumar


C#
// C# program to find partition point in 
// array to maximize xor sum
using System;
  
class GFG
{
  
    // Function to find partition point in
    // array to maximize xor sum
    public static int Xor_Sum(int[] arr,
                              int n) 
    {
        int sum = 0, index = -1;
        int left_xor = 0, right_xor = 0;
  
        // Traverse through the array
        for (int i = 0; i < n; i++)
        {
  
            // Calculate xor of elements left of index i
            // including ith element
            left_xor = left_xor ^ arr[i];
            right_xor = 0;
  
            for (int j = i + 1; j < n; j++)
            {
  
                // Calculate xor of the elements 
                // right of index i
                right_xor = right_xor ^ arr[j];
            }
  
            // Keep the maximum possible xor sum
            if (left_xor + right_xor > sum) 
            {
                sum = left_xor + right_xor;
                index = i;
            }
        }
  
        // Return the 1 based index of the array
        return index + 1;
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        int[] arr = { 1, 4, 6, 3, 8, 
                      13, 34, 2, 21, 10 };
        int n = arr.Length;
  
        // Function call
        Console.WriteLine (Xor_Sum(arr, n));
    }
}
  
// This code is contributed by PrinciRaj1992


C++
// CPP program to find partition point in 
// array to maximize xor sum
#include 
using namespace std;
   
// Function to calculate Prefix Xor array
void ComputePrefixXor(int arr[], int PrefixXor[], int n)
{
    PrefixXor[0] = arr[0];
      
    // Calculating prefix xor
    for (int i = 1; i < n; i++)
        PrefixXor[i] = PrefixXor[i - 1] ^ arr[i];
}
   
// Function to find partition point in 
// array to maximize xor sum
int Xor_Sum(int arr[], int n)
{
    // To store prefix xor
    int PrefixXor[n];
      
    // Compute the prfix xor
    ComputePrefixXor(arr, PrefixXor, n);
  
    // To store sum and index
    int sum = 0, index;
  
    // Calculate the maximum sum that can be obtained
    // splitting the array at some index i
    for (int i = 0; i < n; i++) 
    {
        // PrefixXor[i] = Xor of all arr 
        // elements till i'th index PrefixXor[n-1]
        //  ^ PrefixXor[i] = Xor of all elements 
        // from i+1' th index to n-1'th index
        if (PrefixXor[i] + (PrefixXor[n - 1] ^ 
                PrefixXor[i]) > sum) 
        {
            sum = PrefixXor[i] + 
                 (PrefixXor[n - 1] ^ PrefixXor[i]);
            index = i;
        }
    }
      
    // Return the index
    return index+1;
}
   
// Driver code
int main()
{
    int arr[] = { 1, 4, 6, 3, 8, 13, 34, 2, 21, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
      
    // Function call
    cout << Xor_Sum(arr, n);
      
    return 0;
}


Java
// Java program to find partition point in 
// array to maximize xor sum
import java.util.*;
  
class GFG 
{
  
// Function to calculate Prefix Xor array
static void ComputePrefixXor(int arr[], 
                             int PrefixXor[],
                             int n)
{
    PrefixXor[0] = arr[0];
      
    // Calculating prefix xor
    for (int i = 1; i < n; i++)
        PrefixXor[i] = PrefixXor[i - 1] ^ arr[i];
}
  
// Function to find partition point in 
// array to maximize xor sum
static int Xor_Sum(int arr[], int n)
{
    // To store prefix xor
    int []PrefixXor = new int[n];
      
    // Compute the prfix xor
    ComputePrefixXor(arr, PrefixXor, n);
  
    // To store sum and index
    int sum = 0, index = 0;
  
    // Calculate the maximum sum that can be obtained
    // splitting the array at some index i
    for (int i = 0; i < n; i++) 
    {
        // PrefixXor[i] = Xor of all arr 
        // elements till i'th index PrefixXor[n-1]
        // ^ PrefixXor[i] = Xor of all elements 
        // from i+1' th index to n-1'th index
        if (PrefixXor[i] + (PrefixXor[n - 1] ^ 
                PrefixXor[i]) > sum) 
        {
            sum = PrefixXor[i] + 
                (PrefixXor[n - 1] ^ PrefixXor[i]);
            index = i;
        }
    }
      
    // Return the index
    return index+1;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 1, 4, 6, 3, 8, 
                 13, 34, 2, 21, 10 };
    int n = arr.length;
      
    // Function call
    System.out.println(Xor_Sum(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find partition point in 
# array to maximize xor sum
  
# Function to calculate Prefix Xor array
def ComputePrefixXor(arr, PrefixXor, n):
    PrefixXor[0] = arr[0];
      
    # Calculating prefix xor
    for i in range(1, n):
        PrefixXor[i] = PrefixXor[i - 1] ^ arr[i];
  
# Function to find partition point in 
# array to maximize xor sum
def Xor_Sum(arr, n):
    # To store prefix xor
    PrefixXor = [0] * n;
      
    # Compute the prfix xor
    ComputePrefixXor(arr, PrefixXor, n);
  
    # To store sum and index
    sum, index = 0, 0;
  
    # Calculate the maximum sum that can be obtained
    # splitting the array at some index i
    for i in range(n):
          
        # PrefixXor[i] = Xor of all arr 
        # elements till i'th index PrefixXor[n-1]
        # ^ PrefixXor[i] = Xor of all elements 
        # from i+1' th index to n-1'th index
        if (PrefixXor[i] + (PrefixXor[n - 1] ^ 
                            PrefixXor[i]) > sum): 
            sum = PrefixXor[i] +\
                 (PrefixXor[n - 1] ^ PrefixXor[i]);
            index = i;
  
    # Return the index
    return index + 1;
  
# Driver code
arr = [ 1, 4, 6, 3, 8, 13, 34, 2, 21, 10 ];
n = len(arr);
  
# Function call
print(Xor_Sum(arr, n));
  
# This code is contributed by Rajput-Ji


C#
// C# program to find partition point in 
// array to maximize xor sum
using System;
  
class GFG 
{
  
// Function to calculate Prefix Xor array
static void ComputePrefixXor(int[] arr, 
                             int[] PrefixXor,
                              int n)
{
    PrefixXor[0] = arr[0];
      
    // Calculating prefix xor
    for (int i = 1; i < n; i++)
        PrefixXor[i] = PrefixXor[i - 1] ^ arr[i];
}
  
// Function to find partition point in 
// array to maximize xor sum
static int Xor_Sum(int[] arr, int n)
{
    // To store prefix xor
    int []PrefixXor = new int[n];
      
    // Compute the prfix xor
    ComputePrefixXor(arr, PrefixXor, n);
  
    // To store sum and index
    int sum = 0, index = 0;
  
    // Calculate the maximum sum that can be obtained
    // splitting the array at some index i
    for (int i = 0; i < n; i++) 
    {
        // PrefixXor[i] = Xor of all arr 
        // elements till i'th index PrefixXor[n-1]
        // ^ PrefixXor[i] = Xor of all elements 
        // from i+1' th index to n-1'th index
        if (PrefixXor[i] + (PrefixXor[n - 1] ^ 
                            PrefixXor[i]) > sum) 
        {
            sum = PrefixXor[i] + (PrefixXor[n - 1] ^
                                  PrefixXor[i]);
            index = i;
        }
    }
      
    // Return the index
    return index + 1;
}
  
// Driver code
public static void Main() 
{
    int[] arr = { 1, 4, 6, 3, 8, 
                13, 34, 2, 21, 10 };
    int n = arr.Length;
      
    // Function call
    Console.WriteLine(Xor_Sum(arr, n));
}
}
  
// This code is contributed by Code_Mech


输出:

2 

时间复杂度: O(N ^ 2)。

高效的方法:一种有效的方法是使用前缀xor数组。在任何索引’i’上,PrefixXor [i]都给我们arr [1] ^ arr [1] ^ …. ^ arr [i]并得到arr [i + 1] ^ arr [i + 2] ^。 。 ^ arr [n-1] ,找到PrefixXor [i] ^ PrefixXor [n]

下面是上述方法的实现:

C++

// CPP program to find partition point in 
// array to maximize xor sum
#include 
using namespace std;
   
// Function to calculate Prefix Xor array
void ComputePrefixXor(int arr[], int PrefixXor[], int n)
{
    PrefixXor[0] = arr[0];
      
    // Calculating prefix xor
    for (int i = 1; i < n; i++)
        PrefixXor[i] = PrefixXor[i - 1] ^ arr[i];
}
   
// Function to find partition point in 
// array to maximize xor sum
int Xor_Sum(int arr[], int n)
{
    // To store prefix xor
    int PrefixXor[n];
      
    // Compute the prfix xor
    ComputePrefixXor(arr, PrefixXor, n);
  
    // To store sum and index
    int sum = 0, index;
  
    // Calculate the maximum sum that can be obtained
    // splitting the array at some index i
    for (int i = 0; i < n; i++) 
    {
        // PrefixXor[i] = Xor of all arr 
        // elements till i'th index PrefixXor[n-1]
        //  ^ PrefixXor[i] = Xor of all elements 
        // from i+1' th index to n-1'th index
        if (PrefixXor[i] + (PrefixXor[n - 1] ^ 
                PrefixXor[i]) > sum) 
        {
            sum = PrefixXor[i] + 
                 (PrefixXor[n - 1] ^ PrefixXor[i]);
            index = i;
        }
    }
      
    // Return the index
    return index+1;
}
   
// Driver code
int main()
{
    int arr[] = { 1, 4, 6, 3, 8, 13, 34, 2, 21, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
      
    // Function call
    cout << Xor_Sum(arr, n);
      
    return 0;
}

Java

// Java program to find partition point in 
// array to maximize xor sum
import java.util.*;
  
class GFG 
{
  
// Function to calculate Prefix Xor array
static void ComputePrefixXor(int arr[], 
                             int PrefixXor[],
                             int n)
{
    PrefixXor[0] = arr[0];
      
    // Calculating prefix xor
    for (int i = 1; i < n; i++)
        PrefixXor[i] = PrefixXor[i - 1] ^ arr[i];
}
  
// Function to find partition point in 
// array to maximize xor sum
static int Xor_Sum(int arr[], int n)
{
    // To store prefix xor
    int []PrefixXor = new int[n];
      
    // Compute the prfix xor
    ComputePrefixXor(arr, PrefixXor, n);
  
    // To store sum and index
    int sum = 0, index = 0;
  
    // Calculate the maximum sum that can be obtained
    // splitting the array at some index i
    for (int i = 0; i < n; i++) 
    {
        // PrefixXor[i] = Xor of all arr 
        // elements till i'th index PrefixXor[n-1]
        // ^ PrefixXor[i] = Xor of all elements 
        // from i+1' th index to n-1'th index
        if (PrefixXor[i] + (PrefixXor[n - 1] ^ 
                PrefixXor[i]) > sum) 
        {
            sum = PrefixXor[i] + 
                (PrefixXor[n - 1] ^ PrefixXor[i]);
            index = i;
        }
    }
      
    // Return the index
    return index+1;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 1, 4, 6, 3, 8, 
                 13, 34, 2, 21, 10 };
    int n = arr.length;
      
    // Function call
    System.out.println(Xor_Sum(arr, n));
}
}
  
// This code is contributed by 29AjayKumar

Python3

# Python3 program to find partition point in 
# array to maximize xor sum
  
# Function to calculate Prefix Xor array
def ComputePrefixXor(arr, PrefixXor, n):
    PrefixXor[0] = arr[0];
      
    # Calculating prefix xor
    for i in range(1, n):
        PrefixXor[i] = PrefixXor[i - 1] ^ arr[i];
  
# Function to find partition point in 
# array to maximize xor sum
def Xor_Sum(arr, n):
    # To store prefix xor
    PrefixXor = [0] * n;
      
    # Compute the prfix xor
    ComputePrefixXor(arr, PrefixXor, n);
  
    # To store sum and index
    sum, index = 0, 0;
  
    # Calculate the maximum sum that can be obtained
    # splitting the array at some index i
    for i in range(n):
          
        # PrefixXor[i] = Xor of all arr 
        # elements till i'th index PrefixXor[n-1]
        # ^ PrefixXor[i] = Xor of all elements 
        # from i+1' th index to n-1'th index
        if (PrefixXor[i] + (PrefixXor[n - 1] ^ 
                            PrefixXor[i]) > sum): 
            sum = PrefixXor[i] +\
                 (PrefixXor[n - 1] ^ PrefixXor[i]);
            index = i;
  
    # Return the index
    return index + 1;
  
# Driver code
arr = [ 1, 4, 6, 3, 8, 13, 34, 2, 21, 10 ];
n = len(arr);
  
# Function call
print(Xor_Sum(arr, n));
  
# This code is contributed by Rajput-Ji

C#

// C# program to find partition point in 
// array to maximize xor sum
using System;
  
class GFG 
{
  
// Function to calculate Prefix Xor array
static void ComputePrefixXor(int[] arr, 
                             int[] PrefixXor,
                              int n)
{
    PrefixXor[0] = arr[0];
      
    // Calculating prefix xor
    for (int i = 1; i < n; i++)
        PrefixXor[i] = PrefixXor[i - 1] ^ arr[i];
}
  
// Function to find partition point in 
// array to maximize xor sum
static int Xor_Sum(int[] arr, int n)
{
    // To store prefix xor
    int []PrefixXor = new int[n];
      
    // Compute the prfix xor
    ComputePrefixXor(arr, PrefixXor, n);
  
    // To store sum and index
    int sum = 0, index = 0;
  
    // Calculate the maximum sum that can be obtained
    // splitting the array at some index i
    for (int i = 0; i < n; i++) 
    {
        // PrefixXor[i] = Xor of all arr 
        // elements till i'th index PrefixXor[n-1]
        // ^ PrefixXor[i] = Xor of all elements 
        // from i+1' th index to n-1'th index
        if (PrefixXor[i] + (PrefixXor[n - 1] ^ 
                            PrefixXor[i]) > sum) 
        {
            sum = PrefixXor[i] + (PrefixXor[n - 1] ^
                                  PrefixXor[i]);
            index = i;
        }
    }
      
    // Return the index
    return index + 1;
}
  
// Driver code
public static void Main() 
{
    int[] arr = { 1, 4, 6, 3, 8, 
                13, 34, 2, 21, 10 };
    int n = arr.Length;
      
    // Function call
    Console.WriteLine(Xor_Sum(arr, n));
}
}
  
// This code is contributed by Code_Mech

输出:

2