Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.58 KB | None | 0 0
  1. <?php
  2.  
  3. $arr = [7,5, 6,2,4,1,0, 12, 15, 14, 18, 20, 21, 19];
  4.  
  5. sort($arr);
  6.  
  7. $min = null;
  8. $max = null;
  9.  
  10. $count = count($arr);
  11.  
  12. for($index = 0; $index < $count; $index++) {
  13.     $isLastIteration = ($count - $index === 1);
  14.  
  15.     if (!$isLastIteration and ($arr[$index] === ($arr[$index + 1] - 1))) {
  16.         if ($min === null) {
  17.              $min = $arr[$index];
  18.         }
  19.          
  20.         continue;
  21.     }
  22.  
  23.     if ($min !== null) {
  24.         $max = $arr[$index];
  25.  
  26.         echo "$min -> $max", '<br>';
  27.          
  28.         $min = null;
  29.         $max = null;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement