find the missing number in an array

To find the missing number in an array we are perform following step :-

  • First we initialize one array a with int value;
  • Declare two int variable sum1 and sum2;
  • Create one for loop and execute from 0 to less than array length size.
  • Add the value a[i] with sum1 temp vaiable.
  • Create another for loop and execute from 0 to last value of array variable.
  • Add index value in temp sum2 variable.
  • Now sum2 minus sum1 do,which result will be missing number.

                int mn = sum2 – sum1;

				
					public class MissingNumber {
	public static void main(String[] args) {
		
		int a[] = { 1, 2, 3, 5 };
		int sum1 = 0;
		int sum2 = 0;
		
		for (int i = 0; i < a.length; i++) {
			sum1 = sum1 + a[i];
		}
		
		for (int i = 0; i <= 5; i++) {
			sum2 = sum2 + i;
		}
		int mn = sum2 - sum1;
		System.out.println("MissingNumber are :- "+mn);
	}
}
				
			

Output :-
MissingNumber are :- 4