Sword offer--Face question 29: The number of occurrences in the array is more than half

Source: Internet
Author: User

Title Description:

If there is a number in the array that appears more than half the length of the array, 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:

For each test case, the output occurs more than half the number of array lengths, if no output-1.

Solution One: A method based on fast-row segmentation algorithm

There is a number in the array that appears more than half the length of the array. If this array is sorted, then the number in the middle of the array after sorting must be the number that appears more than half the length of the array.

That is, this number is a statistically significant median, that is, the number of N/2 in an array of length n. We have a mature O (n) algorithm to get the number of any K-large in the array.

This algorithm is inspired by the fast sorting algorithm, in the random fast row, the first random selection of a number, and then the smaller than its number is placed on its left, bigger than it is placed on its right.

If a position is adjusted, the subscript of the selected number is exactly N/2, then the number is the median in the array.

If its subscript is greater than N/2, then the median should be on its left side, which can then be found on its left, and the subscript is less than N/2, which is a typical recursive process.

Of course, there are cases where the input is not valid (the array pointer is null), or if the most frequently occurring number in the input array does not reach more than half the length of the array.

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h>using namespace std;void change (int numbers[],int i,int j) {int temp=numbers[i];numbers[i]=numbers[j];numbers[j]=temp;} int partion (int numbers[],int length,int Start,int end) {if (start==end) return start;int temp=numbers[end];int I=start; int J=end;while (I&LT;J) {while (i<j&&numbers[i]<=temp) I++;change (NUMBERS,I,J), while (i<j& &numbers[j]>=temp) J--;change (numbers,i,j);} return i;} int morethanhalf (int numbers[],int length) {int start=0;int end=length-1;int mid= (length-1)/2;int index=partion ( Numbers,length,start,end), while (Index!=mid) {if (Index<mid) {start=index+1;end=end;index=partion (numbers, Length,start,end);} Else{start=start;end=index-1;index=partion (Numbers,length,start,end);}} return Numbers[index];} BOOL Ifright (int numbers[],int length,int re) {int times=0;for (int i=0;i<length;i++) if (numbers[i]==re) times++;if ( Times>= (LENGTH/2)) return 1;elsereturn 0;}

  

Solution Two: Find an O (n) algorithm based on the array characteristics

There is a number in the array that occurs more than half the length of the array, which means that it appears more often than any other number .

So we can consider using two variables: one to save a number, and one to save the number of times.

At the beginning, save the first element in the array, set the number of times to 1;

To iterate over an array:

If the next digit is the same as the previous saved number, the number is incremented by 1;

If the next number is different from the previously saved number, the number is decremented by 1;

If the number is zero, we need to save the next number and set the number to 1.

Since the number we are looking for appears to be more than the sum of the number of other numbers appearing, the number to be searched is definitely the last number to be set at 1 o'clock.

But in the end, you need to check to see if the number is more than half the length of the array, because it is possible that the array does not contain such a number.

int morethanhalf2 (int numbers[],int length) {int re=numbers[0];int num=1;for (int i=1;i<length;i++) {if (num==0) {re= numbers[i];num++;} Else{if (Re==numbers[i]) num++;elsenum--;} return re;} int main () {int ary[5]={1,2,2,1,1};cout<<morethanhalf (ary,5) <<endl;//cout<<partion (ary,4,0,3) <<endl;system ("Pause");}

  

Sword offer--face question 29: Number of occurrences in the array 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.