Find Missing Number Solution for Accenture

Company: Accenture · Difficulty: Easy · Topics: Array, Bit Manipulation, Math

Problem Statement

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

Key Understanding:

Examples

Example 1:

Input: nums = [3,0,1]

Output: 2

Explanation:

Example 2:

Input: nums = [9,6,4,2,3,5,7,0,1]

Output: 8

Explanation:

Example 3:

Input: nums = [0]

Output: 1

Explanation:

Intuition

The key insight is that we know the array should contain all numbers from 0 to n, so we can use mathematical properties to find the missing number efficiently.

Mathematical Properties:

1. Sum Formula: The sum of first n natural numbers is n*(n+1)/2

2. XOR Property: XORing a number with itself gives 0, and XORing with 0 gives the number itself

3. Range Property: All numbers from 0 to n should be present exactly once

Why These Approaches Work:

Approach

Approach 1: Sum Formula (Optimal)

Algorithm:

1. Calculate the expected sum using the formula: n*(n+1)/2

2. Calculate the actual sum of all elements in the array

3. Return the difference: expected sum - actual sum

Why This Works:

Complexity Analysis:

Approach 2: XOR Operation

Algorithm:

1. Initialize result = 0

2. XOR result with all numbers from 0 to n

3. XOR result with all numbers in the array

4. Return result

Why This Works:

Complexity Analysis:

Approach 3: Sorting and Linear Search

Algorithm:

1. Sort the array in ascending order

2. Iterate through the sorted array

3. Check if each position contains the expected value (index)

4. Return the first position where the value doesn't match the index

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: nums = [3,0,1]

Sum Formula Approach:

Step 1: Calculate expected sum

Step 2: Calculate actual sum

Step 3: Find missing number

Verification: The array [3,0,1] is missing 2, which is correct.

XOR Approach:

Step 1: XOR all numbers from 0 to 3

Step 2: XOR with all numbers in array

Verification: The missing number is 2, which matches our result.

Sorting Approach:

Step 1: Sort the array

Step 2: Check each position

Result: Position 2 has value 3 instead of 2, so 2 is missing.

Key Insights

1. Mathematical Efficiency: The sum formula approach is both time and space optimal

2. XOR Elegance: XOR approach is mathematically elegant and handles overflow better than sum

3. Sorting Trade-off: Sorting approach is less efficient but more intuitive

4. Overflow Consideration: Sum approach might overflow for large arrays, XOR is safer

5. Single Pass: Both sum and XOR approaches can be done in a single pass

Edge Cases

1. Empty Array: Not possible as per constraints (1 ≤ nums.length ≤ 10^4)

2. Single Element: Array with one element, missing number is either 0 or 1

3. Large Numbers: Sum approach might overflow, XOR is safer

4. All Numbers Present: Not possible as per problem statement (exactly one number is missing)

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Sum Formula (Optimal)

Time Complexity: O(n) - We traverse the array once

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 2: XOR Operation

Time Complexity: O(n) - We traverse the array once

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 3: Sorting and Linear Search

Time Complexity: O(n log n) due to sorting

Space Complexity: O(1) if in-place sorting, O(n) if not

Advantages:

Disadvantages:

More Accenture coding questions