Given an integer 'k' and an sorted array A (can consist of both +ve/-ve nos), output 2 integers from A such that a-b=k.
PS:
nlogn solution would be to check for the occurence of k-a[i] (using binary search) when you encounter a[i]. methods like hash consume space.
Is an O(n) solution with O(1) extraspace possible?
void fun(vector<int> &a, int key)
{
int n = a.size();
if (n < 2)
{
return;
}
key = abs(key);
int p = 0;
int q = 1;
while (q < n)
{
if (a[q] - a[p] == key)
{
cout << p << " " << q << endl;
return;
}
else if (a[q] - a[p] > key)
{
p++;
}
else
{
q++;
}
}
}