1
var countPairs = function (nums, low, high) {4
for (i = 14; i >= 0; i--) {5
var bit = (num >> i) & 1;6
if (!node.children[bit]) {7
node.children[bit] = new TrieNode();9
node.children[bit].cnt += 1;10
node = node.children[bit];13
function countSmallerNums(num, limit) {15
// count pairs with num strictly smaller than limit17
for (i = 14; i >= 0 && node; i--) {18
var lbit = (limit >> i) & 1;19
var nbit = (num >> i) & 1;21
// we can decrease on 1 bit22
if (node.children[nbit]) {23
cnt += node.children[nbit].cnt;25
// go to another way to find next bit count26
node = node.children[1 - nbit];28
// cannot increase the lbit === 0,29
// find the same Nbit to XOR to be '0'30
node = node.children[nbit];36
var root = new TrieNode();38
for (var num of nums) {39
res += countSmallerNums(num, high + 1) - countSmallerNums(num, low);48
this.children = new Array(2).fill(null);