[Risolto] Top Integers - PHP Array Task

  

3
Argomento iniziale

Write a program to find all the top integers in an array. A top integer is an integer which is bigger than all the elements to its right. Print the biggest number and all the numbers after it.

Examples:

top integers php array task

1 risposta
2

The solution:

<?php
 
$array = array_map('intval', explode(' ', readline()));
$count = count($array);
$topInteger = [];
 
for ($row = 0; $row < $count; $row++) {
    $top = true;
    for ($i = $row + 1; $i < $count; $i++) {
        if ($array[$row] <= $array[$i]) {
            $top = false;
            break;
        }
    }
    if ($top) {
        $topInteger[] = $array[$row];
    }
}
echo implode(' ', $topInteger);
Condividi: