📜  两个子数组的 XOR 值的最小绝对差

📅  最后修改于: 2022-05-13 01:57:50.194000             🧑  作者: Mango

两个子数组的 XOR 值的最小绝对差

给定一个包含n 个数字的数组。问题是将数组拆分为两个子数组,使两个子数组的异或值的绝对差最小。
注意:数组至少包含 2 个数字。

例子:

Input : arr[] = {12, 6, 20, 14, 38, 6}
Output : 16
The two subarrays are:
{12, 6, 20} = 12 ^ 6 ^ 20 = 30
{14, 38, 6} = 14 ^ 38 ^ 6 = 46
Absolute difference = abs(30-46) 
                    = 16

Input : arr[] = {10, 16, 9, 34, 7, 46, 23}
Output : 1

朴素的方法:使用两个 for 循环查找每个i = 1 到 n-1 的两个子数组 sub_arr1[1..i]sub_arr2[i+1..n]的异或值。找到它们的绝对差并相应地更新最小绝对差。
时间复杂度:O(n 2 )

高效方法:它基于^(xor)运算符的以下属性:

  1. 一个^ 0 =一个。
  2. a ^ a = 0。

算法:

minDiffBtwXorValues(arr, n)
    Declare tot_xor = 0
    for i = 0 to n-1
        tot_xor ^= arr[i]

    Declare part_xor = 0
    Declare min = Maximum Integer

    for i = 0 to n-2    
        tot_xor ^= arr[i]
    part_xor ^= arr[i]
    if abs(tot_xor - part_xor) < min
        min = abs(tot_xor - part_xor)
            
    return min
C++
// C++ implementation to find the minimum
// absolute difference of the xor values
// of the two subarrays
#include 
using namespace std;
 
// function to find the minimum absolute
// difference of the xor values of the
// two subarrays
int minDiffBtwXorValues(int arr[], int n)
{
    // to store the xor value of the
    // entire array
    int tot_xor = 0;
    for (int i = 0; i < n; i++)
        tot_xor ^= arr[i];
 
    // 'part_xor' to store the xor value
    // of some subarray
    int part_xor = 0, min = INT_MAX;
 
    for (int i = 0; i < n - 1; i++) {
 
        // removing the xor value of the
        // subarray [0..i] form 'tot_xor',
        // i.e, it will contain the xor
        // value of the subarray [i+1..n-1]
        tot_xor ^= arr[i];
 
        // calculating the xor value of the
        // subarray [0..i]
        part_xor ^= arr[i];
 
        // if absolute difference is minimum,
        // then update 'min'
        if (abs(tot_xor - part_xor) < min)
            min = abs(tot_xor - part_xor);
    }
 
    // required minimum absolute difference
    return min;
}
 
// Driver program to test above
int main()
{
    int arr[] = { 12, 6, 20, 14, 38, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Minimum Absolute Difference = "
         << minDiffBtwXorValues(arr, n);
    return 0;
}


Java
// Java implementation to
// find the minimum
// absolute difference
// of the xor values
// of the two subarrays
 
import java.util.*;
import java.lang.*;
 
public class GfG{
 
    // function to find
    // the minimum absolute
    // difference of the
    // xor values of the
    // two subarrays
    public static int minDiffBtwXorValues(int arr[],
    int n)
    {
        // to store the xor value of the
        // entire array
        int tot_xor = 0;
        for (int i = 0; i < n; i++)
            tot_xor ^= arr[i];
  
        // 'part_xor' to store the xor value
        // of some subarray
        int part_xor = 0, min = Integer.MAX_VALUE;
  
        for (int i = 0; i < n - 1; i++) {
  
            // removing the xor value of the
            // subarray [0..i] form 'tot_xor',
            // i.e, it will contain the xor
            // value of the subarray [i+1..n-1]
            tot_xor ^= arr[i];
  
            // calculating the xor value of the
            // subarray [0..i]
            part_xor ^= arr[i];
  
            // if absolute difference is minimum,
            // then update 'min'
            if (Math.abs(tot_xor - part_xor) < min)
                min = Math.abs(tot_xor - part_xor);
        }
  
        // required minimum absolute difference
        return min;
    }
     
    // Driver function
    public static void main(String argc[]){
 
        int arr[] = { 12, 6, 20, 14, 38, 6 };
        int n = 6;
        System.out.println("Minimum Absolute Difference = " +
        minDiffBtwXorValues(arr, n));
    }
     
}
 
// This code is contributed by Sagar Shukla


Python
# Python implementation to find the minimum
# absolute difference of the xor values
# of the two subarrays
 
import sys
 
# function to find the minimum absolute
# difference of the xor values of the
# two subarrays
def minDiffBtwXorValues(arr, n):
    
    # to store the xor value of the
    # entire array
    tot_xor = 0
     
    for i in range(n):
        tot_xor ^= arr[i]
  
    # 'part_xor' to store the xor value
    # of some subarray
    part_xor = 0
    min = sys.maxint
  
    for i in range(n - 1):
  
        # removing the xor value of the
        # subarray [0..i] form 'tot_xor',
        # i.e, it will contain the xor
        # value of the subarray [i+1..n-1]
        tot_xor ^= arr[i]
  
        # calculating the xor value of the
        # subarray [0..i]
        part_xor ^= arr[i]
  
        # if absolute difference is minimum,
        # then update 'min'
        if (abs(tot_xor - part_xor) < min):
            min = abs(tot_xor - part_xor)
  
    # required minimum absolute difference
    return min
  
# Driver program to test above
arr = [ 12, 6, 20, 14, 38, 6 ]
n = len(arr)
print "Minimum Absolute Difference =", minDiffBtwXorValues(arr, n)
 
# This code is contributed by Sachin Bisht


C#
// C# implementation to
// find the minimum
// absolute difference
// of the xor values
// of the two subarrays
using System;
 
class GFG {
     
    // function to find
    // the minimum absolute
    // difference of the
    // xor values of the
    // two subarrays
    public static int minDiffBtwXorValues(int[] arr,
                                              int n)
    {
        // to store the xor value of the
        // entire array
        int tot_xor = 0;
        for (int i = 0; i < n; i++)
            tot_xor ^= arr[i];
 
        // 'part_xor' to store the xor value
        // of some subarray
        int part_xor = 0, min = int.MaxValue;
 
        for (int i = 0; i < n - 1; i++) {
 
            // removing the xor value of the
            // subarray [0..i] form 'tot_xor',
            // i.e, it will contain the xor
            // value of the subarray [i+1..n-1]
            tot_xor ^= arr[i];
 
            // calculating the xor value of the
            // subarray [0..i]
            part_xor ^= arr[i];
 
            // if absolute difference is minimum,
            // then update 'min'
            if (Math.Abs(tot_xor - part_xor) < min)
                min = Math.Abs(tot_xor - part_xor);
        }
 
        // required minimum absolute difference
        return min;
    }
 
    // Driver function
    public static void Main()
    {
 
        int[] arr = { 12, 6, 20, 14, 38, 6 };
        int n = 6;
        Console.WriteLine("Minimum Absolute Difference = " +
                               minDiffBtwXorValues(arr, n));
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出:

Minimum Absolute Difference = 16

时间复杂度:O(n)