You are given an inclusive range [lower, upper] and a sorted unique integer array nums, where all elements are in the inclusive range.

A number x is considered missing if x is in the range [lower, upper] and x is not in nums.

Return the smallest sorted list of ranges that cover every missing number exactly. That is, no element of nums is in any of the ranges, and each missing number is in one of the ranges.

Each range [a,b] in the list should be output as:

"a->b" if a != b
"a" if a == b
 

Example 1:

Input: nums = [0,1,3,50,75], lower = 0, upper = 99
Output: ["2","4->49","51->74","76->99"]
Explanation: The ranges are:
[2,2] --> "2"
[4,49] --> "4->49"
[51,74] --> "51->74"
[76,99] --> "76->99"
Example 2:

Input: nums = [], lower = 1, upper = 1
Output: ["1"]
Explanation: The only missing range is [1,1], which becomes "1".
Example 3:

Input: nums = [], lower = -3, upper = -1
Output: ["-3->-1"]
Explanation: The only missing range is [-3,-1], which becomes "-3->-1".
Example 4:

Input: nums = [-1], lower = -1, upper = -1
Output: []
Explanation: There are no missing ranges since there are no missing numbers.
Example 5:

Input: nums = [-1], lower = -2, upper = -1
Output: ["-2"]
 

Constraints:

-109 <= lower <= upper <= 109
0 <= nums.length <= 100
lower <= nums[i] <= upper
All the values of nums are unique.

Solution in PHP

<?php
// Your code here!
 class Solution {
        public function getRange($n1, $n2) {
            return $n1==$n2 ? strval($n1) : sprintf("%d->%d" , $n1, $n2);
        }
        
        public function findMissingRanges(array $nums, $lower, $upper) {
            $res= array();
            $next = $lower;
            for ($i = 0; $i < count($nums); $i++) { 
                if($lower==PHP_INT_MAX) return res; 
                if ($nums[$i] < $next) { continue; }
                if ($nums[$i]==$next) {
                    $next++;
                    continue; 
                } 
                array_push($res,$this->getRange($next, $nums[$i] - 1));
                if($nums[$i]==PHP_INT_MAX) return $res; 
                $next=$nums[$i] + 1; 
            } 
                
            if ($next<=$upper) { 
                    array_push($res,$this->getRange($next, $upper));
            }
            return $res; 
        }                          

    }
    $obf = new Solution;
    print_r( $obf->findMissingRanges(array('0', '1', '3', '50', '75'),0,99));




    

OR



    class Solution {

        /**
         * @param Integer[] $nums
         * @param Integer $lower
         * @param Integer $upper
         * @return String[]
         */
        var $result = [];
        
        function findMissingRanges($nums, $lower, $upper) {
            $n = count($nums);
            if ($n == 0 ) { 
                $this->addResult($lower, $upper);
                return $this->result;
            }
            
            if ($nums[0] > $lower) {
                $this->addResult($lower, $nums[0] - 1);
            }
            
            for ($i = 1; $i < $n; $i++ ) {
                if ($nums[$i] - $nums[$i-1] > 1) {
                    $this->addResult($nums[$i-1] + 1, $nums[$i] - 1);
                }
            }
            
            if ($nums[$n-1] < $upper) {
                $this->addResult($nums[$n-1] + 1, $upper);
            }
            return $this->result;
        }
        
        function addResult($min, $max) {
            if ($min == $max) {
                $this->result[] = (string)$min;
            } else {
                $this->result[] = $min . '->' . $max;
            }
        }
    }
?>
Category: WordPress
Balbir KaurPHP and WordPress