**Objective**: Given ‘n’ Nuts and ‘n’ Bolts of different sizes. There is one-to-one mapping between nuts and bolts. Write an algorithm to find all matches between nuts and bolts

**Note**: This problem can also be framed as- Given ‘n’ keys and ‘n’ locks. There is one-to-one mapping between keys and locks, means each lock has a specific key and can be unlocked using that key only. Write an algorithm to find all matches between keys and locks.

You are allowed to compare only nuts with bolts (or keys with locks), nuts with nuts or bolts with bolts comparisons are not allowed.

**Example**:

[] nuts = {'Approach:Brute Force– Compare each nut (or key) will all the bolts (or locks) till we do not find the match. Time Complexity: O(n^{2})Code:Use Hash Map:

- Insert all the nuts as key and its position as value into Hash Map.
- Iterate through all the bolts and check for the nuts in the hash map and put it into the right place.

**Code**:

**Output**:

(Hash Map) Matched nuts and bolts are: [%, @, x, $, &] [%, @, x, $, &]

Reference - http://www.geeksforgeeks.org/nuts-bolts-problem-lock-key-problem-set-2-hashmap/

, '%', '&', '*', 'x'}

[] bolts = {'%', 'x', '*', '**Approach**:

**Brute Force** –

Compare each nut (or key) will all the bolts (or locks) till we do not find the match.

Time Complexity: O(n^{2})

**Code**:

**Use Hash Map**:

- Insert all the nuts as key and its position as value into Hash Map.
- Iterate through all the bolts and check for the nuts in the hash map and put it into the right place.

**Code**:

**Output**:

Reference - http://www.geeksforgeeks.org/nuts-bolts-problem-lock-key-problem-set-2-hashmap/

, '&'}

**Output**:

Matched nuts and bolts are:

[$, %, &, *, x]

[$, %, &, *, x]

**Approach**:

**Brute Force** –

Compare each nut (or key) will all the bolts (or locks) till we do not find the match.

Time Complexity: O(n^{2})

**Code**:

**Use Hash Map**:

- Insert all the nuts as key and its position as value into Hash Map.
- Iterate through all the bolts and check for the nuts in the hash map and put it into the right place.

**Code**:

**Output**:

Reference – http://www.geeksforgeeks.org/nuts-bolts-problem-lock-key-problem-set-2-hashmap/