3v4l.org

run code in 300+ PHP versions simultaneously
<?php $candiesTests = [ [1, 1, 2, 3], // Test1 => 1,3 [1, 1, 2, 3, 4], // Test2 => 1,4 [1, 1, 2, 2, 3, 4, 5, 5], // Test3 => 1,2,5,5 [1, 1, 2, 2, 3, 4, 5, 5, 1, 1, 5], // Test4 => 1,1,1,2,5 ]; function distributeCandies(array $candies): array { $quota = ceil(count($candies) / 2); // Berit's share $adam = []; // fill first $berit = []; // the leftovers $candyCounts = array_count_values($candies); ksort($candyCounts); while ($candyCounts) { foreach ($candyCounts as $candy => &$count) { $adam[] = $candy; --$count; if (!$count) { unset($candyCounts[$candy]); } if (count($adam) == $quota) { break 2; } } } foreach ($candyCounts as $candy => $count) { array_push($berit, ...array_fill(0, $count, $candy)); } return [$adam, $berit]; } foreach ($candiesTests as $candies) { $distribution = distributeCandies($candies); echo 'Adams candies: ' . implode(', ', $distribution[0]) . PHP_EOL . 'Berits candies: ' . implode(', ', $distribution[1]) . PHP_EOL . '---' . PHP_EOL; }
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.29, 8.4.1 - 8.4.14, 8.4.16 - 8.4.17, 8.5.0 - 8.5.2
Adams candies: 1, 2 Berits candies: 1, 3 --- Adams candies: 1, 2, 3 Berits candies: 1, 4 --- Adams candies: 1, 2, 3, 4 Berits candies: 1, 2, 5, 5 --- Adams candies: 1, 2, 3, 4, 5, 1 Berits candies: 1, 1, 2, 5, 5 ---
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
163.05 ms | 407 KiB | 5 Q