Sunday, 2 July 2017

How to Find Missing Number in a Sorted Array in Java

Today's coding problem is not very new, it's age old classic from programming interviews. You have a sorted array containing n - 1 unique numbers starting from 0 to n - 1. There is only one number missing in this range and you need to find that out. I mean you need to write a Java method to find the missing number and print it's value in the console. Some of you might have seen this question before, but if you have not been asked this question before, what is the first approach comes into your mind to solve this question?
Since only one number is missing, many programmers come up with the approach of iterating over the array and comparing each element with the expected one e.g. first element should be 0, the second element should be 1 and so on. Though this will sort the problem, it will costs you O(n) time. What can you do to improve performance? The key here has we have sorted the array, do you think our earlier solution is taking full advantage of this knowledge, well it is but not fully. What it is doing is performing linear search which is costing O(n), but if you do binary search, which of course need a sorted array, we can reduce the time taken in the range of O(logN). Since numbers are in the range from 0 to n - 1 and are sorted, the first number till the missing one should be same as their indexes. I mean if 0 is not missing, then it must be in the first index i.e. at 0. If you generalize this, you will find out that if the missing number is k then all numbers less than k are located in an array with indexes same as their value. Also number k + 1 will be located at index k, and number k + 2 will be located at index k + 1. What does this mean? Well, it means that missing number is the first cell whose value is not same as its index. So our problem reduces to search in an array to find the first cell, whose value is not same as its index. You can easily find out this by using binary search algorithm in O(logN) time. Our function implements this logic to find the missing integer in a sorted array in Java. You can use this solution to find missing number in array of numbers 1-1000 or 1 -100.

Function to find Missing Number in Sorted Array

Here is our complete solution of this problem. As discussed in first paragraph, solution is based upon binary search algorithm and that's why it's complexity is in logarithmic order. If you asked this question in interview, you must write production quality code, what this means is handling invalid input and boundary conditions. In our method, we are checking whether array is not null and empty before proceeding further. If you are not familiar with binary search algorithm than this diagram will help you how does it work. In binary search, instead of starting form front we start from middle element. If middle element is greater than expected number than expected number is on left hand side of middle element (lower array), otherwise it is on right hand side (higher array). So in each iteration we end up reducing our problem set by half. So in start if you have 16 element in array, next iteration you only need to search in 8 element and subsequently 4 and 2, this is how we get O(logN) complexity.

How to Find Missing Number in a Sorted Array in Java

And here is our code example to find missing integer in a sorted array or series.

import java.util.Arrays;
 * Java Program to find the missing number in a sorted array with integers in range 0 to n -1
 * @author Javin Paul
public class MissingNumberFinder {

    public static void main(String args[]) {

        System.out.println("Test #1 : Missing number in sorted array ");
        int[] input = new int[]{1, 2, 3, 4, 6};
        int missing = missingNumberFromSortedArray(input);
        System.out.println("Missing number from array : " + Arrays.toString(input) + " is : " + missing);


    public static int missingNumberFromSortedArray(int[] numbers) {
        if (numbers == null || numbers.length <= 0) {
            throw new IllegalArgumentException("Null or Empty array not permitted");

        int left = 0;
        int right = numbers.length - 1;
        while (left <= right) {
            int middle = (right + left) >> 1;
            if (numbers[middle] != middle) {
                if (middle == 0 || numbers[middle - 1] == middle - 1) {
                    return middle;
                right = middle - 1;
            } else {
                left = middle + 1;
        throw new RuntimeException("No missing number");


Test #1 : Missing number in sorted array
Missing number from array : [1, 2, 3, 4, 6] is : 0

That's all about how do you find missing integer in an array of 0 to n - 1. Remember you can also solve this problem with linear search and their is nothing wrong if you first come up with that solution, in fact it's very natural, but you must pay attention on sorted word. Binary search and sorted array goes hand in hand. When it comes to improving search algorithm, binary search always better than linear search.