Q:

Write a PHP program to check a given array of integers and return true if the array contains three increasing adjacent numbers

0

Write a PHP program to check a given array of integers and return true if the array contains three increasing adjacent numbers.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($numbers)
    { 
       for ($i = 0; $i <= sizeof($numbers) - 3; $i++)
          {
             if ($numbers[$i] == $numbers[$i + 1] - 1
                 && $numbers[$i] == $numbers[$i + 2] - 2)
               {
                  return true;
                }
          }
          return false;
      }   

var_dump(test([1, 2, 3, 5, 3, 7]));
var_dump(test([3, 7, 5, 5, 3, 7]));
var_dump(test([3, 7, 5, 5, 6, 7, 5]));

Sample Output:

bool(true)
bool(false)
bool(true) 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now