-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathMissing-number-in-array.cpp
54 lines (42 loc) · 1.02 KB
/
Missing-number-in-array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
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.
Example 1:
Input:
N = 5
A[] = {1,2,3,5}
Output: 4
Example 2:
Input:
N = 10
A[] = {1,2,3,4,5,6,7,8,10}
Output: 9
Your Task :
You don't need to read input or print anything.
Complete the function MissingNumber() that takes array and N as input parameters and returns the value of the missing number.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
*/
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
int MissingNumber(vector<int>& array, int n) {
// Your code goes here
int sum=0;
for(int i=0;i<n-1;i++) sum+=array[i];
return (n*(n+1)/2)-sum;
}
};
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> array(n - 1);
for (int i = 0; i < n - 1; ++i) cin >> array[i];
Solution obj;
cout << obj.MissingNumber(array, n) << "\n";
}
return 0;
}