Posted on

Description

Submission

class Solution {
    int bitCounts[33];
    int visited[33];
public:
    long long countExcellentPairs(vector<int>& nums, int k) {
        unordered_set<int> firstTime;

        for(int x: nums) {
            if(firstTime.find(x) != firstTime.end()) continue;
            firstTime.insert(x);
            bitCounts[__builtin_popcount(x)]++;
        }

        long long ret = 0;
        
        for(int i = 1; i <= 32; ++i) {
            for(int j = 1; j <= 32; ++j) {
                
                if(i == j) {
                    if(visited[i]) continue;
                    visited[i] = 1;
                }

                if(i + j >= k) ret += bitCounts[i] * bitCounts[j];
            }
        }

        return ret;
    }
};

// It's actually sum all the 1's count inside the two numbers.
// 10011101
// 10101101
// 11110111

Leave a Reply

Your email address will not be published. Required fields are marked *