Minimum absolute difference of XOR values of two subarrays
Last Updated :
20 Apr, 2023
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:
- a ^ 0 = a.
- 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
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minDiffBtwXorValues( int arr[], int n)
{
int tot_xor = 0;
for ( int i = 0; i < n; i++)
tot_xor ^= arr[i];
int part_xor = 0, min = INT_MAX;
for ( int i = 0; i < n - 1; i++) {
tot_xor ^= arr[i];
part_xor ^= arr[i];
if ( abs (tot_xor - part_xor) < min)
min = abs (tot_xor - part_xor);
}
return min;
}
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
import java.util.*;
import java.lang.*;
public class GfG{
public static int minDiffBtwXorValues( int arr[],
int n)
{
int tot_xor = 0 ;
for ( int i = 0 ; i < n; i++)
tot_xor ^= arr[i];
int part_xor = 0 , min = Integer.MAX_VALUE;
for ( int i = 0 ; i < n - 1 ; i++) {
tot_xor ^= arr[i];
part_xor ^= arr[i];
if (Math.abs(tot_xor - part_xor) < min)
min = Math.abs(tot_xor - part_xor);
}
return min;
}
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));
}
}
|
Python
import sys
def minDiffBtwXorValues(arr, n):
tot_xor = 0
for i in range (n):
tot_xor ^ = arr[i]
part_xor = 0
min = sys.maxint
for i in range (n - 1 ):
tot_xor ^ = arr[i]
part_xor ^ = arr[i]
if ( abs (tot_xor - part_xor) < min ):
min = abs (tot_xor - part_xor)
return min
arr = [ 12 , 6 , 20 , 14 , 38 , 6 ]
n = len (arr)
print "Minimum Absolute Difference =" , minDiffBtwXorValues(arr, n)
|
C#
using System;
class GFG {
public static int minDiffBtwXorValues( int [] arr,
int n)
{
int tot_xor = 0;
for ( int i = 0; i < n; i++)
tot_xor ^= arr[i];
int part_xor = 0, min = int .MaxValue;
for ( int i = 0; i < n - 1; i++) {
tot_xor ^= arr[i];
part_xor ^= arr[i];
if (Math.Abs(tot_xor - part_xor) < min)
min = Math.Abs(tot_xor - part_xor);
}
return min;
}
public static void Main()
{
int [] arr = { 12, 6, 20, 14, 38, 6 };
int n = 6;
Console.WriteLine( "Minimum Absolute Difference = " +
minDiffBtwXorValues(arr, n));
}
}
|
PHP
<?php
function minDiffBtwXorValues( $arr , $n )
{
$tot_xor = 0;
for ( $i = 0; $i < $n ; $i ++)
$tot_xor ^= $arr [ $i ];
$part_xor = 0; $min = PHP_INT_MAX;
for ( $i = 0; $i < $n - 1; $i ++)
{
$tot_xor ^= $arr [ $i ];
$part_xor ^= $arr [ $i ];
if ( abs ( $tot_xor - $part_xor ) < $min )
$min = abs ( $tot_xor - $part_xor );
}
return $min ;
}
$arr = array (12, 6, 20, 14, 38, 6);
$n = count ( $arr );
echo "Minimum Absolute Difference = "
, minDiffBtwXorValues( $arr , $n );
?>
|
Javascript
<script>
function minDiffBtwXorValues(arr, n)
{
let tot_xor = 0;
for (let i = 0; i < n; i++)
tot_xor ^= arr[i];
let part_xor = 0, min = Number.MAX_VALUE;
for (let i = 0; i < n - 1; i++)
{
tot_xor ^= arr[i];
part_xor ^= arr[i];
if (Math.abs(tot_xor - part_xor) < min)
min = Math.abs(tot_xor - part_xor);
}
return min;
}
let arr = [ 12, 6, 20, 14, 38, 6 ];
let n = arr.length;
document.write( "Minimum Absolute Difference = " +
minDiffBtwXorValues(arr, n));
</script>
|
Output
Minimum Absolute Difference = 16
Time Complexity: O(n)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...