LeetCampus
Interview Question

Given an array of non-duplicating numbers from 1 to n where one number is missing, write an efficient java program to find that missing number.

July 24, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

Finding a missing number in an array is a common coding challenge that tests a candidate's understanding of algorithms and data structures. The problem typically involves an array containing integers from 1 to n, with one number missing. Interviewers ask this question to evaluate a candidate's problem-solving skills, ability to optimize algorithms, and understanding of mathematical properties. The challenge lies not just in finding the missing number, but in doing so efficiently, often with a focus on time complexity. This question is relevant in many real-world applications, such as data validation and error detection, where identifying missing or corrupted data is crucial. Candidates may also face misconceptions, such as assuming that brute-force methods are the only solution, or overlooking the potential for mathematical solutions, such as exploiting the formula for the sum of an arithmetic series. Understanding different approaches, including linear scans, mathematical calculations, and bit manipulation, is essential for success in this problem.

Sample Answers

Example 1: Using Sum Formula

To find the missing number efficiently, we can use the sum formula for the first n natural numbers, which is given by:

int sum(int n) {
    return n * (n + 1) / 2;
}

This formula computes the expected sum of numbers from 1 to n. We can then subtract the actual sum of the elements in the array from this expected sum to find the missing number. Here’s how we can implement this:

public class MissingNumber {
    public static int findMissingNumber(int[] nums) {
        int n = nums.length + 1; // Since one number is missing
        int expectedSum = sum(n);
        int actualSum = 0;
        for (int num : nums) {
            actualSum += num;
        }
        return expectedSum - actualSum;
    }
}

The time complexity is O(n) due to the single loop through the array, and the space complexity is O(1) since we use only a few extra variables. This approach is optimal and straightforward.

Example 2: Using XOR Operation

Another efficient method to find the missing number is to use the XOR operation. The key idea is that XORing a number with itself results in 0, and XORing a number with 0 results in the number itself. By XORing all numbers from 1 to n with the numbers in the array, we can isolate the missing number. Here’s how this can be implemented:

public class MissingNumber {
    public static int findMissingNumber(int[] nums) {
        int n = nums.length + 1;
        int xorFull = 0;
        int xorArray = 0;

        for (int i = 1; i <= n; i++) {
            xorFull ^= i;
        }
        for (int num : nums) {
            xorArray ^= num;
        }
        return xorFull ^ xorArray;
    }
}

This method runs in O(n) time and requires O(1) space. The XOR trick is particularly elegant and often surprises interviewers, showcasing a deeper understanding of bit manipulation.

Example 3: Sorting Approach

A more straightforward approach is to sort the array and then find the missing number by scanning through it. While this method is less efficient than the previous two, it’s a good way to demonstrate understanding of sorting algorithms. Here’s how it can be implemented:

import java.util.Arrays;

public class MissingNumber {
    public static int findMissingNumber(int[] nums) {
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != i + 1) {
                return i + 1; // The missing number
            }
        }
        return nums.length + 1; // If missing number is n
    }
}

In this approach, we first sort the array, which takes O(n log n) time, then we check for the missing number in a single pass, giving a total complexity of O(n log n). The space complexity is O(1) if we sort in place. While this isn't the most efficient method, it’s still a valid approach and can be useful in scenarios where sorting is already part of the problem.

Keywords

Javamissing numberalgorithmdata structuresproblem-solving

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions