Write a PHP program to check if a given array of integers contains 5 next to a 5 somewhere
<?php function test($nums) { for ($i = 0; $i < sizeof($nums) - 1; $i++) { if ($nums[$i] == 5 && $nums[$i] == $nums[$i + 1]) return true; } return false; } var_dump(test([1, 5, 6, 9, 10, 17])); var_dump(test([1, 5, 5, 9, 10, 17])); var_dump(test([1, 5, 5, 9, 10, 17, 5, 5]));
Sample Output:
bool(false) bool(true) bool(true)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer