Companies:
Paytm, Ola-Cabs, Microsoft, Intuit, Cisco, Amazon
Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missing number is to be found.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,size of array.
The second line of each test case contains N-1 input C[i],numbers in array.
Output:
The first line of each test case is N,size of array.
The second line of each test case contains N-1 input C[i],numbers in array.
Output:
Print the missing number in array.
Constraints:
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 1000
1 ≤ C[i] ≤ 1000
1 ≤ N ≤ 1000
1 ≤ C[i] ≤ 1000
Example:
Input
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Output
4
9
4
9
Algorithm:
1) Get the actual size of array (n in our solution)
2) Find the sum using the formula n*(n+1)/2
3) Subtract all elements of array from the above obtained sum to get ans
Code;
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) {
//code
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for (int i=0;i<m;i++){
int n = sc.nextInt();//size of array
int sum = n*(n+1)/2;
int [] arr = new int[n];
for(int j=0;j<n-1;j++){
arr[j]=sc.nextInt();
}
for (int k=0;k<n-1;k++){
sum=sum-arr[k];
}
System.out.println(sum);
}
}
}
Comments
Post a Comment