3514. Number of Unique XOR Triplets II LeetCode Solution

In this guide, you will get 3514. Number of Unique XOR Triplets II LeetCode Solution with the best time and space complexity. The solution to Number of Unique XOR Triplets II 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. Number of Unique XOR Triplets II solution in C++
  4. Number of Unique XOR Triplets II solution in Java
  5. Number of Unique XOR Triplets II solution in Python
  6. Additional Resources
3514. Number of Unique XOR Triplets II LeetCode Solution image

Problem Statement of Number of Unique XOR Triplets II

You are given an integer array nums.
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).

Example 1:

Input: nums = [1,3]
Output: 2
Explanation:
The possible XOR triplet values are:

(0, 0, 0) → 1 XOR 1 XOR 1 = 1
(0, 0, 1) → 1 XOR 1 XOR 3 = 3
(0, 1, 1) → 1 XOR 3 XOR 3 = 1
(1, 1, 1) → 3 XOR 3 XOR 3 = 3

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

Example 2:

Input: nums = [6,7,8,9]
Output: 4
Explanation:
The possible XOR triplet values are {6, 7, 8, 9}. Thus, the output is 4.

Constraints:

1 <= nums.length <= 1500
1 <= nums[i] <= 1500

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(n^2)

3514. Number of Unique XOR Triplets II LeetCode Solution in C++

class Solution {
  public int uniqueXorTriplets(int[] nums) {
    final int n = nums.length;
    if (n == 1)
      return 1;

    Set<Integer> pairs = new HashSet<>();
    BitSet triplets = new BitSet();

    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j < n; ++j)
        pairs.add(nums[i] ^ nums[j]);

    for (final int pair : pairs)
      for (final int num : nums)
        triplets.set(pair ^ num);

    return triplets.cardinality();
  }
}
/* code provided by PROGIEZ */

3514. Number of Unique XOR Triplets II LeetCode Solution in Java

N/A
// code provided by PROGIEZ

3514. Number of Unique XOR Triplets II LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

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