Find a number such that maximum in array is minimum possible after XOR

• Last Updated : 29 Apr, 2021

Given an array of non-negative integers. Choose an integer P and take XOR of P with all elements of the array. The task is to choose P such that the maximum value of the array is minimum possible after performing XOR of all elements of the array with P.

Examples:

Input: arr = {3, 2, 1}
Output:
Explanation:
We can choose P = 3 such that after taking XOR the maximum element is minimum possible.
After taking exclusive-OR arr = {0, 1, 2}
2 is minimum possible value.
Input: arr = {5, 1}
Output: 4

Approach:

• We can solve this problem recursively starting from the most significant bit.
• Split the elements into two groups, one with the elements which have the current bit on (1) and one with the elements which have the current bit off (0).
• If either group is empty, we can assign the current bit of P accordingly so that we have the current bit off in our answer.
• Otherwise, If both groups aren’t empty, so whatever value we assign to the current bit of P, we will have this bit on(1) in our answer.
• Now, To decide which value to assign to the current bit of P, we will recursively call for each of the groups for the next bit and return the minimum of both.

Below is the implementation of the above approach:

C++

 // C++ program that find the minimum// possible maximum#include using namespace std; // Recursive function that find the//  minimum value after exclusive-ORint RecursiveFunction(vector ref,                      int bit){    // Condition if ref size is zero or    // bit is negative then return 0    if (ref.size() == 0 || bit < 0)        return 0;     vector curr_on, curr_off;     for (int i = 0; i < ref.size(); i++)    {        // Condition if current bit is        // off then push current value        // in curr_off vector        if (((ref[i] >> bit) & 1) == 0)            curr_off.push_back(ref[i]);                 // Condition if current bit is on        // then push current value in        // curr_on vector        else            curr_on.push_back(ref[i]);    }     // Condition if curr_off is empty    // then call recursive function    // on curr_on vector    if (curr_off.size() == 0)        return RecursiveFunction(curr_on,                                 bit - 1);     // Condition if curr_on is empty    // then call recursive function    // on curr_off vector    if (curr_on.size() == 0)        return RecursiveFunction(curr_off,                                 bit - 1);     // Return the minimum of curr_off and     // curr_on and add power of 2 of    // current bit    return min(RecursiveFunction(curr_off,                                 bit - 1),               RecursiveFunction(curr_on,                                 bit - 1))           + (1 << bit);} // Function that print the minimum// value after exclusive-ORvoid PrintMinimum(int a[], int n){    vector v;     // Pushing values in vector    for (int i = 0; i < n; i++)        v.push_back(a[i]);     // Printing answer    cout << RecursiveFunction(v, 30)         << "\n";} // Driver Codeint main(){    int arr[] = { 3, 2, 1 };     int size = sizeof(arr) / sizeof(arr);     PrintMinimum(arr, size);     return 0;}

Java

 // Java program that find the minimum// possible maximumimport java.util.*; class GFG{ // Recursive function that find the// minimum value after exclusive-ORstatic int RecursiveFunction(ArrayList ref,                             int bit){         // Condition if ref size is zero or    // bit is negative then return 0    if (ref.size() == 0 || bit < 0)        return 0;     ArrayList curr_on = new ArrayList<>();    ArrayList curr_off = new ArrayList<>();     for(int i = 0; i < ref.size(); i++)    {                 // Condition if current bit is        // off then push current value        // in curr_off vector        if (((ref.get(i) >> bit) & 1) == 0)            curr_off.add(ref.get(i));         // Condition if current bit is on        // then push current value in        // curr_on vector        else            curr_on.add(ref.get(i));    }     // Condition if curr_off is empty    // then call recursive function    // on curr_on vector    if (curr_off.size() == 0)        return RecursiveFunction(curr_on, bit - 1);     // Condition if curr_on is empty    // then call recursive function    // on curr_off vector    if (curr_on.size() == 0)        return RecursiveFunction(curr_off, bit - 1);     // Return the minimum of curr_off and    // curr_on and add power of 2 of    // current bit    return Math.min(RecursiveFunction(curr_off,                                      bit - 1),                    RecursiveFunction(curr_on,                                      bit - 1)) +                                     (1 << bit);} // Function that print the minimum// value after exclusive-ORstatic void PrintMinimum(int a[], int n){    ArrayList v = new ArrayList<>();     // Pushing values in vector    for(int i = 0; i < n; i++)        v.add(a[i]);     // Printing answer    System.out.println(RecursiveFunction(v, 30));} // Driver Codepublic static void main(String[] args){    int arr[] = { 3, 2, 1 };    int size = arr.length;     PrintMinimum(arr, size);}} // This code is contributed by jrishabh99

Python3

 # Python3 program that find# the minimum  possible maximum # Recursive function that find the#  minimum value after exclusive-ORdef RecursiveFunction(ref, bit):     # Condition if ref size is zero or    # bit is negative then return 0    if(len(ref) == 0 or bit < 0):        return 0;    curr_on = []    curr_off = []     for i in range(len(ref)):               # Condition if current bit is         # off then push current value         # in curr_off vector        if(((ref[i] >> bit) & 1) == 0):            curr_off.append(ref[i])         # Condition if current bit is on         # then push current value in         # curr_on vector        else:            curr_on.append(ref[i])     # Condition if curr_off is empty    # then call recursive function    # on curr_on vector    if(len(curr_off) == 0):        return RecursiveFunction(curr_on,                                 bit - 1)     # Condition if curr_on is empty    # then call recursive function    # on curr_off vector    if(len(curr_on) == 0):        return RecursiveFunction(curr_off,                                 bit - 1)     # Return the minimum of curr_off and      # curr_on and add power of 2 of    # current bit    return(min(RecursiveFunction(curr_off,                                 bit - 1),               RecursiveFunction(curr_on,                                 bit - 1)) + (1 << bit)) # Function that print the minimum# value after exclusive-ORdef PrintMinimum(a, n):    v = []     # Pushing values in vector    for i in range(n):        v.append(a[i])     # Printing answer    print(RecursiveFunction(v, 30)) # Driver Codearr = [3, 2, 1]size = len(arr)PrintMinimum(arr, size) #This code is contributed by avanitrachhadiya2155

C#

 // C# program that find the minimum// possible maximumusing System;using System.Collections.Generic; class GFG{ // Recursive function that find the// minimum value after exclusive-ORstatic int RecursiveFunction(List re,                             int bit){         // Condition if ref size is zero or    // bit is negative then return 0    if (re.Count == 0 || bit < 0)        return 0;     List curr_on = new List();    List curr_off = new List();     for(int i = 0; i < re.Count; i++)    {                 // Condition if current bit is        // off then push current value        // in curr_off vector        if (((re[i] >> bit) & 1) == 0)            curr_off.Add(re[i]);         // Condition if current bit is on        // then push current value in        // curr_on vector        else            curr_on.Add(re[i]);    }     // Condition if curr_off is empty    // then call recursive function    // on curr_on vector    if (curr_off.Count == 0)        return RecursiveFunction(curr_on,                                 bit - 1);     // Condition if curr_on is empty    // then call recursive function    // on curr_off vector    if (curr_on.Count == 0)        return RecursiveFunction(curr_off,                                 bit - 1);     // Return the minimum of curr_off and    // curr_on and add power of 2 of    // current bit    return Math.Min(RecursiveFunction(curr_off,                                      bit - 1),                    RecursiveFunction(curr_on,                                      bit - 1)) +                                     (1 << bit);} // Function that print the minimum// value after exclusive-ORstatic void PrintMinimum(int []a, int n){    List v = new List();     // Pushing values in vector    for(int i = 0; i < n; i++)        v.Add(a[i]);     // Printing answer    Console.WriteLine(RecursiveFunction(v, 30));} // Driver Codepublic static void Main(String[] args){    int []arr = { 3, 2, 1 };    int size = arr.Length;     PrintMinimum(arr, size);}} // This code is contributed by Amit Katiyar

Javascript


Output:
2

My Personal Notes arrow_drop_up