JAVA VIVA
Name: Ranjit Raj UID:20BCS9943
Q2.Given an array of size N-1 such that it only contains distinct integers in the range of 1
to N. Find the missing element
CODE:
package com.company;
import java.util.Arrays;
class Main
{
public static int getMissingNumber(int[] arr)
{
int n = arr.length;
int m = n + 1;
int total = m * (m + 1) / 2;
int sum = Arrays.stream(arr).sum();
return total - sum;
}
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 5, 7, 8, 9, 10 };
System.out.println("The missing number is " + getMissingNumber(arr));
}
}
OUTPUT: