Imagine that you have an exercise in the following aspect, I need to get an array with random integers, the largest number that is between the ranges.
For example, I have an array: array(2, 8, 4);
would have to sort: 2,4,8
check the range of 2 to 4 = 2, and 4 to 8 = 4
The longest range in this case would be 4 , between 4 and 8.
Based on this idea, I have the following code structure:
<?php
class VerifyNumbers
{
private $arr = array();
public function __construct(array $arr)
{
$this->arr = $arr;
}
public function verifyMaxIntersectionFromArray()
{
$collection = array();
sort($this->arr);
for ($i= 0; $i < count($this->arr); $i++) {
if (isset($this->arr[$i + 1])) {
$collection[] = ($this->arr[$i + 1] - $this->arr[$i]);
}
}
return max($collection);
}
}
$verifyNumber = new VerifyNumbers(array(100, 3000, 4000, 25, 540, 20, 200, 300));
$maxIntersection = $verifyNumber->verifyMaxIntersectionFromArray();
echo $maxIntersection;
The question I ask is, and if in case, would I have input values in this array that would exceed 4 billion, or even larger, because there could be negative numbers, which would double, the interval, as I could convert these input numbers into a smaller, readable string, because in PHP 5, an invalid digit is passed to octal integer (for example, 8 or 9), the rest of the number will be ignored.