Mark As Completed Discussion

Good morning! Here's our prompt for today.

We're given an array of continuous numbers that should increment sequentially by 1, which just means that we expect a sequence like:

[1, 2, 3, 4, 5, 6, 7]

However, we notice that there are some missing numbers in the sequence.

[1, 2, 4, 5, 7]

Description

Can you write a method missingNumbers that takes an array of continuous numbers and returns the missing integers?

JAVASCRIPT
1missingNumbers([1, 2, 4, 5, 7]);
2// [3, 6]

Constraints

  • Length of the array <= 100000
  • The array will always contain non negative integers (including 0)
  • Expected time complexity : O(n)
  • Expected space complexity : O(1)

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

PYTHON
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's a video of us explaining the solution.

To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's our guided, illustrated walk-through.

How do I use this guide?

The key thing to note here is that the numbers are sequential, and as such there's an expectation that the input array is sorted to begin with. In other words, since the numbers are incrementing by one at each index, they'll by definition be sorted in ascending order.

Key Thing

Knowing that, we can do a simple iteration through the array, and check that the difference between the current number and the one before it is 1.

The difficult part is when there are gaps larger than 1. For example:

[3, 4, 7, 8]

When we get to 7, we'll notice that the difference between it and the last number (4) is 3, which is more than just a gap of 1.

Knowing that, we can simply use a while loop to append the appropriate numbers in the missing array while keeping a counter. Let's call this counter diff. It'll track how many numbers we have to append before we close the gap.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

A Faster Approach, But Harder to Grok

In the above solution, the runtime complexity appears to be O(n^2) because of the additional while loop-- but it isn't too hard to show that we never iterate through more than n elements. This is still O(n) linear time because regardless of whether we enter the while clause, we'll still be iterating through the same number of input.

However, we can still speed this up by iterating through both the input array and the "comparison array" simultaneously.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

One Pager Cheat Sheet

  • Write a function missingNumbers that takes an array of continuous numbers and returns the missing integers in O(n) time with O(1) space complexity.
  • The key to finding the missing number in an array is to make sure that the array is sorted in ascending order, then check that each adjacent number is incrementing by 1, or use a while loop to append the appropriate numbers.
  • This approach is harder to grok, however it can be optimized by iterating through both arrays simultaneously, resulting in an O(n) runtime complexity.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

PYTHON
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Great job getting through this. Let's move on.

If you had any problems with this tutorial, check out the main forum thread here.