# Find a pair with the given difference

Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference is n. **Examples:**

Input: arr[] = {5, 20, 3, 2, 50, 80}, n = 78 Output: Pair Found: (2, 80) Input: arr[] = {90, 70, 20, 80, 50}, n = 45 Output: No Such Pair

Become a success story instead of just reading about them. Prepare for coding interviews at Amazon and other top product-based companies with our **Amazon Test Series**. Includes **topic-wise practice questions on all important DSA topics** along with **10 practice contests** of 2 hours each. Designed by industry experts that will surely help you practice and sharpen your programming skills. Wait no more, start your preparation today!

The simplest method is to run two loops, the outer loop picks the first element (smaller element) and the inner loop looks for the element picked by outer loop plus n. Time complexity of this method is O(n^2).

We can use sorting and Binary Search to improve time complexity to O(nLogn). The first step is to sort the array in ascending order. Once the array is sorted, traverse the array from left to right, and for each element arr[i], binary search for arr[i] + n in arr[i+1..n-1]. If the element is found, return the pair.

Both first and second steps take O(nLogn). So overall complexity is O(nLogn).

The second step of the above algorithm can be improved to O(n). The first step remain same. The idea for second step is take two index variables i and j, initialize them as 0 and 1 respectively. Now run a linear loop. If arr[j] – arr[i] is smaller than n, we need to look for greater arr[j], so increment j. If arr[j] – arr[i] is greater than n, we need to look for greater arr[i], so increment i. Thanks to Aashish Barnwal for suggesting this approach.

The following code is only for the second step of the algorithm, it assumes that the array is already sorted.

## C++

`// C++ program to find a pair with the given difference` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// The function assumes that the array is sorted` `bool` `findPair(` `int` `arr[], ` `int` `size, ` `int` `n)` `{` ` ` `// Initialize positions of two elements` ` ` `int` `i = 0;` ` ` `int` `j = 1;` ` ` `// Search for a pair` ` ` `while` `(i < size && j < size)` ` ` `{` ` ` `if` `(i != j && (arr[j] - arr[i] == n || arr[i] - arr[j] == n) )` ` ` `{` ` ` `cout << ` `"Pair Found: ("` `<< arr[i] <<` ` ` `", "` `<< arr[j] << ` `")"` `;` ` ` `return` `true` `;` ` ` `}` ` ` `else` `if` `(arr[j]-arr[i] < n)` ` ` `j++;` ` ` `else` ` ` `i++;` ` ` `}` ` ` `cout << ` `"No such pair"` `;` ` ` `return` `false` `;` `}` `// Driver program to test above function` `int` `main()` `{` ` ` `int` `arr[] = {1, 8, 30, 40, 100};` ` ` `int` `size = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);` ` ` `int` `n = -60;` ` ` `findPair(arr, size, n);` ` ` `return` `0;` `}` `// This is code is contributed by rathbhupendra` |

## C

`// C program to find a pair with the given difference` `#include <stdio.h>` `// The function assumes that the array is sorted` `bool` `findPair(` `int` `arr[], ` `int` `size, ` `int` `n)` `{` ` ` `// Initialize positions of two elements` ` ` `int` `i = 0; ` ` ` `int` `j = 1;` ` ` `// Search for a pair` ` ` `while` `(i<size && j<size)` ` ` `{` ` ` `if` `(i != j && (arr[j] - arr[i] == n || arr[i] - arr[j] == n))` ` ` `{` ` ` `printf` `(` `"Pair Found: (%d, %d)"` `, arr[i], arr[j]);` ` ` `return` `true` `;` ` ` `}` ` ` `else` `if` `(arr[j]-arr[i] < n)` ` ` `j++;` ` ` `else` ` ` `i++;` ` ` `}` ` ` `printf` `(` `"No such pair"` `);` ` ` `return` `false` `;` `}` `// Driver program to test above function` `int` `main()` `{` ` ` `int` `arr[] = {1, 8, 30, 40, 100};` ` ` `int` `size = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);` ` ` `int` `n = -60;` ` ` `findPair(arr, size, n);` ` ` `return` `0;` `}` |

## Java

`// Java program to find a pair with the given difference` `import` `java.io.*;` `class` `PairDifference` `{` ` ` `// The function assumes that the array is sorted` ` ` `static` `boolean` `findPair(` `int` `arr[],` `int` `n)` ` ` `{` ` ` `int` `size = arr.length;` ` ` `// Initialize positions of two elements` ` ` `int` `i = ` `0` `, j = ` `1` `;` ` ` `// Search for a pair` ` ` `while` `(i < size && j < size)` ` ` `{` ` ` `if` `(i != j && (arr[j] - arr[i] == n || arr[i] - arr[j] == n))` ` ` `{` ` ` `System.out.print(` `"Pair Found: "` `+` ` ` `"( "` `+arr[i]+` `", "` `+ arr[j]+` `" )"` `);` ` ` `return` `true` `;` ` ` `}` ` ` `else` `if` `(arr[j] - arr[i] < n)` ` ` `j++;` ` ` `else` ` ` `i++;` ` ` `}` ` ` `System.out.print(` `"No such pair"` `);` ` ` `return` `false` `;` ` ` `}` ` ` `// Driver program to test above function` ` ` `public` `static` `void` `main (String[] args)` ` ` `{` ` ` `int` `arr[] = {` `1` `, ` `8` `, ` `30` `, ` `40` `, ` `100` `};` ` ` `int` `n = -` `60` `;` ` ` `findPair(arr,n);` ` ` `}` `}` `/*This code is contributed by Devesh Agrawal*/` |

## Python

`# Python program to find a pair with the given difference` `# The function assumes that the array is sorted` `def` `findPair(arr,n):` ` ` `size ` `=` `len` `(arr)` ` ` `# Initialize positions of two elements` ` ` `i,j ` `=` `0` `,` `1` ` ` `# Search for a pair` ` ` `while` `i < size ` `and` `j < size:` ` ` `if` `i !` `=` `j ` `and` `arr[j]` `-` `arr[i] ` `=` `=` `n:` ` ` `print` `"Pair found ("` `,arr[i],` `","` `,arr[j],` `")"` ` ` `return` `True` ` ` `elif` `arr[j] ` `-` `arr[i] < n:` ` ` `j` `+` `=` `1` ` ` `else` `:` ` ` `i` `+` `=` `1` ` ` `print` `"No pair found"` ` ` `return` `False` `# Driver function to test above function` `arr ` `=` `[` `1` `, ` `8` `, ` `30` `, ` `40` `, ` `100` `]` `n ` `=` `60` `findPair(arr, n)` `# This code is contributed by Devesh Agrawal` |

## C#

`// C# program to find a pair with the given difference` `using` `System;` `class` `GFG {` ` ` ` ` `// The function assumes that the array is sorted` ` ` `static` `bool` `findPair(` `int` `[]arr, ` `int` `n)` ` ` `{` ` ` `int` `size = arr.Length;` ` ` `// Initialize positions of two elements` ` ` `int` `i = 0, j = 1;` ` ` `// Search for a pair` ` ` `while` `(i < size && j < size)` ` ` `{` ` ` `if` `(i != j && arr[j] - arr[i] == n)` ` ` `{` ` ` `Console.Write(` `"Pair Found: "` ` ` `+ ` `"( "` `+ arr[i] + ` `", "` `+ arr[j] +` `" )"` `);` ` ` ` ` `return` `true` `;` ` ` `}` ` ` `else` `if` `(arr[j] - arr[i] < n)` ` ` `j++;` ` ` `else` ` ` `i++;` ` ` `}` ` ` `Console.Write(` `"No such pair"` `);` ` ` ` ` `return` `false` `;` ` ` `}` ` ` `// Driver program to test above function` ` ` `public` `static` `void` `Main ()` ` ` `{` ` ` `int` `[]arr = {1, 8, 30, 40, 100};` ` ` `int` `n = 60;` ` ` ` ` `findPair(arr, n);` ` ` `}` `}` `// This code is contributed by Sam007.` |

## PHP

`<?php` `// PHP program to find a pair with` `// the given difference` `// The function assumes that the` `// array is sorted` `function` `findPair(&` `$arr` `, ` `$size` `, ` `$n` `)` `{` ` ` `// Initialize positions of` ` ` `// two elements` ` ` `$i` `= 0;` ` ` `$j` `= 1;` ` ` `// Search for a pair` ` ` `while` `(` `$i` `< ` `$size` `&& ` `$j` `< ` `$size` `)` ` ` `{` ` ` `if` `(` `$i` `!= ` `$j` `&& ` `$arr` `[` `$j` `] -` ` ` `$arr` `[` `$i` `] == ` `$n` `)` ` ` `{` ` ` `echo` `"Pair Found: "` `. ` `"("` `.` ` ` `$arr` `[` `$i` `] . ` `", "` `. ` `$arr` `[` `$j` `] . ` `")"` `;` ` ` `return` `true;` ` ` `}` ` ` `else` `if` `(` `$arr` `[` `$j` `] - ` `$arr` `[` `$i` `] < ` `$n` `)` ` ` `$j` `++;` ` ` `else` ` ` `$i` `++;` ` ` `}` ` ` `echo` `"No such pair"` `;` ` ` `return` `false;` `}` `// Driver Code` `$arr` `= ` `array` `(1, 8, 30, 40, 100);` `$size` `= sizeof(` `$arr` `);` `$n` `= 60;` `findPair(` `$arr` `, ` `$size` `, ` `$n` `);` `// This code is contributed` `// by ChitraNayal` `?>` |

## Javascript

`<script>` ` ` `// JavaScript program for the above approach` ` ` `// The function assumes that the array is sorted` ` ` `function` `findPair(arr, size, n) {` ` ` `// Initialize positions of two elements` ` ` `let i = 0;` ` ` `let j = 1;` ` ` `// Search for a pair` ` ` `while` `(i < size && j < size) {` ` ` `if` `(i != j && arr[j] - arr[i] == n) {` ` ` `document.write(` `"Pair Found: ("` `+ arr[i] + ` `", "` `+` ` ` `arr[j] + ` `")"` `);` ` ` `return` `true` `;` ` ` `}` ` ` `else` `if` `(arr[j] - arr[i] < n)` ` ` `j++;` ` ` `else` ` ` `i++;` ` ` `}` ` ` `document.write(` `"No such pair"` `);` ` ` `return` `false` `;` ` ` `}` ` ` `// Driver program to test above function` ` ` `let arr = [1, 8, 30, 40, 100];` ` ` `let size = arr.length;` ` ` `let n = 60;` ` ` `findPair(arr, size, n);` ` ` `// This code is contributed by Potta Lokesh` ` ` ` ` `</script>` |

**Output**

Pair Found: (100, 40)

Hashing can also be used to solve this problem. Create an empty hash table HT. Traverse the array, use array elements as hash keys and enter them in HT. Traverse the array again look for value n + arr[i] in HT.

Please write comments if you find any of the above codes/algorithms incorrect, or find other ways to solve the same problem.