MaxProductOfThree::solution()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 14
nc 5
nop 1
1
<?php
2
3
namespace Lesson04;
4
5
class MaxProductOfThree
6
{
7
    public function solution($A)
8
    {
9
        sort($A);
10
        $allPositives = $this->getPositives($A);
11
        list($allNegatives, $twoNegatives) = $this->getNegatives($A, $allPositives);
12
13
        $maxProduct = null;
14
        $values = array_merge($allPositives, $allNegatives, $twoNegatives);
15
        $count = count($values);
16
17
        for ($i = 0; $i < $count - 2; $i++) {
18
            for ($j = $i + 1; $j < $count - 1; $j++) {
19
                for ($k = $j + 1; $k < $count; $k++) {
20
                    $product = $values[$i] * $values[$j] * $values[$k];
21
                    if (empty($maxProduct) || $product > $maxProduct) {
22
                        $maxProduct = $product;
23
                    }
24
                }
25
            }
26
        }
27
28
        return $maxProduct;
29
    }
30
31
    /**
32
     * @param $A
33
     *
34
     * @return array
35
     *
36
     * @internal param $arrayCount
37
     * @internal param $allPositives
38
     */
39
    private function getPositives($A)
40
    {
41
        $arrayCount = count($A);
42
        $allPositives = [];
43 View Code Duplication
        for ($i = $arrayCount - 3; $i < $arrayCount; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
            if ($A[$i] >= 0) {
45
                $allPositives[] = $A[$i];
46
            }
47
        }
48
49
        return $allPositives;
50
    }
51
52
    /**
53
     * @param $A
54
     * @param $allPositives
55
     *
56
     * @return array
57
     *
58
     * @internal param $arrayCount
59
     * @internal param $allNegatives
60
     * @internal param $twoNegatives
61
     */
62
    private function getNegatives($A, $allPositives)
63
    {
64
        $arrayCount = count($A);
65
        $allNegatives = [];
66
        $twoNegatives = [];
67
        if (count($allPositives) === 0) {
68 View Code Duplication
            for ($i = $arrayCount - 3; $i < $arrayCount; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
                if ($A[$i] < 0) {
70
                    $allNegatives[] = $A[$i];
71
                }
72
            }
73
74
            return [$allNegatives, $twoNegatives];
75
        } else {
76
            for ($i = 0; $i < 2; $i++) {
77
                if ($A[$i] < 0) {
78
                    $twoNegatives[] = $A[$i];
79
                }
80
            }
81
82
            return [$allNegatives, $twoNegatives];
83
        }
84
    }
85
}
86