Find Xor-Beauty of Array — LeetCode 2527 Python Solution

MediumBit ManipulationArrayMath
Problem
#2527
Reading time
2 min

The problem

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]).

Example

Input
nums = [1,4]
Output
5
Explanation
The triplets and their corresponding effective values are listed below:

Python solution

Python
class Solution:
    def xorBeauty(self, nums: List[int]) -> int:
        return reduce(xor, nums)

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1), where n is the length of the array auxiliary

Pattern: Bit Manipulation

Use XOR, masks and the low-bit trick to replace whole data structures with an integer. LeetCode 2527. Find Xor-Beauty of Array is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Bit Manipulation.

The bit manipulation guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2527. Find Xor-Beauty of Array?
LeetCode 2527. Find Xor-Beauty of Array is rated Medium on LeetCode.
What is the time complexity of LeetCode 2527. Find Xor-Beauty of Array?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2527. Find Xor-Beauty of Array?
The Python solution on this page uses O(1), where n is the length of the array auxiliary space.
What topics does LeetCode 2527. Find Xor-Beauty of Array cover?
LeetCode 2527. Find Xor-Beauty of Array is tagged Bit Manipulation, Array and Math on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview