-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_BIT_CheckMissingNumber.cpp
55 lines (41 loc) · 1.27 KB
/
GFG_BIT_CheckMissingNumber.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
55
/* https://practice.geeksforgeeks.org/problems/missing-number-in-array1416/1#
* Missing number in array
* 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.
*/
// { Driver Code Starts
// Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function template for C++
class Solution{
public:
int MissingNumber(vector<int>& array, int n) {
// Your code goes here
/********* using sum of n numbers ***********/
// int sum_of_n = (n*(n+1))/2;
// int sum_of_arr = accumulate(array.begin(), array.end(),0);
// return sum_of_n - sum_of_arr;
/********* using xor ***********/
int xor_sum=0;
for (int e: array)
xor_sum ^= e;
for (int i=1; i<=n; i++)
xor_sum ^= i;
return xor_sum;
}
};
// { Driver Code Starts.
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;
} // } Driver Code Ends