Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently.

**Constraint:** Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.

Examples:

Input : nuts[] = {'@', '#', '$', '%', '^', '&'} bolts[] = {'$', '%', '&', '^', '@', '#'} Output : Matched nuts and bolts are- $ % & ^ @ # $ % & ^ @ #

Other way of asking this problem is, given a box with locks and keys where one lock can be opened by one key in the box. We need to match the pair.

We have discussed a sorting based solution in below post.

Nuts & Bolts Problem (Lock & Key problem) | Set 1

In this post, hashmap based approach is discussed.

- Travese the nuts array and create a hashmap
- Traverse the bolts array and search for it in hashmap.
- If it is found in the hashmap of nuts than this means bolts exist for that nut.

`// Hashmap based solution to solve ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to match nuts and bolts ` `void` `nutboltmatch(` `char` `nuts[], ` `char` `bolts[], ` `int` `n) ` `{ ` ` ` `unordered_map<` `char` `, ` `int` `> hash; ` ` ` ` ` `// creating a hashmap for nuts ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `hash[nuts[i]] = i; ` ` ` ` ` `// searching for nuts for each bolt in hash map ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `if` `(hash.find(bolts[i]) != hash.end()) ` ` ` `nuts[i] = bolts[i]; ` ` ` ` ` `// print the result ` ` ` `cout << ` `"matched nuts and bolts are-"` `<< endl; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `cout << nuts[i] << ` `" "` `; ` ` ` `cout << endl; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `cout << bolts[i] << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `char` `nuts[] = {` `'@'` `, ` `'#'` `, ` `'$'` `, ` `'%'` `, ` `'^'` `, ` `'&'` `}; ` ` ` `char` `bolts[] = {` `'$'` `, ` `'%'` `, ` `'&'` `, ` `'^'` `, ` `'@'` `, ` `'#'` `}; ` ` ` `int` `n = ` `sizeof` `(nuts) / ` `sizeof` `(nuts[0]); ` ` ` `nutboltmatch(nuts, bolts, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

matched nuts and bolts are- $ % & ^ @ # $ % & ^ @ #

Time complexity for this solution is O(n).

This article is contributed by **Niteesh kumar**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Nuts & Bolts Problem (Lock & Key problem) | Set 1
- Store duplicate keys-values pair and sort the key-value pair by key
- Find four elements that sum to a given value | Set 3 (Hashmap)
- Internal Working of HashMap in Java
- Load Factor in HashMap in Java with Examples
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- Probability of a key K present in array
- Check if a key is present in every segment of size k in an array
- Check if two given key sequences construct same BSTs
- Find the value mapped to a given composite key for every query
- Delete all occurrences of a given key in a linked list
- Print equal sum sets of array (Partition problem) | Set 1
- Print equal sum sets of array (Partition Problem) | Set 2
- The Knight's tour problem | Backtracking-1
- N Queen Problem | Backtracking-3
- 0-1 Knapsack Problem | DP-10
- Activity Selection Problem | Greedy Algo-1
- Box Stacking Problem | DP-22
- Subset Sum Problem | DP-25
- The Stock Span Problem