Code Logo

Missing Seat Number

Published atDate not available
Easy 0 views
Like0

You can think of this as a small game with a very specific goal. In Missing Seat Number, you are trying to work toward the right number by following one clear idea.

Find missing number in seat sequence A good way to think about it is to first understand what goes in, then what rule you must follow, and finally what shape the answer should have.

For example, if the input is nums = [9,6,4,2,3,5,7,0,1], the answer is 8. Among seats 0 through 9, the label 8 never appears. Another example is nums = [3,0,1], which gives 2. Seats 0 through 3 exist, and 2 is the missing label.

This is a friendly practice problem, but it still rewards careful reading. The key is understanding the rule clearly and then applying it carefully.

One helpful habit is to say the rule out loud in your own words before you start solving. If you can explain what counts, what changes, and what the final answer should look like, you are already much closer to the right solution.

Example Input & Output

Example 1
Input
nums = [3,0,1]
Output
2
Explanation

Seats 0 through 3 exist, and 2 is the missing label.

Example 2
Input
nums = [0,1]
Output
2
Explanation

The full range is 0, 1, 2; the last seat is missing.

Example 3
Input
nums = [9,6,4,2,3,5,7,0,1]
Output
8
Explanation

Among seats 0 through 9, the label 8 never appears.

Algorithm Flow

Recommendation Algorithm Flow for Missing Seat Number
Recommendation Algorithm Flow for Missing Seat Number

Best Answers

java
class Solution {
    public int missing_number(Object nums) {
        if (nums instanceof int[]) {
            int[] arr = (int[]) nums;
            int n = arr.length;
            int expected = n * (n + 1) / 2;
            int sum = 0;
            for(int x : arr) sum += x;
            return expected - sum;
        } else if (nums instanceof java.util.List) {
            java.util.List<?> list = (java.util.List<?>) nums;
            int n = list.size();
            int expected = n * (n + 1) / 2;
            int sum = 0;
            for(Object x : list) sum += (Integer)x;
            return expected - sum;
        }
        return 0;
    }
}