-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_MissingNum.cpp
More file actions
executable file
·51 lines (45 loc) · 1.16 KB
/
LC_MissingNum.cpp
File metadata and controls
executable file
·51 lines (45 loc) · 1.16 KB
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lli long long int
#define vi vector<int>
#define forinput for(int i = 0; i<n ; i++){cin>>arr[i];}
#define foroutput for(int i = 0; i<n; i++){cout<<arr[i]<<" ";}
#define pb push_back
#define pob pop_back
class Solution {
public:
// BitManipulationApproach
int missingNumber(vector<int>& nums) {
int res = 0;
for (int val: nums) {
res ^= val;
}
for (int i=1; i<=nums.size(); i++) {
res ^= i;
}
return res;
}
// Adding Approach
// int missingNumber(vector<int>& nums) {
// int n = nums.size();
// int count = (n*(n+1))/2; //Optimal Ligic than Line 16
// int sum = accumulate(nums.begin(), nums.end(), 0);
// // for(int it = 0; it <= nums.size(); it++){
// // count+=it;
// // }
// int miss_num = count - sum;
// return miss_num;
// }
};
int main(){
Solution S1;
vector<int> nums;
// cout<<S1.missingNumber(nums)<<endl;
int input;
while(cin>>input){
nums.pb(input);
}
cout<<S1.missingNumber(nums)<<endl;
return 0;
}