> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2024/05-2024-may-3/17-find-pair-given-difference.md).

# 17. Find Pair Given Difference

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/find-pair-given-difference1559/1)

## My Approach

* The input array arr is sorted to facilitate the two-pointer technique.
* Use two pointers low and high. Initialize low to 0 and high to 1.
* Use a while loop to traverse the array with the condition low < n and high < n.
* If the absolute difference between the elements at high and low is equal to x, return 1 (indicating a pair is found).
* If the absolute difference is less than x, increment the high pointer to increase the difference.
* If the absolute difference is greater than x, increment the low pointer to decrease the difference.
* If no such pair is found by the end of the loop, return -1.

## Time and Auxiliary Space Complexity

* **Time Complexity**: The time complexity of this approach is `O(NlogN)`, where N is the number of elements in the array.
* **Auxiliary Space Complexity**: The auxiliary space complexity is `O(1)`.

## Code (C++)

```cpp
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

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/05-2024-may-3/17-find-pair-given-difference.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
