Write a PHP program to check a given array of integers and return true if there is a 3 with a 5 somewhere later in the given array
<?php function test($numbers) { $three = false; for ($i = 0; $i < sizeof($numbers); $i++) { if ($three && $numbers[$i] == 5) return true; if ($numbers[$i] == 3) $three = true; } return false; } var_dump(test([3, 5, 1, 3, 7])); var_dump(test([1, 2, 3, 4])); var_dump(test([3, 3, 5, 5, 5, 5])); var_dump(test([2, 5, 5, 7, 8, 10]));
Sample Output:
bool(true) bool(false) bool(true) bool(false)
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