Partition array into two subsets with minimum Bitwise XOR between their maximum and minimum
Last Updated :
30 Apr, 2021
Given an array arr[] of size N, the task is to split the array into two subsets such that the Bitwise XOR between the maximum of the first subset and minimum of the second subset is minimum.
Examples:
Input: arr[] = {3, 1, 2, 6, 4}
Output: 1
Explanation:
Splitting the given array in two subsets {1, 3}, {2, 4, 6}.
The maximum of the first subset is 3 and the minimum of the second subset is 2.
Therefore, their bitwise XOR is equal to 1.
Input: arr[] = {2, 1, 3, 2, 4, 3}
Output: 0
Approach: The idea is to find the two elements in the array such that the Bitwise XOR between the two array elements is minimum. Follow the steps below to solve the problem:
Below is the implementation of above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to split the array into two subset
// such that the Bitwise XOR between the maximum
// of one subset and minimum of other is minimum
int splitArray(int arr[], int N)
{
// Sort the array in
// increasing order
sort(arr, arr + N);
int result = INT_MAX;
// Calculating the min Bitwise XOR
// between consecutive elements
for (int i = 1; i < N; i++) {
result = min(result,
arr[i] - arr[i - 1]);
}
// Return the final
// minimum Bitwise XOR
return result;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 3, 1, 2, 6, 4 };
// Size of array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << splitArray(arr, N);
return 0;
}
Java
// java program for the above approach
import java.util.*;
class GFG{
// Function to split the array into two subset
// such that the Bitwise XOR between the maximum
// of one subset and minimum of other is minimum
static int splitArray(int arr[], int N)
{
// Sort the array in
// increasing order
Arrays.sort(arr);
int result = Integer.MAX_VALUE;
// Calculating the min Bitwise XOR
// between consecutive elements
for (int i = 1; i < N; i++)
{
result = Math.min(result,
arr[i] - arr[i - 1]);
}
// Return the final
// minimum Bitwise XOR
return result;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 3, 1, 2, 6, 4 };
// Size of array
int N = arr.length;
// Function Call
System.out.print(splitArray(arr, N));
}
}
Python3
# Python3 program for the above approach
# Function to split the array into two subset
# such that the Bitwise XOR between the maximum
# of one subset and minimum of other is minimum
def splitArray(arr, N):
# Sort the array in increasing
# order
arr = sorted(arr)
result = 10 ** 9
# Calculating the min Bitwise XOR
# between consecutive elements
for i in range(1, N):
result = min(result, arr[i] ^ arr[i - 1])
# Return the final
# minimum Bitwise XOR
return result
# Driver Code
if __name__ == '__main__':
# Given array arr[]
arr = [ 3, 1, 2, 6, 4 ]
# Size of array
N = len(arr)
# Function Call
print(splitArray(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to split the array into two subset
// such that the Bitwise XOR between the maximum
// of one subset and minimum of other is minimum
static int splitArray(int []arr, int N)
{
// Sort the array in increasing order
Array.Sort(arr);
int result = Int32.MaxValue;
// Calculating the min Bitwise XOR
// between consecutive elements
for (int i = 1; i < N; i++)
{
result = Math.Min(result,
arr[i] ^ arr[i - 1]);
}
// Return the final
// minimum Bitwise XOR
return result;
}
// Driver Code
public static void Main()
{
// Given array arr[]
int []arr = { 3, 1, 2, 6, 4 };
// Size of array
int N = arr.Length;
// Function Call
Console.Write(splitArray(arr, N));
}
}
JavaScript
<script>
// Javascript program for the above approach
// Function to split the array into two subset
// such that the Bitwise XOR between the maximum
// of one subset and minimum of other is minimum
function splitArray(arr, N)
{
// Sort the array in
// increasing order
arr.sort();
let result = Number.MAX_VALUE;
// Calculating the min Bitwise XOR
// between consecutive elements
for (let i = 1; i < N; i++) {
result = Math.min(result,
arr[i] - arr[i - 1]);
}
// Return the final
// minimum Bitwise XOR
return result;
}
// Driver Code
// Given array arr[]
let arr = [ 3, 1, 2, 6, 4 ];
// Size of array
let N = arr.length;
// Function Call
document.write(splitArray(arr, N));
</script>
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Similar Reads
Split an array into subarrays with maximum Bitwise XOR of their respective Bitwise OR values Given an array arr[] consisting of N integers, the task is to find the maximum Bitwise XOR of Bitwise OR of every subarray after splitting the array into subarrays(possible zero subarrays). Examples: Input: arr[] = {1, 5, 7}, N = 3Output: 7Explanation:The given array can be expressed as the 1 subarr
9 min read
Split Array into maximum Subsets with same bitwise AND Given an array arr[] of size N, the task is to find the maximum number of subsets the array can be split such that the bitwise AND of the subsets is the same. Examples: Input: N = 4, arr[] = {1, 5, 2, 8}Output: 2Explanation:1st subset -> {1, 8}; bitwise AND = 0 2nd subset -> {2, 5}; bitwise AN
10 min read
Minimum integer with at most K bits set such that their bitwise AND with N is maximum Given an integer N which can be represented in 32 bit, the task is to find another integer X that has at most K bits set in its binary representation and bitwise AND of X and N is maximum. Examples: Input: N = 5, K = 1 Output: X = 2 Explanation: Binary representation of 5 is 101, the possible value
7 min read
Minimum value to be added to maximize Bitwise XOR of the given array Given an array arr[] consisting of N integers, the task is to find an integer K, not having more than maximum bits present in any array element, which when added to the array, maximizes the Bitwise XOR of the array. Examples: Input: N = 7, arr[] = {1, 7, 8, 11, 6, 9, 6}Output: 3Explanation:Bitwise X
6 min read
Split array into minimum number of subsets having difference between maximum and minimum element at most K Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum number of sets, the array elements can be divided into such that the difference between the maximum and minimum element of each set is at most K. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 2 Output: 2E
6 min read
Maximum and minimum sum of Bitwise XOR of pairs from an array Given an array arr[] of size N, the task is to find the maximum and minimum sum of Bitwise XOR of all pairs from an array by splitting the array into N / 2 pairs. Examples: Input: arr[] = {1, 2, 3, 4}Output: 6 10Explanation:Bitwise XOR of the all possible pair splits are as follows:(1, 2), (3, 4) â
11 min read