Last active
May 26, 2017 07:32
-
-
Save ValeriiVasyliev/aa388f2383d3e92111c95695cc968cac to your computer and use it in GitHub Desktop.
Remaining sticks element (HackerRank Task)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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