views:

164

answers:

4

You have an array size n and a constant k (whatever)

You can assume the the array is of int type (although it could be of any type)

Describe an algorithm that finds if there is an element(s) that repeats itself at least n/k times... if there is return one. Do so in linear time (O(n))

The catch: do this algorithm (or even pseudo-code) using constant memory and running over the array only twice

+6  A: 
erickson
A: 

A simple O(n) algorithm would be to keep a hashed map from the number found to the number of instances found. Using a hashed map is important for maintaining O(n). The, a final pass over the map will reveal the answers. This pass is also O(n) since the worst case scenario is that every element appeared only once and hence the map is the same size as the original array.

dty
I believe this violates the "constant memory" constraint
John Rasch
That's O(n) memory consumption, not O(1).
erickson
This doesn't meet the constant space requirement.
Jim Lewis
Sorry, I read the "constant memory" requirement as being a "bonus marks" question... :-)
dty
Also hash-tables are O(1) amoritized insert/lookup, but O(n) worst-case.
BlueRaja - Danny Pflughoeft
...which maintains the (imagined!) O(n) constraint! Plus, you've got to be pretty pathological to make a hash lookup O(n).
dty
A: 

I don't know if you are restricted on which additional data structures you can use.

What about creating a hashmap with 'elements' <--> count mapping. Insertion is O(log N). Lookup is O(1). For each element, lookup on hashtable, insert if does not exist with count 1. If exists, check if count < (n/k). It will stay O(n).

EDIT:

I forgot the constant memory restriction. It's preallocating hash map entries with N elements permitted?

Hernán
@Hernan, no that would be O(n) memory consumption.
VeeArr
Thank you VeeArr for clearing that.
Hernán
even without the memory consumption...what whould your hashtable size be?Lookup is O(1) in avarage case ,we should deal with worst case...nah I dont think it's has anything to do with hash
A: 

thanks for the quick respond... I knew it will raise more question so let me give some example for instance

41 25 347 352 24 57 24 -12 64 32 25 43 12 41 25 - is our array k lets say is 5 what leads to 15/5=3 we have number "25" so 25 should be returned

another one -3 23 12 64 23 12 12 -3 23 = array.length =9 k=3 so 9/3=3 23/12 should be returned.

"So what have you tried and what specifically are you having trouble with? – Chris Dunaway" trouble will lets say that we all know how to make it in o(n) but how to make it by running over the array only twice :(

"By "repeat itself," do you mean it should be a consecutive run of the same number?" same number :) see examples above please.

"Constant memory per array, or constant memory per element?" dont understand the question :( sorry

"Sorry, I read the "constant memory" requirement as being a "bonus marks" question... :-) " as a matter of fact it's a bonus question the memory requirement.

"I'm not going to provide source code" I didnt ask for any code... but a direction would be nice after all nameing the people who deal with this issue isnt realy helpful.A link to the article would be nice...thanks in advance!
I realy don't get why not just to post the source to the article?!?!Advanced searchAbout 3,360,000 results (0.17 seconds) why not to save me the time and point out so I would understand what page 4 you meant to... otherwise I dont see any point to mention that...
I *did* provide a link to the article so that you could read the whole thing if you needed more context. But I also quoted the most important part of the article, which simply explains the algorithm to solve this problem, in my answer. There was no need to use "advanced search" (whatever that is). You could simply click the link in my answer. It really can't get any easier, short of writing the code for you.
erickson