forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpucmm334.cpp
43 lines (43 loc) · 910 Bytes
/
pucmm334.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
// 2016-04-29
// stupid problem doesn't allow C++11 :(
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N; cin >> N;
int m = 1e9, M = -1;
vector<int> v(N);
for (int i = 0; i < N; i++) {
cin >> v[i];
m = min(m, v[i]);
M = max(M, v[i]);
}
int res;
if (m == M) {
if (m == 0) {
res = 0;
} else if (m == N-1) {
res = N;
} else {
res = -1;
}
} else if (M - m > 1) {
res = -1;
} else {
if (M >= 1 && M < N) {
int nM = 0;
for (int i = 0; i < N; i++) {
if (v[i] == M) nM++;
}
if (M + nM == N) {
res = M;
} else {
res = -1;
}
} else {
res = -1;
}
}
cout << res << "\n";
}