Sword refers to the offer series source code-the number of occurrences in the array is more than half

Source: Internet
Author: User

Topic 1370: Digital time limit for more than half occurrences in an array: 1 seconds Memory limit: 32 Mega Special: No submission: 2844 Resolution: 846 title 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 of length 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears in the array 5 times, which exceeds half the length of the array, the output is 2. Input: Each test case consists of 2 rows: The first line enters an integer n (1<=n<=100000) that represents the number of elements in the array. The second line enters n integers, representing each element in the array, and the range of n integers is [1,1000000000]. Output: corresponding to each test case, the output occurs more than half the length of the array, if there is no output-1. Sample input: 91 2 3 2 2 2 5 4 2 sample output: 2


The most intuitive solution:

Sort the array, select the median, and then compare whether the value is equal to the median and the total is greater than N/2.

#include <iostream> #include <stdio.h>using namespace std;//find base position int adjust (int a[],int left,int right) {int    x = A[left];        while (Left<right) {while (left<right&&a[right]>x) {right--;            } if (left<right) {A[left] = A[right];        left++;        } while (left<right&&a[left]<x) {left++;            } if (left<right) {a[right] = A[left];        right--;    }} A[left] = x; return left;}         Recursion (so that the base position interval is one number) void Quick_sort (int a[],int left,int right) {if (left<right) {int i = adjust (a,left,right);        Quick_sort (a,left,i-1);    Quick_sort (A,i+1,right);    }}int Middle (int *a,int length) {int x = A[LENGTH/2];    int count = 0;        for (int i=0;i<length;i++) {if (X==a[i]) {count++;    }} if (Count*2>length) {return x; } return-1;}    int main () {int n;   while (scanf ("%d", &n)!=eof) {     int* a = new int[n];    for (int i=0;i<n;i++) {scanf ("%d", &a[i]);    } quick_sort (a,0,n-1);    printf ("%d\n", Middle (a,n)); } return 0;}


Sword refers to the offer series source code-the number of occurrences in the array is more than half

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.