713153. Minimum X (xor) A

Medium

Minimum X (xor) A -- 170645

Medium

Summary

Given an array A of size N, find the minimum value that can be obtained by performing bitwise XOR operation on all elements in the array. The problem requires us to use Bit Magic and Data Structures concepts to solve it efficiently.

Detailed Explanation

The given problem is a classic example of using bit manipulation to solve a complex problem. To solve this problem, we will first calculate the XOR of all elements in the array. Then, we can find the minimum value that can be obtained by performing bitwise XOR operation on all elements in the array.

To do this, we will use two variables: xor_sum and min_xor. xor_sum will store the sum of all bits in the array (i.e., the sum of 1's and 0's). min_xor will store the minimum value that can be obtained by performing bitwise XOR operation on all elements in the array.

We initialize xor_sum as 0, since there are no bits yet. Then, we iterate over each element in the array, updating xor_sum with the sum of its bits and the previous xor_sum. After iterating over all elements, xor_sum will store the sum of all bits in the array.

To find min_xor, we initialize it as the last bit of xor_sum. This is because the minimum value that can be obtained by performing bitwise XOR operation on all elements in the array must have the same number of 1's and 0's as the sum of all bits in the array, since each element has either 0 or 1 bits.

Finally, we return min_xor as the result. This solution takes O(N) time complexity, where N is the size of the array, since it iterates over each element once.

Time Complexity: O(N)

Space Complexity: O(1)

Optimized Solutions

Java

View on GeeksforGeeks
public int findMinXor(int[] A) {
    int xor_sum = 0;
    for (int i : A) {
        xor_sum ^= i;
    }
    int min_xor = xor_sum & -xor_sum; // Find the rightmost set bit in xor_sum
    return min_xor;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.