Find Missing Number from Array in Java

In this post, we will see how to find missing number from array or series of numbers.


Problem

You have an array of numbers from 1 to n and one number is missing from series.You need to find missing number using java program.

Example:
arr[] = { 1,4,5,3}
Missing number = 2

Solution

  • Calculate sum of number using 1 to N using formula sum1ToN= (N * (N+1))/2
  • Calculate sum of all number in array numArrSum
  • Missing number =¬†sum1ToN –¬†numArrSum

Java Program to find missing number in the array

When you run the above program, you will get below output

Missing number in numArr1: 2
Missing number in numArr2: 6

That’s all about how to find Missing Number from Array in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *