Maximum Element
PHP
Easy
4 views
Problem Description
Print the maximum value in the array.
Input Format
First n. Next line n integers.
Output Format
One integer max.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$i=0; $n=intval($tokens[$i++] ?? 0);
$mx=null;
for($k=0;$k<$n;$k++){
$v=intval($tokens[$i++] ?? 0);
if($mx===null || $v>$mx) $mx=$v;
}
echo ($mx===null?0:$mx);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!