With First look you think practical way of solving this question, and that is randomly taking a bolt and finding (by Looking at all nuts) perfect fit nut for it.
but does such logic to find approximate fit nut doing hit and try is possible in simple coding practice ?
No , so in order to achieve that what can be simple ways
Method 1) Brute Force Approach
Take a nut and find its pair bolt in all Bolts.
Complexity : O(n^2)
Method 2) Simple and Efficient
Sort all bolts and all nuts based upon diameter .. All pairs will be automatically formed.
Nuts[i] <-> Bolt[i]. (as same diameter bolt and nut will fit )
But this will be inefficient as question is asked with least comparisons .
here comparisons will increase (as You are sorting Nuts and Bolts both)
with less sorting complexity also it can be done if you use non-comparison based methods.
Method 3) With help of Map.
Algorithm :
1) Create a Map of <diameter, Address of Bolt>
2) For all N nuts.
check diameter and search in map.
if diameter matches = declare them both as pair
else print “no bolt for this nut”
3) end
With this simple algo you can find all pairs of nuts and bot in N comparisons only.