settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Top Integers - PHP Array Task

+4 votes

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

asked in PHP category by user john7

1 Answer

+3 votes

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);
answered by user Jolie Ann
...