Geeks#1-Missing number in array

Problem:
Given an array C of size N-1 and given that there are numbers from 1 to N with one element missing, the missing number is to be found.
Input:
The first line of input contains an integer T denoting the number of test cases. For each test case first line contains N(size of array). The subsequent line contains N-1 array elements.
The first line of input contains an integer T denoting the number of test cases. For each test case first line contains N(size of array). The subsequent line contains N-1 array elements.
Output:
Print the missing number in array.
Print the missing number in array.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ C[i] ≤ 107
1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ C[i] ≤ 107
Example:
Input:
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Input:
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Output:
4
9
4
9
Explanation:
Testcase 1: Given array : 1 2 3 5. Missing element is 4.
Testcase 1: Given array : 1 2 3 5. Missing element is 4.
Solution:
import java.util.Arrays;
import java.util.Scanner;
public class MissinNumbers {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
long i=in.nextLong();
boolean flag=true;
while(i-->0) {
int j = in.nextInt();
long sum=0;
int arr[] =new int[j];
for(int l=0;l<j-1;l++) {
sum+=in.nextInt();
}
long nan = j*(j+1)/2;
System.out.println(nan -sum);
}
}
}
latest tech updates
ReplyDeletelatest tech info
enable wi fi calling
Set up parental controls in play store
parental controls in play store
use iphone without touching
How to use iphone without touching it
Move photos in your iPhone
create Backup of images on your iphone
How to move photos from your iphone to google photos
best android Photo editor
best ios Photo editor
best Photo editor 2020
best Photo editor
best Photo editing app
2020 iphones
2020 new iphones
iphones launches in 2020
latest tech updates
enable wi fi calling
best laptop for students
best gaming laptop under 50k
best gaming laptop under 50,000
best laptop under 50k
best laptop under 40K
best laptop under 40,000
best laptop under 40,000