C++ Solution | One-Pass | Beats 100% | Two-Pointer Approach
Sort the vector.\nHere 3 cases arise : \n Case - 1 : When nums[j] - nums[i] > k\n\tThe difference between element at j and element at i is greater than required
Sort the vector.\nHere 3 cases arise : \n* **Case - 1 : When nums[j] - nums[i] > k**\n\tThe difference between element at j and element at i is greater than required, so to reduce it increment i.\n* **Case - 2 : When nums[j] - nums[i] < k**\n\tThe difference between element at j and element at i is lesser than required, so to increase it increment j.\n* **Case - 3 : When nums[j] - nums[i] == k**\n\t Required pair found. Increment ans, and skip similar elements for both i and j.\n\n```\nclass Solution {\npublic:\n    int findPairs(vector<int>& nums, int k) {\n        \n        sort(nums.begin(),nums.end());\n        int ans = 0;\n        int n = nums.size();\n        int i = 0, j = 1;\n        while(i<n && j<n){\n            if(nums[j] - nums[i] == k && i!=j){\n                while(i+1 < n && nums[i+1] == nums[i])\n                    i++;\n                i++;\n                while(j+1<n && nums[j+1] == nums[j])\n                    j++;\n                j++;\n                ans++;\n            }\n            else if(nums[j] - nums[i] > k){\n                i++;\n            }\n            else{\n                j++;\n            }\n        }\n        return ans;\n    }\n};\n```
['C']