EquiLeader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 36
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C solution() 0 33 8
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