find-missing-and-repeated-values 1.0.0
Find Missing and Repeated Values
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <bits/stdc++.h>
2
3using namespace std;
4
6{
7public:
8 vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid)
9 {
10 // construct set of elements and find repeating value
11 int n = (int)grid.size();
12 vector<int> values(n * n + 1, 0);
13 int a, b;
14 for (const auto& row: grid)
15 {
16 for (int el: row)
17 {
18 if (values[el] != 0)
19 a = el;
20 else
21 values[el] = 1;
22 }
23 }
24 // find missing value
25 for (int i = 1; i <= n * n; ++i)
26 {
27 if (i == a)
28 continue;
29 else if (values[i] == 0)
30 {
31 b = i;
32 break;
33 }
34 }
35 vector<int> answer = {a, b};
36 return answer;
37 }
38};
39
40int main()
41{
42 vector<vector<int>> grid = {{1, 3}, {2, 2}};
43 vector<int> answer = Solution().findMissingAndRepeatedValues(grid);
44 cout << "[" << answer[0] << ", " << answer[1] << "]\n";
45 return 0;
46}
vector< int > findMissingAndRepeatedValues(vector< vector< int > > &grid)
Definition main.cpp:8
int main()
Definition main.cpp:40