On the topic of water King--find the number of occurrences greater than/equal to half the length of the array

Source: Internet
Author: User

On the topic of water King--find the number of occurrences greater than/equal to half the length of the array

1. Title Description


An array of length n, in which some numbers appear more frequently. Please find the number of occurrences greater than half the length of the array. (Water King 1)


an array of length n, in which some numbers appear more frequently. Please find the number of occurrences equal to half the length of the array. (Water King 2)



2. Two-variable method and three-variable method


Whether it is the Water King 1, or the Water King 2, the time complexity of the solution is O (n).


For Water King 1, we use two variables to solve: candidate and Count, the former record water King candidate, the latter count his absolute number of times ! Note that this is the absolute number of times ~ Let's say the following:


    • Will candidate=a[0], count=1, traverse the following number;
    • If the latter number equals candidate, then count++;
    • If the latter number is not equal to candidate, then count--;
    • When the count==0, we let candidate equals the next number, at this time the number of the front reached the "balance" ~ and the absolute number of actual meaning is to count these numbers in the presence of candidate and not candidate the number of the difference!
    • Output finally let count!=0 of that candidate, remember as water King ~


This step obviously can be thought of,absolute number >0 must be the water king.The


The code is as follows:


#include <iostream>using namespace std; int Findshuiwang (int *a, int num) {int candidate=a[0]; int count=1; for (int i=1; i<num; i++) {if (Candidate==a[i]) count++ ; else{count--, if (count==0) {candidate=a[i+1]; i++; count=1;}}} return candidate; }int Main () {int a[6]={2, 1, 2, 2, 1, 2}; int num=6; int shuiwang; Shuiwang=findshuiwang (A, num); cout<<shuiwang<& Lt;endl; return 0; }


For Water King 2, the number of occurrences is exactly half the length of the array, and this time we can determine two things:

1) Water King's "absolute number" ==0;

2) The length of the array must be an even number.


Also our solution answer according to the above Water King 1 idea, until the last candidate count==0, at this time either this candidate is the water King 2, or the last number of the array, because it is he let the candidate count into 0, To the rhythm of the two election ~

At this time, we cleverly introduced a variablehelp, record the number of times the last number of arrays, if equal to N/2 (note that n must be even), then we think the last number is the water king, or candidate is the water King ~


The procedure is as follows:

#include <iostream>using namespace std;  int Findshuiwang (int *a, int num) {int candidate=a[0]; int count=1; int lastone=a[num-1]; int helpcount=lastone==a[0]? 1: 0; for (int i=1, i<num-1; i++)  //a trick to avoid overflow{if (Candidate==a[i]) count++; else{count--; if (count==0) { CANDIDATE=A[I+1]; i++; Count=1; }}if (A[i]==lastone) helpcount++; }return helpcount==num/2-1? Lastone:candidate;   The last one must is lastone!} int main () {int a[6]={2, 1, 2, 2, 1, 0}; int num=6; int shuiwang; Shuiwang=findshuiwang (A, num); COUT<<SHUIWANG<&L T;endl; return 0; }


On the topic of water King--find the number of occurrences greater than/equal to half the length of the array

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.