Archive for the Programming Category

Interview Question: Find the missing number in sequence from 1 to 100/100000

Posted in Programming on October 16, 2016 by tasam21

You can use your own programming logic to find that answer in any languages but I would like to share how to resolve this problem using PHP programming logic:

If the number range is huge like 1 to 100 million then I would prefer to break into smaller ranges and then run the following code to make the CPU to consume less resources.


//1. put the number in array variable
$numberRange = array(1,2,3,4,6,7,9,10,13,15,16,18,19,23,24,25.....);
//2. Initialize the Missing number to be search from 1
$missingNumber = 1;
//3. For loop variable $n initialize with 0, should be less than 100 and increment.
for($n=0; $n<100; $n++) {
//4. Using If condition to check Missing number is in that array for not
if(!in_array($missingNumber, $numberRange)){
//5. Print that Missing number
echo $missingNumber. " is missing number" . '
';
}
$missingNumber++;
}

Advertisements