Using Binary Search
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n1. The input nums vecto
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. The input nums vector is sorted in ascending order, just like in the previous solution.\n\n2. The bs function is a binary search function that searches for the value x in the sorted array nums. It returns the index of the element if found, and -1 otherwise. This function will be used to find elements that have a difference of k with the current element at index i.\n\n3. A set ans of pairs is used to store the unique pairs that satisfy the condition of having a difference of k.\n\n4. The code then iterates through the sorted array using a for loop. For each element nums[i], it calls the bs function to find an element with a value of nums[i] + k. If such an element is found (i.e., bs(nums, i+1, nums[i]+k) returns a valid index), it means we have a valid pair, and the pair (nums[i], nums[i]+k) is inserted into the set ans.\n\n5. The function returns the size of the set ans, which represents the count of unique pairs with a difference of k.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(nlog(n))\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n\nint bs(vector<int>&nums,int start, int x){\n    int end = nums.size()-1;\n    while(start<=end){\n        int mid = (start+end)/2;\n        if(nums[mid]==x){\n            return mid;\n        }\n        else if(x>nums[mid]){\n            start = mid+1;\n        }\n        else{\n            end = mid-1;\n        }\n    }\n    return -1;\n}\n\n\n    int findPairs(vector<int>& nums, int k) {\n        sort(nums.begin(),nums.end());\n        set<pair<int,int>> ans;\n        for(int i=0;i<nums.size();i++){\n            if(bs(nums,i+1,nums[i]+k)!=-1){\n                ans.insert({nums[i],nums[i]+k});\n            }\n        }\n        return ans.size();\n    }\n};\n```
['Binary Search', 'C++']