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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: