fork(2) download
  1. <?php
  2.  
  3. $list = range(1,100);
  4. unset($list[3]);
  5. unset($list[31]);
  6.  
  7. findMissing($list,1,100);
  8.  
  9. function findMissing($list, $min, $max) {
  10. if(empty($list)) {
  11. print_r(range($min, $max));
  12. return;
  13. }
  14.  
  15. $l = $r = [];
  16. $pivot = array_pop($list);
  17.  
  18. foreach($list as $number) {
  19. if($number < $pivot) {
  20. $l[] = $number;
  21. }
  22. else {
  23. $r[] = $number;
  24. }
  25. }
  26.  
  27. if(count($l) == $pivot - $min - 1) {
  28. // only 1 missing number use difference of sums
  29. print array_sum(range($min, $pivot-1)) - array_sum($l) . "\n";
  30. }
  31. else if(count($l) < $pivot - $min) {
  32. // more than 1 missing number, recurse
  33. findMissing($l, $min, $pivot-1);
  34. }
  35.  
  36. if(count($r) == $max - $pivot - 1) {
  37. // only 1 missing number use difference of sums
  38. print array_sum(range($pivot + 1, $max)) - array_sum($r) . "\n";
  39. } else if(count($r) < $max - $pivot) {
  40. // mroe than 1 missing number recurse
  41. findMissing($r, $pivot+1, $max);
  42. }
  43. }
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
4
32