Tuesday, November 28, 2017

One of the most frequently asked question on programming interviews is, write a program to find the missing number in an array in Java, C# or any other language; depending upon which language you choose. This kind of coding interview questions are not only asked in small start-ups but also on some of the biggest technical companies like Google, Amazon, Facebook, Microsoft, mostly when they visit the campus of reputed universities to hire graduates. Simplest version of this question is to find missing elements in an area of 100 integers, which contains numbers between 1 and 100. This can easily be solved by calculating the sum of the series using n(n+1)/2, and this is also one of the quickest and efficient ways, but it cannot be used if the array contains more than one missing numbers or if the array contains duplicates.
This gives interviewer some nice follow-up questions to check whether a candidate can apply his knowledge of the slightly different condition or not. So if you get through this, they will ask you to find the missing number in an array of duplicates. This might be tricky but you will soon find out that another way to find missing and duplicate number in the array is to sort it.

In a sorted array, you can compare whether a number is equal to expected next number or not. Alternatively, you can also use BitSet in Java to solve this problem.

Java Program to find missing numbers

Let's understand the problem statement, we have numbers from 1 to 100 that are put into an integer array, what's the best way to find out which number is missing? If Interviewer especially mentions 1 to 100 then you can apply the above trick about the sum of the series as shown below as well. If it has more than one missing element that you can use BitSet class, of course only if your interviewer allows it.

1) Sum of the series: Formula: n (n+1)/2( but only work for one missing number)
2) Use BitSet, if an array has more than one missing elements.

I have provided a BitSet solution with another purpose, to introduce with this nice utility class. In many interviews, I have asked about this class to Java developers, but many of them not even aware of this. I think this problem is a nice way to learn how to use BitSet in Java as well.

You can see that how using a right data structure can solve the problem easily. This is the key takeaway of this program, for the more coding question, you can check the Cracking the Coding Interviews, a collection of 189 coding questions from programming interviews of tech companies like Google, Amazon, Microsoft and others.

That's all on this program to find missing element in an array of 100 elements. As I said, it's good to know the trick, which just require you to calculate sum of numbers and then subtract that from actual sum, but you can not use that if array has more than one missing numbers. On the other hand, BitSet solution is more general, as you can use it to find more than one missing values on integer array. For more programming questions, you can also check here.

There are many ways to search an element in array :1) linear search involves comparing each element to expected value, complexity O(n)2) If array is sorted then you can also use binary search, compexity O(logN)

Why not just add all the number, and subtract the calculate sum with expected sum, if array doesn't contain any duplicates then result is your missing number. This logic will work in any programming language e.g. Go, Rust, Ruby, Python, C, C++ or even JavaScript, becasue it doesn't use language specifice feature, like JDK class BitSet

Int[] input={121,122,123,124,125,126,127,128,129};Output should be:{123,124,125,126,127,128,129}Since 121 and 122 as repeated number. 121 has two 1's and 122 as two 2's so that two number should be removed. Any one suggest me the code.

Hello Nipun, you can still use the same technique but with different formula e.g. n*(a + l)/2 which gives you sum of n numbers, where a and l is first and last number. Btw, the solution will only work if there is only one missing number, if you have two or more, you need to use a hashset

Hello Javin, Thanks for sharing knowledge.Recently i was asked to "write a program to find the missing numbers(more than one number) in unsorted array (lower and upper range and size of array was given) without using any Java API and only 1 iteration is allow". Could you please help.

Your solution implies a sorted array from low to high, even a sorted array in decrease order will fail and it will print only one missing value. The if could be simplify using not and the way you are declaring the array although is accepted by the compiler is not the clearer.

DON'T USE INTEGERS FOR DIVIDING! Yes, you need to calculate the series by n((n+1)/2). If n is even, then n+1 will be odd. If you divide an odd number by 2, you will get the even division without the remainder. The solution would be to either declare expectedSum and actualSum to doubles and then cast the difference to an integer before returning the value.