Search⌘ K
AI Features

Missing Number

Understand how to solve the missing number problem where an array contains distinct numbers in a specific range. Explore how cyclic sort helps find the absent number efficiently without duplicates. Practice implementing the solution and reinforce your grasp of in-place sorting.

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

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


1 / 3

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.

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

1
2
3
4
5

Try it yourself

Implement your solution in MissingNumber.java in the following coding playground. The supporting code template provided in Traversal.java is meant to assist in developing your solution to the problem.

Java
usercode > MissingNumber.java
import java.util.*;
public class MissingNumber{
public static int findMissingNumber(int[] arr) {
// Your code will replace this placeholder return statement
return 0;
}
}
Missing Number