Skip to content

Instantly share code, notes, and snippets.

@ValeriiVasyliev
Last active May 26, 2017 07:32
Show Gist options
  • Save ValeriiVasyliev/aa388f2383d3e92111c95695cc968cac to your computer and use it in GitHub Desktop.
Save ValeriiVasyliev/aa388f2383d3e92111c95695cc968cac to your computer and use it in GitHub Desktop.
Remaining sticks element (HackerRank Task)
<?php
$lengths = [6, 5, 4, 4, 2, 2, 8];
$result = [];
while (true) {
$lengths = array_filter($lengths, function($a) { return ($a !== 0); });
$minValue = min($lengths);
$remainingSticks = 0;
foreach ($lengths as $index => $val) {
$lengths[$index] = $lengths[$index] - $minValue;
$remainingSticks++;
}
if ($remainingSticks > 0) {
$result[] = $remainingSticks;
if ($remainingSticks == 1) {
break;
}
} else {
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment