wu :: forums
« wu :: forums - find the common element »

Welcome, Guest. Please Login or Register.
Nov 28th, 2024, 10:28am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: william wu, Grimbal, Icarus, towr, ThudnBlunder, SMQ, Eigenray)
   find the common element
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: find the common element  (Read 724 times)
RamRaul
Newbie
*





  rahul7132003  


Gender: male
Posts: 5
find the common element  
« on: Feb 2nd, 2010, 4:23am »
Quote Quote Modify Modify

You have 3 arrays containing 200 integers each. Given that there is one and only one common element in these 3 arrays. Find the common element.
 
any suggestions how to proceed Huh
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: find the common element  
« Reply #1 on: Feb 2nd, 2010, 4:29am »
Quote Quote Modify Modify

You could sort the arrays, then do a traversal where you increment the index into an array if its corresponding value is smaller than the value at the indices of the other two array.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: find the common element  
« Reply #2 on: Feb 2nd, 2010, 5:47am »
Quote Quote Modify Modify

... smaller than one of the values at the indices of the other two array.
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: find the common element  
« Reply #3 on: Feb 2nd, 2010, 5:59am »
Quote Quote Modify Modify

Hmm, yeah, that's better. Otherwise you can get stuck when the two lowest ones are equal.
I was thinking of just always incrementing the least value, but then I should have phrased it differently.
« Last Edit: Feb 2nd, 2010, 6:01am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
RamRaul
Newbie
*





  rahul7132003  


Gender: male
Posts: 5
Re: find the common element  
« Reply #4 on: Feb 3rd, 2010, 4:24am »
Quote Quote Modify Modify

@towr:thank u..thats workin
 
     
what i was thinking was  to hash two of the arrays and run lookups from the third, which takes  O(n) steps and O(n log n) bits of space.  
     bt i dont think that dere's  improvement in complexity than the method u explained!!!!!!!
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: find the common element  
« Reply #5 on: Feb 3rd, 2010, 4:38am »
Quote Quote Modify Modify

If the arrays were very large (much, much larger than 200 integers), than using hashing would probably be more time efficient. Just as long as the hashing/lookup takes less than O(log(n)) time per item.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
bmudiam
Junior Member
**





   


Gender: male
Posts: 55
Re: find the common element  
« Reply #6 on: Feb 8th, 2010, 7:21pm »
Quote Quote Modify Modify

If the elements are all prime numbers, then you can actually multiply the elements in first array, then second array and then third array and see if there is any common factor of a *b and b *c.
 
Can we make this solution work for non-prime number  s too?
IP Logged

“Nobody can go back and start a new beginning, but anyone can start today and make a new ending.” - Maria Robinson
Obob
Senior Riddler
****





   


Gender: male
Posts: 489
Re: find the common element  
« Reply #7 on: Feb 8th, 2010, 8:18pm »
Quote Quote Modify Modify

If the integers are all positive, say the integers in the first array are a_i, then you can take the product of the numbers p(a_i), where p(a) is the a-th prime number.  Then take the GCD of the three numbers obtained in this way to get a prime, and figure out which prime it is to get the answer.  You can easily adapt this to all integers.  This is clearly a bad way to do it though, and you'll have overflow errors very quickly.
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board