First Value Greater Than X

First Value Greater Than X

Medium PHP PHP Control Flow 22 views
Explanation Complexity

Problem Statement

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.

Example

5 3
1 3 2 8 5
8

Constraints

n

Input / Output Format

Input Format
Line1 n x. Line2 n integers.
Output Format
One integer.
Constraints
n

Examples

Input:
5 3 1 3 2 8 5
Output:
8

Example Solution (Public)

PHP
<?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;
?>

Official Solution Code

<?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;
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.