<?php
// arrB to not get changes reflected once we sort
$arrB = $array = [4, 1, 5, 52, 75, 52, 24];
function findMinDiff($arr)
{
$n = count($arr);
// Initialize difference
// as infinite
$diff = PHP_INT_MAX;
// Find the min diff by comparing
// difference of all possible
// pairs in given array
$two_values = [];
for ($i = 0; $i < $n - 1; $i++) {
for ($j = $i + 1; $j < $n; $j++) {
if (abs($arr[$i] - $arr[$j]) < $diff) {
$diff = abs($arr[$i] - $arr[$j]);
$two_values['values'] = [$arr[$i], $arr[$j]];
$two_values['keys'] = [$i, $j];
$two_values['diff'] = $diff;
}
}
}
// Return min diff
return $two_values;
}
print_r(findMinDiff($array));
- Output for 7.1.25 - 7.1.30, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
- Array
(
[values] => Array
(
[0] => 52
[1] => 52
)
[keys] => Array
(
[0] => 3
[1] => 5
)
[diff] => 0
)
preferences:
46.47 ms | 407 KiB | 5 Q