Header menu

_________________________________________________________________________________

Thursday, 6 February 2014

An array contains number from 1 to 10 . One number is missing. Find the number missing with O(n) complexity.

<?php
  $arr=array(10,1,4,5,7,8,9,3,2);
  $sum=0;
  for($i=0;$i<=10;$i++){
      $sum=$sum+$i;
    }
   $newsum=0;
  foreach($arr as $arr){
      $newsum=$newsum+$arr;
    }
  $missingnumber=$sum-$newsum;
  echo  $missingnumber;
?>