K diff Pairs | C++ 6 liner solution with explanation | O(nlogn) time | O(1) space
 Upvote this post, if you liked it. Happy Coding :)\n\nApproach : \n1) Sort the nums array.\n2) Loop over the nums array using iterator\n\ta) maintain the previ
<b> Upvote this post, if you liked it. Happy Coding :)</b>\n\nApproach : \n1) Sort the nums array.\n2) Loop over the nums array using iterator\n\ta) maintain the previous value so that we can jump over duplicate values, to make sure that only unique pairs will be counted.\n\tb) perform binary search on elements from the next element (current\'s next element) to last element,  to find the value\n\t\twhich can satisfy the pair cond.\n3) return count\n```\nclass Solution {\npublic:\n    int findPairs(vector<int>& nums, int k) {\n        int prev_val, count = 0;\n        sort(nums.begin(), nums.end());\n        for(auto it = nums.begin(); it != nums.end(); it++) {\n            if(it != nums.begin() and *it == prev_val) continue;\n            if(binary_search(it+1, nums.end(), *it + k)) count++;\n            prev_val = *it;\n        }\n        return count;\n    }\n};\n```\n\n**I Hope you understood the solution, if you have any doubts regarding the solution or any suggestions to improve the solution,** then feel free to comment down below.
[]