17. Find Pair Given Difference
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class Solution {
public:
int findPair(int n, int x, vector<int> &arr)
{
sort(arr.begin(), arr.end());
int low=0, high=1;
while (low<n && high<n)
{
if (abs(arr[high]-arr[low])==x)
return 1;
else if (abs(arr[high]-arr[low])<x)
high++;
else low++;
}
return -1;
}
};Contribution and Support
Last updated