Minimum absolute difference of XOR values of two subarrays

Given an array containing n numbers. The problem is to split the array into two subarrays such that the absolute difference of the xor values of the two subarrays is minimum.
Note: Array contains at least 2 numbers.

Examples:

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

Naive Approach: Using two for loops find the xor values of the two subarrays sub_arr1[1..i] and sub_arr2[i+1..n] for every i = 1 to n-1. Find their absolute difference and accordingly update the minimum absolute difference.
Time Complexity: O(n2)

Efficient Approach: It is based on the following properties of the ^(xor) operator:

  1. a ^ 0 = a.
  2. a ^ a = 0.

Algorithm:

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the minimum 
// absolute difference of the xor values
// of the two subarrays
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to find the minimum 
// absolute difference of the xor values
// of the two subarrays
  
// function to find the minimum absolute
// difference of the xor values of the
// two subarrays
function minDiffBtwXorValues($arr, $n)
{
      
    // to store the xor value
    // of the entire array
    $tot_xor = 0;
      
    for ($i = 0; $i < $n; $i++)
        $tot_xor ^= $arr[$i];
  
    // 'part_xor' to store the
    // xor value of some subarray
    $part_xor = 0; $min = PHP_INT_MAX;
  
    for ( $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 Code
    $arr = array(12, 6, 20, 14, 38, 6);
    $n = count($arr);
    echo "Minimum Absolute Difference = "
        , minDiffBtwXorValues($arr, $n);
          
// This code is contributed by anuj_67.
?>

chevron_right



Output:

Minimum Absolute Difference = 16

Time Complexity: O(n)

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.