Find the number of occurrences in an array of size n greater than N/2

Source: Internet
Author: User

        problem Description: In an array of size n, one of the number occurrences exceeds N/2, and the number is calculated. This problem seems very simple, but to find the best solution is not easy, the general situation we first think of the most stupid method, each selected a number, iterate through an array, the complexity O (n^2), or first sort and then find that number, the complexity is generally o (NLGN), or with hash, time complexity O (N), Space complexity needs to look at the input data size, Space complexity O (N). So these are not the optimal solution, we first analyze this topic, set the number of occurrences of x, then x satisfied, n/2+1<= x <=n; So we can think of if the number and the rest of the number is completely offset, there are at least 1 left, we go from the back, set the key to the first number, The number of occurrences of the key is Ntime, initialized to 1, on behalf of the key appears once, from the trip, if a number is not equal to key, then they cancel, the occurrence of key minus one, if equal to key, the number of occurrences of key and 1, if the number of occurrences of key becomes 0, The key is already exhausted, so you need to reinitialize the key to another number, and then repeat the above steps, because there must be a number greater than N/2, so the last number to traverse to the end is the number required.

 #include <iostream> #include <vector> using namespace std;/* The number of searches in an array of size n is more than N/2 */int
    Find_data (vector<int> &arry) {int ntime = 0;//indicates the number of occurrences of a number int result;
              for (unsigned int i = 0; i < arry.size (); i++) {if (Ntime = = 0) {////Before I delete all, or start, arry[i] into the result
              result = Arry[i];  Ntime = 1;
                Arry[i] The number of occurrences is 1;} else {//If there is a number above, the result has not been offset by the if (result = = Arry[i])//If the number of equal result occurrences +1
              ntime++;  else ntime--; /* If arry[i at this time] is not equal to result, then they are two offset, the number of result is reduced by one, and the number is greater than N/2 so it is not complete, ntime minimum is 1 that the number of occurrences is greater than or equal to n/2+1
*/}} return result; 
    } int main () {vector<int> arry;
    int n;
    cin>>n;
        for (int i = 0; i < n; i++) {int D;
        cin>>d;
    Arry.push_back (d);
    } int result = Find_data (Arry);
    cout<<result;
return 0; }

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.