Algorithm 22: Search for the number that appears more than half of the number in the array.

Source: Internet
Author: User

[Topic] a number in the array appears more than half the length of the array.

[Thu 1] Because the number to be searched appears more than half the length of the array, if you sort the array, the median must be the number we are looking, therefore, our task is to sort the array, and the time complexity of the best and fast sorting is O (nlogn). We can directly use the library function, because the efficiency is not very high, so I will not go into details here.

[Thinking 2] What is the fastest way to find the number of elements in an array? Of course, the table is hashed! Can we create a hash table? If we think about it, we can find that the hash table only applies when the element range is small, and assume that our array is an integer array, the value range is too long, so it is not suitable for hash tables, which is a waste of space.

Wait a moment. If a hash table is used, we do not need the condition that the number of occurrences of this number exceeds half of the length of the array? So what can we think of when there are so many such conditions? The number of occurrences of this number exceeds the sum of the occurrences of all other numbers. Therefore, if we use a key value to record the numbers in the array, then use a value to record the occurrences of this number, and then accumulate: continue to traverse all the remaining numbers. If it is equal to this number, add 1 to the number of times. If it is different from this number, then the number of times of the number is reduced by 1; if the number of occurrences of a number is 0, we will replace it with the next number and reset the occurrence to 1. In this way, the remaining number is definitely the number that appears more than half the length of the array. Now, we can easily write the followingCode:

 1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 // Global variable to check whether the input is valid
6 Bool Invalidinput = False ;
7
8 /* **************************************** *******************
9 /* Find the number in the array that appears more than half the length of the array.
10 /*************************************** ******************** */
11 Int Numberappearmorethanhalf ( Int * Numbers, unsigned Int Length)
12 {
13 If (Numbers = NULL | length <= 0 )
14 {
15 Invalidinput = True ;
16 Return 0 ;
17 }
18
19 Invalidinput = False ;
20 Int Key = numbers [ 0 ];
21 Unsigned Int Appeartimes = 1 ;
22 For ( Int I = 1 ; I <length; ++ I)
23 {
24 If (Appeartimes = 0 )
25 {
26 Key = numbers [I];
27 Appeartimes = 1 ;
28 }
29
30 If (Numbers [I] = key)
31 Appeartimes ++;
32 Else
33 Appeartimes --;
34 }
35
36 // Check that the input array contains numbers that meet the conditions.
37 Appeartimes = 0 ;
38 For (I = 0 ; I <length; I ++)
39 {
40 If (Numbers [I] = key)
41 Appeartimes ++;
42 }
43
44 If (Appeartimes <= length/ 2 )
45 {
46 Invalidinput = True ;
47 Return 0 ;
48 }
49
50 Return Key;
51 }
52
53 Int Main ()
54 {
55 Cout < " Enter the length of your array: " <Endl;
56 Int Arraylength = 0 ;
57 Cin> arraylength;
58
59 Cout < " Enter the elements of your array: " <Endl;
60 Int * Array = New Int [Arraylength];
61 For ( Int K = 0 ; K <arraylength; k ++)
62 {
63 Cin> array [k];
64 }
65
66 Cout < " The number appear more than half length of your array is: " <Endl;
67 Cout <numberappearmorethanhalf (array, arraylength) <Endl;
68
69 Delete [] array;
70
71 Return 0 ;
72 }

The running result is as follows:

 

References:

ProgramMember interview questions featured 100 questions: http://zhedahht.blog.163.com/blog/static/25411174201085114733349/

Note:

1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.

2) BloggerPython27This blogArticleCopyright. For Network reprinting, please indicate the sourceHttp://www.cnblogs.com/python27/. You have any suggestions for solving the problem. Please feel free to comment on them.


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.