C++ Two-Pointer - T(NlogN) and HashMap - T(N)
TWO-POINTER APPROACH T(NLogN)\n\n1. The approach that I used here is similar to two-sum problem.\n2. In two-sum we select two numbers that sums-up to k, whereas
**TWO-POINTER APPROACH T(NLogN)**\n\n1. The approach that I used here is similar to two-sum problem.\n2. In two-sum we select two numbers that sums-up to k, whereas here we have to select two numbers with difference k.\n3. I have used two pointers i & j, in two-sum the we start i from 0 and j from n-1, but here we\'ll start i from 0 and j from 1 because at this point the difference will be minimum b/w nums[i] and nums[j].\n\n**CODE**\n```\nint findPairs(vector<int>& nums, int k) {\n        int n = nums.size();\n        sort(nums.begin(), nums.end());\n        int i=0, j=1, count=0;\n        while(j<n){\n            if(i==j) j++;\n            if(j>n-1) break;\n            if(nums[i]+k == nums[j]){\n                count++;\n                while(j<n and nums[i]+k == nums[j]) j++;\n            }else if(nums[i]+k < nums[j]){\n                i++;\n            }else{\n                j++;\n            }\n        }\n        return count;\n    }\n```\n\n**HASHMAP APPROACH T(N)**\n\n**CODE**\n```\nint findPairs(vector<int>& nums, int k) {\n        unordered_map<int, int> m;\n        int c=0;\n        for(auto i: nums) m[i]++;\n        \n        for(int i=0; i<nums.size(); i++){\n            if(k!=0){\n                if(m[k+nums[i]]>0){\n                    c++;\n                    m[k+nums[i]]=0;\n                }\n            }else{\n                if(m[nums[i]]>1) c++;\n                m[nums[i]]=0;\n            }\n        }\n        return c;\n    }\n```
['Two Pointers', 'C']