2527. Find Xor-Beauty of Array LeetCode Solution

In this guide, you will get 2527. Find Xor-Beauty of Array LeetCode Solution with the best time and space complexity. The solution to Find Xor-Beauty of Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Xor-Beauty of Array solution in C++
  4. Find Xor-Beauty of Array solution in Java
  5. Find Xor-Beauty of Array solution in Python
  6. Additional Resources
2527. Find Xor-Beauty of Array LeetCode Solution image

Problem Statement of Find Xor-Beauty of Array

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.

Example 1:

Input: nums = [1,4]
Output: 5
Explanation:
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.
Example 2:

See also  3412. Find Mirror Score of a String LeetCode Solution

Input: nums = [15,45,20,2,34,35,5,44,32,30]
Output: 34
Explanation: The xor-beauty of the given array is 34.

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2527. Find Xor-Beauty of Array LeetCode Solution in C++

class Solution {
 public:
  int xorBeauty(vector<int>& nums) {
    return accumulate(nums.begin(), nums.end(), 0, bit_xor<>());
  }
};
/* code provided by PROGIEZ */

2527. Find Xor-Beauty of Array LeetCode Solution in Java

class Solution {
  public int xorBeauty(int[] nums) {
    return Arrays.stream(nums).reduce((a, b) -> a ^ b).getAsInt();
  }
}
// code provided by PROGIEZ

2527. Find Xor-Beauty of Array LeetCode Solution in Python

class Solution:
  def xorBeauty(self, nums: list[int]) -> int:
    return functools.reduce(operator.xor, nums)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.