PHP Program to Find a Pair with Given Difference
Last Updated :
22 Jul, 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
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 remains the 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.
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);
?>
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.
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 refer complete article on Find a pair with the given difference for more details!
Similar Reads
Javascript Program to 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 = 78Output: Pair Found: (2, 80)Input: arr[] = {90, 70, 20, 80, 50}, n = 45Output: No Such PairNative Approach:The simplest method is t
4 min read
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
How to get Time Difference in Minutes in PHP ? In this article, we will learn how to get time difference in minutes using PHP. We will be using the built-in function date_diff() to get the time difference in minutes. For this, we will be needed a start date and end date to calculate their time difference in minutes using the date_diff() function
3 min read
Program to find the number of days between two dates in PHP In this article, we will see how to get the date difference in the number of days in PHP, along with will also understand the various ways to get the total count of difference in 2 dates & see their implementation through the examples. We have given two dates & our task is to find the number
3 min read
Count pairs with absolute difference equal to k Given an array arr[] and a positive integer k, the task is to count all pairs (i, j) such that i < j and absolute value of (arr[i] - arr[j]) is equal to k. Examples: Input: arr[] = [1, 4, 1, 4, 5], k = 3Output: 4Explanation: There are 4 pairs with absolute difference 3, the pairs are [1, 4], [1,
15+ min read