explanation with pen and paper for beginners || c++ || binary search
The major problem in this question to see what will be the value of  and(a,b) + or(a,b)\nLet\'s take a example and learn from it\n\nex =>\n  \n\n\n\nclass Solut
**The major problem in this question to see what will be the value of  and(a,b) + or(a,b)**\nLet\'s take a example and learn from it\n\nex =>\n  ![image](https://assets.leetcode.com/users/images/e07845e3-e0a9-4601-8b8c-d19ec3560966_1658652424.4231122.jpeg)\n\n\n```\nclass Solution {\npublic:\n    int setbits(int n) {\n        int cnt = 0;\n        while(n) {\n            cnt += n&1;\n            n = n >> 1;\n        }\n        return cnt;\n    }\n    \n    \n    \n    long long countExcellentPairs(vector<int>& nums, int k) {\n\t   // we are using set just to remove duplicates \n        unordered_set<int> set;\n        for(int& i : nums) {\n            set.insert(i);\n        }\n        nums.clear();\n        for(auto&it: set) nums.push_back(it);\n        int n = nums.size();\n\t\t// here i am not using any extra space for storing the setbits count for each value in the nums\n        for(int i = 0; i < n ;i++) {\n            nums[i] = setbits(nums[i]);\n        }\n\t\t// sorting the function because of lower_bound function\n\t\t// if     nums[i] + any found number in nums >= k than we can definately say that \n\t\t// all the number after this any found number in nums will also satisfy the condition\n\t\t// so apply lower_bound there\n        sort(nums.begin(), nums.end());          \n        long long ans = 0;\n        for(int i = 0; i < n; i++) {\n            ans += n-(lower_bound(nums.begin(), nums.end(), k-nums[i])-nums.begin());\n        }\n        return ans;\n    }\n};\n```\nif you have any doubts then ask me.\nUpvote if you lilke my post.\n\n```\nT.C. = O(nlogn)\nSC = O(n)    because of set \n```
[]