3
Imagine you have an exercise in the following aspect, I need to take from an array with random integers, the largest number that is between the intervals.
For example, I have an array: array(2, 8, 4);
would have to order: 2,4,8
check the range 2 to 4 = 2, and 4 to 8 = 4
the longest interval, in this case, would be the 4, between 4 and 8.
Starting from that 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, if in this case, I had input values in this array that were more than 4 billion, or even larger, because there could be negative numbers, which would double, the range, how could I 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.
This might help Ivan: http://answall.com/questions/9299/integercom-0-%C3%A0-left-%C3%A9-printed-as-other-n%C3%Bamero
– Miguel
http://www.php.net/manual/en/book.bc.php
– bfavaretto
Does your PHP is 32 bits and is breaking the maximum limit of numbers?
– bfavaretto
I found your description a little confusing. "the greater number that is between the intervals. "? From 2 to 4 = 2 ( 2 is less than 4 and not greater, so you refer to 4-2? Or do you want the smallest of them instead of the largest? ) 4 to 8 = 4 (same thing as the previous query)
– Antonio Alexandre
yes, I mean subtraction.
– Ivan Ferrer