MinAvgTwoSlice::solution()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 15
nc 7
nop 1
1
<?php
2
3
namespace Lesson03;
4
5
class MinAvgTwoSlice
6
{
7
    public function solution($A)
8
    {
9
        $position = 0;
10
        $count = count($A);
11
        $minAvg = ($A[0] + $A[1]) / 2;
12
13
        for ($p = 0; $p < $count - 1; $p++) {
14
            $avg = ($A[$p] + $A[$p + 1]) / 2;
15
            if ($avg < $minAvg) {
16
                $minAvg = $avg;
17
                $position = $p;
18
            }
19
20
            if (isset($A[$p + 2])) {
21
                $avg = ($A[$p] + $A[$p + 1] + $A[$p + 2]) / 3;
22
                if ($avg < $minAvg) {
23
                    $minAvg = $avg;
24
                    $position = $p;
25
                }
26
            }
27
        }
28
29
        return $position;
30
    }
31
}
32