Javascript Program to Find a pair with the given difference
Last Updated :
16 Sep, 2024
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
Native Approach:
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.
Example:
JavaScript
// 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) {
console.log("Pair Found: (" + arr[i] + ", " +
arr[j] + ")");
return true;
}
else if (arr[j] - arr[i] < n)
j++;
else
i++;
}
console.log("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
OutputPair Found: (40, 100)
Time Complexity: O(n*log(n)) [Sorting is still required as first step], Where n is number of element in given array.
Efficient Approach:
Hashing can also be used to solve this problem. We first create an empty hash table HT. We then traverse in the array and use array elements as hash keys and enter them in HT. While traversing, if the given difference is 0 then we will check if any element is occurring more than one time or not. If n!=0, then we again Traverse the array and look for the value n + arr[i] in HT(hash table) as the difference between n+arr[i] and arr[i] is n.
Below is the code for the above approach.
JavaScript
// JavaScript program for the above approach
// The function assumes that the array is sorted
function findPair(arr, size, n) {
// Initializing unordered map
let mp = new Map();
for (let i = 0; i < size; i++) {
// Using array elements as hash keys
// and entering them in Hash table.
if (!mp.has(arr[i]))
mp.set(arr[i], 1);
else
mp.set(arr[i], mp.get(arr[i]) + 1);
// For n==0, checking if one element
// is occurring more than 1 times
if (mp.has(arr[i])) {
if (n == 0 && mp.get(arr[i]) > 1)
return true;
}
}
// Check if the difference is
// still 0 with no element
// occurring more than 1 times
if (n == 0)
return false;
for (let i = 0; i < size; i++) {
// Checking if arr[i]+n is present or not
if (mp.has(arr[i] + n)) {
val = n + arr[i];
console.log("Pair Found: " + "(" + arr[i] + ", " + val + ")");
return true;
}
}
console.log("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 Pushpesh Raj.
OutputPair Found: (40, 100)
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Please refer complete article on Find a pair with the given difference for more details!
Similar Reads
Pair with the given difference Given an unsorted array and an integer x, the task is to find if there exists a pair of elements in the array whose absolute difference is x. Examples: Input: arr[] = [5, 20, 3, 2, 50, 80], x = 78Output: YesExplanation: The pair is {2, 80}.Input: arr[] = [90, 70, 20, 80, 50], x = 45Output: NoExplana
14 min read
Co-prime pair with given sum minimum difference Co-prime or mutually prime pairs are those pairs of numbers whose GCD is 1. Given a number n represent the number as the sum of a Co-prime pair (A, B) such that A - B is minimum. Examples : Input : 12 Output : 5 7 Possible co-prime pairs are (5, 7), (1, 11) but difference between 5 and 7 is minimum
5 min read
Find N integers with given difference between product and sum Given two integers N and D, Find a set of N integers such that the difference between their product and sum is equal to D. Examples: Input : N = 2, D = 1 Output : 2 3 Explanation: product = 2*3 = 6, Sum = 2 + 3 = 5. Hence, 6 - 5 = 1(D). Input : N = 3, D = 5. Output : 1 2 8 Explanation : Product = 1*
3 min read
Co-Prime with maximum difference in a given range Given two integers l and r, where l < r, the task is to find co-prime numbers x and y such that l <= x < y <= r and the difference between x and y is maximum.Note: Co-prime numbers are the numbers whose GCD (Greatest Common Divisor) is 1 i.e., A and B are Co-prime if and only if GCD ( A,
5 min read
Pairs with Difference less than K Given an array of n integers, We need to find all pairs with a difference less than k Examples : Input : a[] = {1, 10, 4, 2} K = 3 Output : 2 We can make only two pairs with difference less than 3. (1, 2) and (4, 2) Input : a[] = {1, 8, 7} K = 7 Output : 2 Pairs with difference less than 7 are (1, 7
13 min read