Q:

Write a PHP program to check a given array of integers and return true if the specified number of same elements appears at the start and end of the given array

0

Write a PHP program to check a given array of integers and return true if the specified number of same elements appears at the start and end of the given array.

All Answers

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

<?php
function test($numbers, $len)
    { 
         $arra_size = sizeof($numbers);

            for ($i = 0; $i < $len; $i++)
            {
                if ($numbers[$i] != $numbers[$arra_size - $len + $i])
                {
                    return false;
                }
            }

            return true;
      }   

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

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