Open In App

Partition array into two subsets with minimum Bitwise XOR between their maximum and minimum

Last Updated : 30 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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:
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++
Java Python3 C# JavaScript

Output: 
1

 

Time Complexity: O(N * log N) 
Auxiliary Space: O(1)


Next Article

Similar Reads