LeetCode in Kotlin

2527. Find Xor-Beauty of Array

Medium

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:

Example 1:

Input: nums = [1,4]

Output: 5

Explanation:

The triplets and their corresponding effective values are listed below:

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

Example 2:

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:

Solution

class Solution {
    fun xorBeauty(nums: IntArray): Int {
        var i = 1
        while (i < nums.size) {
            nums[0] = nums[0] xor nums[i]
            i++
        }
        return nums[0]
    }
}