forked from ActiveWebsite/codetest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EquilibriumIndex.php
57 lines (45 loc) · 1.46 KB
/
EquilibriumIndex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/*
This file contains a function that takes in an array and will return
any equilibrium indices of the array if any exist.
Author: Evan Degenhart
*/
// From phpunit test case for debugging
//$arr = array(-7, 1, 5, 2, -4, 3, 0);
function getEquilibriums($arr) {
$output = array(); // Array that holds equilibrium indices (results)
// First step is to check the array to see how long it is
$length = count($arr);
// Display error if no input given
if ($length == 0) {
echo "No input received!";
}
else {
// Initialize right and left sums to test for equilibriums
$right_sum = array_sum($arr); // Initialize right side to the sum of all elements of array
$left_sum = 0; // Initialize to zero because the sum of 0 elements is 0
// Loop through each index in the array starting from left
for ($i = 0; $i < $length; $i++) {
// Subtract current index value from right sum
$right_sum = $right_sum - $arr[$i];
if ($left_sum == $right_sum) { // Equilibrium found
$output[] = $i; // Store equilibrium index in output array
}
// Update left sum to check next index
$left_sum = $left_sum + $arr[$i];
}
}
// Display message if no equilibriums found
if (count($output) == 0) {
echo "No equilibriums found!";
}
return $output;
}
// For debugging
/*
$results = getEquilibriums($arr);
$results_length = array_sum($results);
echo "# results: <br/>";
foreach ($results as $r) echo "$r, ";
*/
?>