LeetCode in Kotlin

3513. Number of Unique XOR Triplets I

Medium

You are given an integer array nums of length n, where nums is a permutation of the numbers in the range [1, n].

A XOR triplet is defined as the XOR of three elements nums[i] XOR nums[j] XOR nums[k] where i <= j <= k.

Return the number of unique XOR triplet values from all possible triplets (i, j, k).

A permutation is a rearrangement of all the elements of a set.

Example 1:

Input: nums = [1,2]

Output: 2

Explanation:

The possible XOR triplet values are:

The unique XOR values are {1, 2}, so the output is 2.

Example 2:

Input: nums = [3,1,2]

Output: 4

Explanation:

The possible XOR triplet values include:

The unique XOR values are {0, 1, 2, 3}, so the output is 4.

Constraints:

Solution

class Solution {
    fun uniqueXorTriplets(nums: IntArray): Int {
        val n = nums.size
        return if (n < 3) n else Integer.highestOneBit(n) shl 1
    }
}