0% found this document useful (0 votes)
22 views2 pages

Java Viva: Name: Ranjit Raj UID:20BCS9943

The document contains code to find the missing number in a range from 1 to N where an array of size N-1 contains distinct integers within that range except for the missing number. The code takes the array as a parameter, calculates the total expected sum of all numbers from 1 to N using the formula for the sum of an arithmetic progression, calculates the actual sum of the elements in the array, and returns the difference which is the missing number. When run on a sample array with a missing 6, the code correctly outputs 6 as the missing number.

Uploaded by

Avi Manyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Java Viva: Name: Ranjit Raj UID:20BCS9943

The document contains code to find the missing number in a range from 1 to N where an array of size N-1 contains distinct integers within that range except for the missing number. The code takes the array as a parameter, calculates the total expected sum of all numbers from 1 to N using the formula for the sum of an arithmetic progression, calculates the actual sum of the elements in the array, and returns the difference which is the missing number. When run on a sample array with a missing 6, the code correctly outputs 6 as the missing number.

Uploaded by

Avi Manyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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:

You might also like