@All: Please pay attention to CodeHunter's answer. This is a standard Nuts 
and Bolts problem.
For those of you that are not aware, the Nuts and Bolts problem is as 
follows:

Given an array of nuts and another array of bolts, we need to match up all 
the nuts with all the bolts. 
The constraint is that a bolt cannot be compared to another bolt except by 
comparing their sizes with
respect to some nut. (A similar constraint on nuts vis a vis bolts).

The solution to this problem can be done in O(N log N) using a variant of 
quicksort.
The algorithm is as follows:

1. Select a random nut from the nuts array as the partition element
2. Partition the bolts array into 3 parts based on the nut: ||< nut|| == nut 
|| > nut||
3. Select a bolt from the == nut array
4. Partition the nuts array into 3 parts based on the bolt: ||< bolt|| == 
bolt || > bolt||
5. Recurse from step 1 on the two subarrays.

Expected Time Complexity: O(N log N)
Extra Space Required: O(1)

--
DK

http://gplus.to/divyekapoor
http://twitter.com/divyekapoor
http://www.divye.in

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/bsBtugahyawJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to