Description:
-
There is a number in the array that appears more than half the length of the array. Please find this number. For example, enter an array {, 2} with a length of 9 }. Because number 2 appears five times in the array and exceeds half the length of the array, Output 2.
-
Input:
-
Each test case contains two rows:
Enter an integer n (1 <=n <= 100000) In the first line, indicating the number of elements in the array.
In the second line, enter n integers, indicating each element in the array. The range of these n integers is [1,].
-
Output:
-
For each test case, the number of output times exceeds half of the length of the array. If-1 is not output.
-
Sample input:
91 2 3 2 2 2 5 4 2
-
Sample output:
2
Solution:
There are two ideas. First, we can successfully solve this problem:
We first sort the array, because we need to find more than half of the number, so if there is, then the median value of this array must be this number. For example
1 2 2 2 1 or 1 1 2 2 2 or 2 2 2 3 3, the middle must be the number we are looking.
If not, perform an O (n) scan. Therefore, the time complexity of our algorithm is fast sorting + one traversal, O (nlogn) + O (n ).
The code for quick sort is as follows:
void Qsort(int begin,int end){ int middle; if(begin < end){ middle = Patition(begin,end); Qsort(begin,middle -1); Qsort(middle+1,end); }}int Patition(int begin,int end){ int middle = gArr[begin]; while(begin < end){ while(begin < end && gArr[end] >= middle) end--; swap(begin,end); while(begin < end && gArr[begin] <= middle) begin++; swap(begin,end); } return begin;}void swap(int begin,int end){ int tmp = gArr[end]; gArr[end] = gArr[begin]; gArr[begin] = tmp;}
All code:
#include <stdio.h>#include <stdlib.h>#define MAXSIZE 100001int gArr[MAXSIZE] = {0};void Qsort(int begin,int end);void swap(int begin,int end);int Patition(int begin,int end);int main(){ int n,i,middle,count; while(scanf("%d",&n)!=EOF && n>0 && n <= 100000){ for(i=0;i<n;i++){ scanf("%d",&gArr[i]); } Qsort(0,n-1); middle = gArr[n/2]; count = 0; for(i=0;i<n;i++){ if(middle == gArr[i]) count++; } if(count > n/2) printf("%d\n",middle); else printf("-1\n"); }}void Qsort(int begin,int end){ int middle; if(begin < end){ middle = Patition(begin,end); Qsort(begin,middle -1); Qsort(middle+1,end); }}int Patition(int begin,int end){ int middle = gArr[begin]; while(begin < end){ while(begin < end && gArr[end] >= middle) end--; swap(begin,end); while(begin < end && gArr[begin] <= middle) begin++; swap(begin,end); } return begin;}void swap(int begin,int end){ int tmp = gArr[end]; gArr[end] = gArr[begin]; gArr[begin] = tmp;} /************************************************************** Problem: 1370 User: xhalo Language: C Result: Accepted Time:800 ms Memory:1304 kb****************************************************************/
Another idea
Each element is counted. However, there is a problem in record statistics, that is, how to find the elements we want to record from the record array. The following code contains a large amount of data. I guess it is 100000 non-repeating points, so the time-out for the repeat operation times out. However, when the data volume is small, it is okay:
#include <stdio.h>#include <stdlib.h>#define MAXSIZE 100000typedef struct flag{ int data; int counter;}Flag;typedef struct fArr{ struct flag arr[MAXSIZE];}FArr;int gArr[MAXSIZE] = {0};int gnum;int main(){ int n,i,max,maxNum; while(scanf("%d",&n)!=EOF && n>0 && n <= 100000){ FArr *a = (FArr *)malloc(sizeof(FArr)); gnum = 0; max = -1; maxNum = -1; for(i=0;i<n;i++){ scanf("%d",&gArr[i]); } for(i=0;i<n;i++){ int j = 0; while(j<gnum){ if(a->arr[j].data == gArr[i]) break; j++; } if(gnum != 0 && j != gnum){ a->arr[j].counter++; }else{ a->arr[gnum].data = gArr[i]; a->arr[gnum++].counter = 1; } } for(i=0;i<gnum;i++){ if(max < a->arr[i].counter){ max = a->arr[i].counter; maxNum = a->arr[i].data; } } if(max > n/2) printf("%d\n",maxNum); else printf("-1\n"); }}/************************************************************** Problem: 1370 User: xhalo Language: C Result: Time Limit Exceed****************************************************************/