<p>Given an integer array nums, return <em>the number of <strong>AND triples</strong></em>.</p>

<p>An <strong>AND triple</strong> is a triple of indices <code>(i, j, k)</code> such that:</p>

<ul>
	<li><code>0 &lt;= i &lt; nums.length</code></li>
	<li><code>0 &lt;= j &lt; nums.length</code></li>
	<li><code>0 &lt;= k &lt; nums.length</code></li>
	<li><code>nums[i] &amp; nums[j] &amp; nums[k] == 0</code>, where <code>&amp;</code> represents the bitwise-AND operator.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,1,3]
<strong>Output:</strong> 12
<strong>Explanation:</strong> We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 &amp; 2 &amp; 1
(i=0, j=1, k=0) : 2 &amp; 1 &amp; 2
(i=0, j=1, k=1) : 2 &amp; 1 &amp; 1
(i=0, j=1, k=2) : 2 &amp; 1 &amp; 3
(i=0, j=2, k=1) : 2 &amp; 3 &amp; 1
(i=1, j=0, k=0) : 1 &amp; 2 &amp; 2
(i=1, j=0, k=1) : 1 &amp; 2 &amp; 1
(i=1, j=0, k=2) : 1 &amp; 2 &amp; 3
(i=1, j=1, k=0) : 1 &amp; 1 &amp; 2
(i=1, j=2, k=0) : 1 &amp; 3 &amp; 2
(i=2, j=0, k=1) : 3 &amp; 2 &amp; 1
(i=2, j=1, k=0) : 3 &amp; 1 &amp; 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> 27
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums[i] &lt; 2<sup>16</sup></code></li>
</ul>

#include <bits/stdc++.h>
using namespace std;

class TrieNode {
public:
    TrieNode* children[2];
    int count;

    TrieNode() {
        children[0] = nullptr;
        children[1] = nullptr;
        count=0;
    }
};

class Trie {
    TrieNode* root;
public:
    Trie() {
        root = new TrieNode();
    }

    // Insert a number into the Trie
    void insert(int n) {
        TrieNode* node = root;
        for (int i = 17; i >= 0; i--) {
            int bit = (n >> i) & 1;

            if (!node->children[bit]) {
                node->children[bit] = new TrieNode();
            }
            node = node->children[bit];
        }
        node->count++;
    }

    // Find the count of numbers with specific XOR properties
    int find(int i, int n, TrieNode* root) {
        int ans = 0;
        if (root == nullptr) {
            return 0;
        }
        if (i == -1) {
            return root->count;
        }
        TrieNode* node = root;
        int bit = (n >> i) & 1;
        if (bit == 1) {
            ans += find(i - 1, n, root->children[0]);
        } else {
            ans += find(i - 1, n, root->children[0]) + find(i - 1, n, root->children[1]);
        }
        return ans;
    }

    // Helper function to call the recursive find function
    int findCount(int n) {
        return find(17, n, root);
    }
};

class Solution {
public:
    int countTriplets(vector<int>& nums) {
        int n = nums.size();
        Trie myTrie;
        // vector<vector<int>> arr(n,vector<int> (n));
        int sol = 0;
        // Insert all numbers into the Trie

        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                int x=nums[i]&nums[j];
                myTrie.insert(x);

            }
        }

        
        for (int num : nums) {
            sol += myTrie.findCount(num);
        }

        return sol;
    }
};

20800