[algorithm] finds the number of occurrences greater than n/k in the array

Source: Internet
Author: User

Title:

1. Given an array of integers, print the number of occurrences greater than half. If no such number appears, print the prompt message.

such as: 1,2,1 Output 1. The such number is output No.

2. Given an integer array, give an integer k, print all occurrences greater than n/k, and if there is no such number, print the hint.

Answer:

Both questions can use a hash table to record the number of occurrences of each number, with an additional space complexity of O (N).

Other methods:

1. time complexity is O (N). Additional space complexity is O (1).

 Public Static voidPrinthalfmajor (int[] arr) {        intCand = 0; intTimes = 0;  for(inti = 0; i < arr.length; i++) {            if(Times = = 0) {Cand=Arr[i]; Times++; } Else if(Arr[i] = =cand) { times++; } Else{ times--; }} times= 0;  for(inti = 0; i < arr.length; i++) {            if(Arr[i] = =cand) times++; }        if(Times > ARR.LENGTH/2) {System.out.println (cand); } Else{System.out.println ("No such number."); }    }

At the end of the first for loop, if such a number really exists, then the value of Cand is the value you are seeking. But the value of cand is not necessarily the value that is being asked. So finally, you have to use the For loop to judge.

2. time complexity is O (n*k). Additional space complexity is O (K).

 Public Static voidPrintkmajor (int[] arr,intK) {if(K < 2) {System.out.println ("The value of K is invalid."); return; } HashMap<integer, integer> cands =NewHashmap<integer, integer>();  for(inti = 0; I! = arr.length; i++) {            if(Cands.containskey (Arr[i])) {cands.put (Arr[i], Cands.get (arr[i))+ 1); } Else {                if(cands.size () = = K-1) {allcandsminusone (cands); } Else{cands.put (arr[i],1); } }} HashMap<integer, integer> reals =getreals (arr, cands); BooleanHasprint =false;  for(Entry<integer, integer>Set:cands.entrySet ()) {Integer key=Set.getkey (); if(Reals.get (key) > Arr.length/K) {hasprint=true; System.out.print (Key+ " "); }} System.out.println (Hasprint? "": "No such number."); }     Public Static voidAllcandsminusone (Hashmap<integer, integer>map) {List<Integer> removelist =NewLinkedlist<integer>();  for(Entry<integer, integer>Set:map.entrySet ()) {Integer key=Set.getkey (); Integer value=Set.getvalue (); if(Value = = 1) {Removelist.add (key); } map.put (key, Value-1); }         for(Integer removekey:removelist) {map.remove (RemoveKey); }    }     Public StaticHashmap<integer, Integer> getreals (int[] arr, HashMap<integer, integer>cands) {HashMap<integer, integer> reals =NewHashmap<integer, integer>();  for(inti = 0; I! = arr.length; i++) {            intCurnum =Arr[i]; if(Cands.containskey (curnum)) {if(Reals.containskey (curnum)) {reals.put (Curnum, Reals.get (curnum)+ 1); } Else{reals.put (Curnum,1); }            }        }        returnreals; }

[algorithm] finds the number of occurrences greater than n/k 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.