About in array

Hello,

array(0,1,5,6)

source: 2

How Can I find the grand value than 2 in array (echo 5)

AND

array(1,2,4)

source: 5

If there isn’t a value grand than 5, the output can be 1(on the beginnest). How can I find it?

Thank you

In your first example there are two numbers greater than 2: 5 and 6. Why 5? Because it is the lowest number greater than 2 or because it is encountered first in the list?

Thanks for your reply.
Yes, I meant it. first largest number than 2. If there isn’t any largest number than given number, the output can be first number of list.

You can do something like this…

[php]$numbers = array (0,1,5,6);
$count = count($numbers);
$source = 2;
$found = false;

for ($i = 0; $i < $count; $i++) {
if ($source > $numbers[$i]) {
echo $numbers[$i];
$found = true;
break;
}

if ($found === false) {
echo $numbers[0];
}
[/php]

[php]$numbers = array (1,2,4);
$count = count($numbers);
$source = 5;
$found = false;

for ($i = 0; $i < $count; $i++) {
if ($source > $numbers[$i]) {
echo $numbers[$i];
$found = true;
break;
}

if ($found === false) {
echo $numbers[0];
}
[/php]

Thanks so much. God bless your hands.
I can’t run it. Where did I do a mistake?

[php]
$numbers = array (0,1,5,6);
$count = count($numbers);
$source = 2;
$found = false;

for ($i = 0; $i < $count; $i++) {
   if ($source > $numbers[$i]) {
     echo $numbers[$i];
    $found  = true;
   break; 

}
} // This “}” was missing
if ($found === false) {
echo $numbers[0];
}
[/php]

I think Topcoder’s will only work if the array is already sorted. Try:

[php]$numbers = array (0,1,5,6);
sort($numbers);
$source = 2;
$result = $numbers[0];

foreach($numbers as $number) {
if($number > $source) {
$result = $number;
break;
}
}[/php]

@AbraCadaver You’re amazing, thanks for pointing that…

Each of you, thank you very very very very very much
Am trying for this script, http://www.zamanlayici.antenfiyati.com

Again thank you

Sponsor our Newsletter | Privacy Policy | Terms of Service