Look for numbers that appear more than half the number in the array

Source: Internet
Author: User

The  "title" Array has a number that appears more than half the length of the array to find the number.  "Idea 1" because the number of numbers to look for is more than half the length of the array, so if the array is sorted, then its median is necessarily the number we are looking for, so our task is to sort the array, and the performance of the best quick sort with the time complexity O (nlogn  , we can do it directly with the library function, because its efficiency is not very high, so we will not repeat it here. "Idea 2" what is the fastest way to find the number of elements in an array? Of course the hash table!  Can we build a hash table and think a little bit, and we'll find out that the hash table only applies to the smaller range of elements, assuming that our array is an integer array, the range of values is too large, so it's not suitable for a hash table, too wasted space. Wait a minute, with a hash table, we don't seem to need the condition that the number appears more than half the length of the array? So much for this condition, what can we think of? This number exceeds the sum of the number of occurrences of all other numbers, so if we use a key value to record the number in the array, and then we use a value to record the number of occurrences of the number, and then add: Continue to traverse all the remaining numbers, if the number is equal, the number of times plus 1; , the number is reduced by 1, and if the number of occurrences is 0, then we replace it with the next number and reset the number of occurrences to 1. So the last number left is definitely the number of occurrences more than half the length of the array. Well, we can easily write down the following code in this discussion:   1#include <iostream>2#include <string>3 using namespacestd;4  5 //Global variables to check if the input is valid 6 BOOLInvalidinput =false; 7  8 /************************************************************ 9/* Find out the number of occurrences in the array more than half the length of the array 10/********************* ***************************************/ One intNumberappearmorethanhalf (int* numbers,unsignedintlength) A { -     if(numbers = = NULL | | length <=0) -     { theInvalidinput =true; -         return 0; -     } -  +Invalidinput =false; -     intKey = numbers[0]; +UnsignedintAppeartimes =1; A      for(inti =1; I < length;++i) at     { -         if(Appeartimes = =0) -         { -Key =Numbers[i]; -Appeartimes =1; -         } in  -         if(Numbers[i] = =key) toappeartimes++; +         Else -appeartimes--; the     } *      $     //Verify that the input array is a number that satisfies the criteriaPanax NotoginsengAppeartimes =0; -      for(i =0; i < length; i++) the     { +         if(Numbers[i] = =key) Aappeartimes++; the     } +  -     if(Appeartimes <= Length/2) $     { $Invalidinput =true; -         return 0; -     } the      -     returnkey;Wuyi } the  - intMain () Wu { -cout<<"Enter The length of your array:"<<Endl; About     intArraylength =0; $Cin>>arraylength; -  -cout<<"Enter The elements of your array:"<<Endl; -     int*array =New int[arraylength]; A      for(intK =0; K < arraylength;k++) +     { theCin>>Array[k]; -     } $  thecout<<"The number appear more than half length of your array is:"<<Endl; theCout<<numberappearmorethanhalf (array,arraylength) <<Endl; the      thedelete[] array; -      in     return 0; the}
Reprinted from: http://www.cnblogs.com/python27/archive/2011/12/15/2289534.html

Look for numbers that appear more than half the number in 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.