LeetCode 2527: Find Xor-Beauty of Array
Problem Description
Explanation:
-
Algorithmic Idea:
- Iterate over all possible triplets of indices (i, j, k) in the array.
- Calculate the effective value of each triplet as ((nums[i] | nums[j]) & nums[k]).
- XOR all the effective values to get the xor-beauty of the array.
-
Step-by-Step Iterations:
- For each triplet (i, j, k), calculate the effective value and XOR them to get the final result.
-
Time Complexity: O(n^3) where n is the length of the input array.
-
Space Complexity: O(1)
:
Solutions
class Solution {
public int xorBeauty(int[] nums) {
int xorBeauty = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
xorBeauty ^= ((nums[i] | nums[j]) & nums[k]);
}
}
}
return xorBeauty;
}
}
Loading editor...