Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
*debug*
46 changes: 39 additions & 7 deletions EquilibriumIndex.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
<?php
/*
* Find all equilibrium indices if any. Try to avoid nested loops if possible.
* It could be done with nested loops by adding up each left and right sum at
* each step through the array, but that yields a big-O of O(N^2). We can do
* better than that. Unnested, we stay at 2n or O(N).
*
* @param array $arr
* @return array Array of equilibrium indices
*/
function getEquilibriums(array $arr)
{
$output = [];
$rightSum = 0;
$leftSum = 0;

# Put a valid docblock here!
function getEquilibriums($arr) {
$output = array();
# Logic goes here!
return $output;
}

// count them all up.
foreach ($arr as $val) {
$rightSum += $val;
}

// now move them over and compare.
foreach ($arr as $i => $val) {
// moving the pile here.
$rightSum -= $val;

// now compare the piles. Note we don't add to the left sum yet b/c we
// are standing on our index. Think of it as our viewing point. While
// we're standing on it, we can only survey those values on either side
// of us.
if ($rightSum == $leftSum) {
$output[] = $i;
}

// step off the viewing point and move it to the other side in prep for
// our next iteration.
$leftSum += $val;
}

return $output;
}
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Booj Code Test

## Fun times with equilibrium indexes on an array.
# Equilibrium Indices

## What is an equilibrium index?
An equilibrium index of a sequence is an index into the sequence such that the sum of elements at lower indices is equal to the sum of elements at higher indices.

## Instructions
Write a function that, given a sequence, returns its equilibrium indices (if any). Assume that the sequence may be very long. Feel free to Google for hints, but give lots of comments about what your thoughts are and your process for devising an algorithm.

## Running the Test
```
phpunit EquilibriumIndexTest.php
```
## Set Up
*NOTE:* The version of phpunit that is used here is only compatible with php 5.6 and higher. If you're on a Mac, head on over [here](http://aerendir.me/2015/08/01/how-to-upgrade-php-built-in-your-mac-osx/) for a good step-by-step guide to upgrade.

Otherwise, install dependencies via composer: `Composer Install`.
Don't have composer installed? Head on over to https://getcomposer.org to get it.

## Running Your Tests
`npm run phpunit --silent`
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"phpunit/phpunit": "^5.7"
}
}
Loading