$max_input = max($input); // find the missing value in ascending order & avoid duplication $missing_values = []; for ($i = 1; $i <= $max_input; $i++) { // skip if current value is not missing OR is already inside the $missing_values container if (in_array($i, $input) || in_array($i, $missing_values)) continue; $missing_values[] = $i; }
$results = []; foreach ($inputas$i) { // goes through all input values $diff = 0; // this is the `-1`, `-2`, `-4` (green color) on the mapping there foreach ($missing_valuesas$mv) { if ($i < $mv) break; $diff++; // count the number of missing values infront of current value } // finally minus the difference will get the desired result $results[] = $i - $diff; }