EquiLeader::solution()   C
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
rs 5.3846
cc 8
eloc 23
nc 12
nop 1
1
<?php
2
3
namespace Lesson06;
4
5
class EquiLeader
6
{
7
    public function solution($A)
8
    {
9
        $N = count($A);
10
        if ($N > 1) {
11
            $count = array_count_values($A);
12
            arsort($count);
13
            $max = 0;
14
            $maxValue = 0;
15
            foreach ($count as $key => $value) {
16
                if ($value > (int) ($N / 2)) {
17
                    $max = $key;
18
                    $maxValue = $value;
19
                    break;
20
                } else {
21
                    return 0;
22
                }
23
            }
24
            $equiCounter = 0;
25
            $leftLeader = 0;
26
            foreach ($A as $key => $value) {
27
                if ($value == $max) {
28
                    $leftLeader++;
29
                }
30
                if ($leftLeader > ($key + 1) / 2 && ($maxValue - $leftLeader) > ($N - $key - 1) / 2) {
31
                    $equiCounter++;
32
                }
33
            }
34
35
            return $equiCounter;
36
        }
37
38
        return 0;
39
    }
40
}
41