Taro Logo

Find Xor-Beauty of Array

Medium
Amazon logo
Amazon
2 views
Topics:
Bit Manipulation

You are given a 0-indexed integer array nums.

The effective value of three indices i, j, and k is defined as ((nums[i] | nums[j]) & nums[k]).

The xor-beauty of the array is the XORing of the effective values of all the possible triplets of indices (i, j, k) where 0 <= i, j, k < n.

Return the xor-beauty of nums.

Note that:

  • val1 | val2 is bitwise OR of val1 and val2.
  • val1 & val2 is bitwise AND of val1 and val2.

For example:

nums = [1,4]

The triplets and their corresponding effective values are listed below:

  • (0,0,0) with effective value ((1 | 1) & 1) = 1
  • (0,0,1) with effective value ((1 | 1) & 4) = 0
  • (0,1,0) with effective value ((1 | 4) & 1) = 1
  • (0,1,1) with effective value ((1 | 4) & 4) = 4
  • (1,0,0) with effective value ((4 | 1) & 1) = 1
  • (1,0,1) with effective value ((4 | 1) & 4) = 4
  • (1,1,0) with effective value ((4 | 4) & 1) = 0
  • (1,1,1) with effective value ((4 | 4) & 4) = 4

Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5.

nums = [15,45,20,2,34,35,5,44,32,30]

The xor-beauty of the given array is 34.

How do you efficiently calculate the xor-beauty of the input array?

Solution


Brute Force Solution

The most straightforward approach is to iterate through all possible triplets (i, j, k) and calculate the effective value for each triplet. Then, XOR all these effective values to get the xor-beauty.

def xor_beauty_brute_force(nums):
    n = len(nums)
    xor_result = 0
    for i in range(n):
        for j in range(n):
            for k in range(n):
                effective_value = ((nums[i] | nums[j]) & nums[k])
                xor_result ^= effective_value
    return xor_result

Time Complexity: O(n^3), due to the three nested loops.

Space Complexity: O(1), as we are using only a constant amount of extra space.

Optimal Solution

We can simplify the expression ((nums[i] | nums[j]) & nums[k]). Notice that the xor-beauty is the XOR of all such triplets. Due to the distributive property of bitwise operations, we can reduce the time complexity.

The xor-beauty can be expressed as the XOR of all elements in the array ANDed together. This is equivalent to XORing all elements of the array. The formula is:

xor_beauty = nums[0] ^ nums[1] ^ ... ^ nums[n-1]

def xor_beauty_optimal(nums):
    xor_result = 0
    for num in nums:
        xor_result ^= num
    return xor_result

Time Complexity: O(n), as we iterate through the array once.

Space Complexity: O(1), as we are using only a constant amount of extra space.

Edge Cases:

  • Empty Array: The problem statement specifies that 1 <= nums.length <= 10^5, so we don't need to handle the case where the array is empty.
  • Single Element Array: If the array contains only one element, the xor-beauty will be the element itself.
  • Large Numbers: The numbers can be up to 10^9, but bitwise operations will handle these sizes correctly within the integer limits.