More than half the number of occurrences in the array "Microsoft interview 100 question 74th"

Source: Internet
Author: User
Tags sorts

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>intFind (int* ID,intN);intMainvoid){ intid[ -] = {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, -)); return 0;}intFind (int* ID,intN) { intcandidate; intNtimes, I; for(i = Ntimes =0; i < N; i++) { if(Ntimes = =0) {candidate= Id[i], Ntimes =1; } Else { if(Candidate = =Id[i]) ntimes++; ElseNtimes--; } } returncandidate;}

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>intFind (int* ID,intN);intMainvoid){    intid[6] = {2,1,2,1,3,1}; printf ("Water King ID:%d\n", Find (ID,6)); return 0;}intFind (int* ID,intN) {    intcandidate; intNtimes =0, I; intFlag =0; intdel[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++; ElseNtimes--; }    }    returncandidate;}

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>voidFind (int* ID,intN);intMainvoid){    intId[] = {5,6,7,1,2,1,2,1,2,3,3,4,3,2,1,3,2,1,2,1,3}; Find (ID, +); return 0;}voidFind (int* ID,intN) {    intntimes[3], I; intcandidate[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 juxtaposition of ideas are important, think about it .{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//when the new ID and the selected three IDs are different, let the selected three IDs simultaneously-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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.