Find the Minimum Distance Between Two Numbers in PHP
Last Updated :
13 Feb, 2024
Given two numbers A and B, and an array arr containing multiple occurrences of these numbers, the task is to find the minimum distance between the given numbers X and Y. It is guaranteed that the array will contain at least one occurrence of both A and B.
Examples:
Input:
A[] = {1,2,3,2}
x = 1, y = 2
Output: 1
Explanation: x = 1 and y = 2. There are two distances between x and y, which are 1 and 3 out of which the least is 1.
Input:
A[] = {86,39,90,67,84,66,62}
x = 42, y = 12
Output: -1
Explanation: x = 42 and y = 12. We return -1 as x and y don't exist in the array.
Using Nested Loop
The objective is to determine the distance between two specified numbers by employing nested loops. The outer loop selects the first element (x), while the inner loop traverses the array to find the second element (y), calculating the minimum distance between them.
Approach:
- Initialize a variable m with the maximum integer value (INT_MAX).
- Use nested loops: the outer loop iterates from the start to the end (counter i), and the inner loop iterates from i+1 to the end (counter j).
- If the ith element equals x and the jth element equals y, or vice versa, update m as m = min(m, j – i).
- Print the value of m as the minimum distance.
Example: Implementation to find minimum distance between two number in php.
PHP
<?php
function minDist( $arr , $n , $x , $y )
{
$i ;
$j ;
$min_dist = PHP_INT_MAX;
for ( $i = 0; $i < $n ; $i ++) {
for ( $j = $i + 1; $j < $n ; $j ++) {
if (
( $x == $arr [ $i ] and $y == $arr [ $j ] or
$y == $arr [ $i ] and $x == $arr [ $j ]) and
$min_dist > abs ( $i - $j )
) {
$min_dist = abs ( $i - $j );
}
}
}
if ( $min_dist > $n ) {
return -1;
}
return $min_dist ;
}
$arr = [3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3];
$n = count ( $arr );
$x = 0;
$y = 6;
echo "Minimum distance between " , $x , " and " , $y , " is " ;
echo minDist( $arr , $n , $x , $y );
?>
|
Output
Minimum distance between 3 and 6 is 4
Time Complexity: O(n^2)
Space Complexity: O(1)
Using Two-Pointer Approach
The approach used in the provided findMinDistance method is commonly known as the “Optimized Two-Pointer Approach.” This approach efficiently utilizes two pointers ($lastX and $lastY) to keep track of the last occurrences of the two target numbers
($x and $y).
Approach
- Start by initializing variables
$lastX
and $lastY
to track the last occurrences of numbers $x
and $y
. Set $minDist
to PHP_INT_MAX initially.
- Iterate through the array, updating the last occurrences of
$x
and $y
. If both last occurrences are recorded, update $minDist
with the minimum distance between them.
- Use the
abs
function to calculate the absolute difference between the last occurrences of $x
and $y
. Update $minDist
if a smaller distance is found.
- After the loop, check if
$minDist
remains PHP_INT_MAX. If so, return -1 as either $x
or $y
is not present in the array. Otherwise, return the calculated minimum distance.
Example: Implementation to find minimum distance between two number in php.
PHP
<?php
function findMinDistance( $arr , $n , $x , $y )
{
$lastX = -1;
$lastY = -1;
$minDist = PHP_INT_MAX;
for ( $i = 0; $i < $n ; $i ++) {
if ( $arr [ $i ] == $x ) {
$lastX = $i ;
if ( $lastY != -1) {
$minDist = min( $minDist , abs ( $lastX - $lastY ));
}
}
if ( $arr [ $i ] == $y ) {
$lastY = $i ;
if ( $lastX != -1) {
$minDist = min( $minDist , abs ( $lastY - $lastX ));
}
}
}
if ( $minDist == PHP_INT_MAX) {
return -1;
}
return $minDist ;
}
$arr = [1, 2, 3, 1, 6, 7, 4, 1, 9];
$n = count ( $arr );
$x = 3;
$y = 4;
echo "Minimum distance between " , $x , " and " , $y , " is " ;
echo findMinDistance( $arr , $n , $x , $y );
?>
|
Output:
Minimum distance between 3 and 4 is 4
Time complexity: O(n)
Space complexity: O(1)
Share your thoughts in the comments
Please Login to comment...