Find the Missing Value in a Complete Integer Range

Quick Overview

Practice identifying the sole missing value from an inclusive integer range. Preserve the distinctness guarantee, handle either missing endpoint, and explain time and storage costs.

Find the Missing Value in a Complete Integer Range

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Find the Missing Value in a Complete Integer Range An array `nums` contains `n` distinct integers chosen from the inclusive range `[0, n]`. Exactly one value in that range is absent. Return it. ### Function `int missingNumber(vector<int>& nums)` ### Example 1 ```text Input: nums = [4,0,2,1] Output: 3 ``` There are four entries, so the complete range is `0` through `4`. Only `3` is missing. ### Example 2 ```text Input: nums = [1] Output: 0 ``` ### Constraints - `n == nums.length`. - `1 <= n <= 10000`. - `0 <= nums[i] <= n`. - All entries are distinct. ### Clarifications The range includes both `0` and `n`; neither is guaranteed to appear. The input need not be sorted. Return the absent value, not an array index. ### What a Strong Answer Covers - How the complete expected range relates to the actual entries. - Why the distinctness and range guarantees identify exactly one answer. - Missing boundary values and an input of length one. - Time and auxiliary-space requirements. ### Follow-up Questions Can you solve the problem in `O(n)` time with `O(1)` extra space?

Overview: Practice identifying the sole missing value from an inclusive integer range. Preserve the distinctness guarantee, handle either missing endpoint, and explain time and storage costs.

Google logo
Google
easySoftware EngineerOtherCoding & Algorithms
0
0

Find the Missing Value in a Complete Integer Range

An array nums contains n distinct integers chosen from the inclusive range [0, n]. Exactly one value in that range is absent. Return it.

Function

int missingNumber(vector<int>& nums)

Example 1

Input: nums = [4,0,2,1]
Output: 3

There are four entries, so the complete range is 0 through 4. Only 3 is missing.

Example 2

Input: nums = [1]
Output: 0

Constraints

  • n == nums.length .
  • 1 <= n <= 10000 .
  • 0 <= nums[i] <= n .
  • All entries are distinct.

Clarifications

The range includes both 0 and n; neither is guaranteed to appear. The input need not be sorted. Return the absent value, not an array index.

What a Strong Answer Covers Guidance

  • How the complete expected range relates to the actual entries.
  • Why the distinctness and range guarantees identify exactly one answer.
  • Missing boundary values and an input of length one.
  • Time and auxiliary-space requirements.

Follow-up Questions Guidance

Can you solve the problem in O(n) time with O(1) extra space?