This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Java Program to find missing numbers in an array
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
To find missing numbers in an array first we need to make sure that array is sorted.
After sorting we need to check that array each element with next element then we can find the difference.
if Array is not sorted :To sort array use Arrays.sort(array);
If difference is 1 then no need to do any thing because numbers are in order.
If difference is not equal to 1 then we need to print all those numbers or pick those numbers and place it in one array.
this would be the logic to find missing numbers in an array
Here there may be a chance of array not starts with 1 then we need to check first itself whether array starts with 1 or not if not we need to print 1 to starting element of array.
for example int a[]={4,5,6,8}; then we need to print 1 2 3 7.
Lets see a java example program to find missing numbers in an array.