wu :: forums
« wu :: forums - 4 members of sum z in hash table »

Welcome, Guest. Please Login or Register.
Dec 4th, 2024, 1:04am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: ThudnBlunder, william wu, towr, SMQ, Icarus, Eigenray, Grimbal)
   4 members of sum z in hash table
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: 4 members of sum z in hash table  (Read 1243 times)
transgalactic
Newbie
*





   


Posts: 11
4 members of sum z in hash table  
« on: Jan 15th, 2012, 7:22am »
Quote Quote Modify Modify

i have a set S of n members and a number Z
i need to find 4 different members which sum is Z
and the time complexity is theta(n^2)
?
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: 4 members of sum z in hash table  
« Reply #1 on: Jan 15th, 2012, 10:02am »
Quote Quote Modify Modify

Use a hashmap mapping A*B A+B to the pair (A,B)
Then for each A*B A+B in the table find Z/(A*B) =C*D Z-(A+B) =C+D and see if A,B,C and D are distinct.
 
i.e. same thing as http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs; action=display;num=1325343356 but using a hash-table.
« Last Edit: Jan 15th, 2012, 12:35pm by towr » IP Logged

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





   


Posts: 11
Re: 4 members of sum z in hash table  
« Reply #2 on: Jan 15th, 2012, 11:59am »
Quote Quote Modify Modify

i cant understand the meaning of
"Use a hashmap mapping A*B to the pair (A,B)"
 
you want me to create all the combinations of
multiplications  via double loop.
then you want me o hash them
 
what does the function pair(A,B) does?
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: 4 members of sum z in hash table  
« Reply #3 on: Jan 15th, 2012, 12:34pm »
Quote Quote Modify Modify

Oh wait, I seem to have misread somewhere, you want to sum, not multiply.  
Well, anyway. You want distinct numbers, right. So you need to find two pairs of numbers that sum to Z: i.e. (A+B) + (C+D) = Z
By using a hashtable that maps A+B back to the pair of numbers A and B, you can iterate over the n*(n-1)/2 sums of two numbers, and find which pair of numbers give that sum, that way you can check all four are distinct.
« Last Edit: Jan 15th, 2012, 12:36pm by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
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