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

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:1Explanation:

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:

- Initialize a variable, say
**minXOR**, to store the minimum possible value of Bitwise XOR between the maximum element of one subset and the minimum element of the other subset. - Sort the array
**arr[]**in ascending order. - Traverse the array and update minXOR = min(minXOR, arr[i] ^ arr[i – 1]).

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

**Output:**

1

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.