First Value Greater Than X
PHP
Medium
5 views
Problem Description
Given n numbers and x, print the first number that is > x. If none, print -1.
Input Format
Line1 n x. Line2 n integers.
Output Format
One integer.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$i=0;
$n=intval($tokens[$i++] ?? 0);
$x=intval($tokens[$i++] ?? 0);
$ans=-1;
for($k=0;$k<$n;$k++){
$v=intval($tokens[$i++] ?? 0);
if($ans===-1 && $v>$x) $ans=$v;
}
echo $ans;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!