Missing Number

Try to solve the Missing Number problem.

Statement#

Given an array, nums, containing nn distinct numbers in the range [0,n][0, n], return the only number in the range that is missing from the array.

Constraints:

  • n=n = nums.length
  • 1n1031 \leq n \leq 10^3
  • 00 \leq nums[i] n\leq n
  • There are no duplicates in the array.

Examples#

Created with Fabric.js 3.6.6 Output Missing number = 8 5 6 4 2 1 3 0 7 9 Sample example 1Input

1 of 2

Created with Fabric.js 3.6.6 5 6 10 2 1 3 0 7 9 12 11 8 Missing number = 4 Output Sample example 2Input

2 of 2

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Missing Number

1

What would be the output if the following array is given as input?

[0, 2, 4, 6, 7, 8, 3, 9, 5, 10]

A)

11

B)

2

C)

1

D)

3

Question 1 of 30 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Start the list traversal from the first element.

If the list element isn’t equal to its index, swap it with the number on the correct index.

Else, if the element is at the correct index or greater than the length of the array, skip it and move one step forward.

Once you’ve iterated over the entire array, compare each number with its index.

The first occurrence of an index that’s not equal to its list element is the missing number.


Try it yourself#

Implement your solution in main.py in the following coding playground. We have provided some useful code templates in the other file that you may build on to solve this problem.

Python
main.py
traversal.py
Input #1
%0 node_01 0 node_11 1 node_21 2 node_31 4
Visualization for Input #1
Missing Number

Cyclic Sort: Introduction

Solution: Missing Number