Java program to find missing number in an array

Previous
Next

If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions.

This is one of basic coding interview question asked in my interviews.

Problem :

You are given an integer array containing 1 to n but one of the number from 1 to n in the array is missing. You need to provide optimum solution to find the missing number. Number can not be repeated in the arry.
For example:

Solution:

  1. Find the sum of n number using formula n=n*(n+1)/2
  2. Find the sum of elements present in given array.
  3. Substract (sum of n numbers – sum of elements present in the array).

Java program to find missing number in an array:

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

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog