-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathleader.c
35 lines (29 loc) · 787 Bytes
/
leader.c
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
#include <stdio.h>
#include <stdlib.h>
// zakladajac, ze tablica t[n] posiada element wystepujacy na wiecej niz polowie
// pozycji, wskazuje jego wartosc
int leader(int n, int t[]) {
int candidate = t[0];
int count = 1;
for (int i = 1; i < n; ++i) {
if (count == 0) {
candidate = t[i];
++count;
} else { // count > 0
if (t[i] == candidate)
++count;
else
--count;
}
}
return candidate;
}
int main(void) {
int n;
if(!scanf("%d", &n)) printf("wrong input");
int* t = (int*)malloc((unsigned)n * sizeof(int));
for (int i = 0; i < n; ++i) if(!scanf("%d", &t[i])) printf("wrong input");
printf("%d", leader(n, t));
free(t);
return 0;
}