Problem Request:
A number in the array appears more than half the length of the array to find this number.
Reference: Programming Beauty 2.3 Looking for a post water king
Problem Analysis:
Method 1 Sorts the array, then finds the most of them sequentially;
Method 2 Sorts the array, the middle one must be the number to find, the time complexity O (NLOGN);
Method 3 Each elimination of the array of two different numbers, the last remaining must be to find the number, time complexity O (N).
Method 3 Code 1 is as follows, the following is the schematic code 1
Code implementation:
Code 1#include <stdio.h>int Find (int* ID, int N); int main (void) { int id[30] = { 5,6,7,1,2,1,2,1,2,3,3,4,3,2,1,3,2,1,2,1,3,1,1,1,1,1}; printf ("Water King ID:%d\n", Find (id,30)); return 0;} int Find (int* ID, int N) { int candidate; int ntimes, I; for (i = ntimes = 0; i < N; i++) { if (Ntimes = = 0) { candidate = Id[i], ntimes = 1; } else { if (candidate = = Id[i]) ntimes++; else ntimes--; } } return candidate;}
Extension Issue 1:
What if the number is half the length of the array?
Problem Analysis:
Remove the 3 different IDs first and then use the method of code 1 to solve them. Since the elimination of 3 different IDs, the water King ID at most eliminate one, then the number of water King ID is more than half of the total, it becomes the original problem.
Code implementation:
#include <stdio.h>int Find (int* ID, int N); int main (void) { int id[6] = {2,1,2,1,3,1}; printf ("Water King ID:%d\n", Find (id,6)); return 0;} int Find (int* ID, int N) { int candidate; int ntimes = 0, I; int flag = 0; int del[3]; Del[0] = id[0]; for (i = 1; i < N; i++) { if ((id[i]! = del[0]) && (flag = = 0)) { del[1] = id[i]; flag = 1; Continue; } if (id[i]! = del[0]) && (id[i]! = del[1]) && (flag = = 1)) { flag = 2; Continue; } if (Ntimes = = 0) { candidate = Id[i], ntimes = 1; } else { if (candidate = = Id[i]) ntimes++; else ntimes--; } } return candidate;}
Extension Issue 2:
If the number of 3 numbers in the array exceeds 1/4 of the array's length, how do you find them?
Problem Analysis:
Bind three IDs at a time, plus minus.
Code implementation:
#include <stdio.h>void Find (int* ID, int N); int main (void) {int id[] = {5,6,7,1,2,1,2,1,2,3,3,4,3,2,1,3,2,1,2,1,3 }; Find (id,21); return 0;} void Find (int* ID, int N) {int ntimes[3], I; int candidate[3]; ntimes[0]=ntimes[1]=ntimes[2]=0; Candidate[0]=candidate[1]=candidate[2]=-1; for (i = 0; i < N; i++) {if (id[i]==candidate[0])//These several parallel ideas are very important, think well of {ntimes[0]++; } else if (Id[i]==candidate[1]) {ntimes[1]++; } else if (Id[i]==candidate[2]) {ntimes[2]++; } else if (ntimes[0]==0) {ntimes[0]=1; Candidate[0]=id[i]; } else if (ntimes[1]==0) {ntimes[1]=1; Candidate[1]=id[i]; } else if (ntimes[2]==0) {ntimes[2]=1; Candidate[2]=id[i]; } else//the new ID and the selected three IDs are different, let the selected three IDs at the same time-1 {ntimes[0]--; NTIMES[1]--; ntimes[2]--; }} printf ("ID:%d%d%d\n", candidate[0],candidate[1],candidate[2]); printf ("times:%d%d%d\n", ntimes[0],ntimes[1],ntimes[2]); return;}
More than half the number of occurrences in the array "Microsoft interview 100 question 74th"