Safe Array Index
PHP
Easy
4 views
Problem Description
Given n integers and index i, print a[i] or OUT_OF_RANGE.
Input Format
Line1 n i. Line2 n integers.
Output Format
Value or OUT_OF_RANGE.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$idx=0; $n=intval($tokens[$idx++] ?? 0);
$i=intval($tokens[$idx++] ?? 0);
$a=[];
for($k=0;$k<$n;$k++) $a[] = $tokens[$idx++] ?? '0';
if($i<0 || $i>=$n) echo 'OUT_OF_RANGE';
else echo $a[$i];
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!