MinimumAbsoluteDifference   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 20
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A minimumAbsDifference() 0 18 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class MinimumAbsoluteDifference
8
{
9
    public static function minimumAbsDifference(array $arr): array
10
    {
11
        if (empty($arr)) {
12
            return [];
13
        }
14
        sort($arr);
15
        [$n, $min] = [count($arr), PHP_INT_MAX];
16
        for ($i = 0; $i < $n - 1; $i++) {
17
            $val = $arr[$i + 1] - $arr[$i];
18
            if ($val < $min) {
19
                $min = $val;
20
                $ans[] = [$arr[$i], $arr[$i + 1]];
21
            } elseif ($val === $min) {
22
                $ans[] = [$arr[$i], $arr[$i + 1]];
23
            }
24
        }
25
26
        return $ans;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ans does not seem to be defined for all execution paths leading up to this point.
Loading history...
27
    }
28
}
29