Nuts and Bolts Problem | Practice | GeeksforGeeks

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.
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.
The elements should follow the following order ! # $ % & * @ ^ ~ .
Example 1:
Input: 
N = 5
nuts[] = {@, %, $, #, ^}
bolts[] = {%, @, #, $ ^}
Output: 
# $ % @ ^
# $ % @ ^

Example 2:
Input: 
N = 9
nuts[] = {^, &, %, @, #, *, $, ~, !}
bolts[] = {~, #, @, %, &, *, $ ,^, !}
Output: 
! # $ % & * @ ^ ~
! # $ % & * @ ^ ~

Your Task:  
You don't need to read input or print anything. Your task is to complete the function matchPairs() which takes an array of characters nuts[], bolts[] and n as parameters and returns void. You need to change the array itself.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 9
Array of Nuts/Bolts can only consist of the following elements:{'@', '#', '$', '%', '^', '&', '~', '*', '!'}.
 
0
jrm6lrcrp3lcpybdms0n4rk0xpecvc73ulg3nna31 month ago
python code 0.0/1.6  time taken:
 
def matchPairs(self,nuts, bolts, n): # code here s=['!','#', '$','%', '&', '*' ,'@' ,'^' ,'~'] d=0 for i in s:     if i in nuts:         nuts.remove(i)         bolts.remove(i)         nuts.insert(d,i)         bolts.insert(d,i)         d+=1
 
 
0
artistdarkangel1 month ago
Java code : O(N) time complexity O(1) Space complexity. All test cases pass.
class Solution {   void matchPairs(char nuts[], char bolts[], int n) {
       HashMap<Character, Integer> hm = new HashMap<Character, Integer>();       hm.put('!',0);       hm.put('#',1);       hm.put('$',2);       hm.put('%',3);       hm.put('&',4);       hm.put('*',5);       hm.put('@',6);       hm.put('^',7);       hm.put('~',8);              HashMap<Integer, Character> hm2 = new HashMap<Integer, Character>();       hm2.put(0,'!');       hm2.put(1,'#');       hm2.put(2,'$');       hm2.put(3,'%');       hm2.put(4,'&');       hm2.put(5,'*');       hm2.put(6,'@');       hm2.put(7,'^');       hm2.put(8,'~');                     int temp[] = new int[9];              for(int i=0; i<n; i++)       {           temp[hm.get(nuts[i])]=1;       }              int k=0;       for(int i=0; i<9; i++)       {           if(temp[i]==1)           {               nuts[k]=hm2.get(i);               bolts[k]=nuts[k];               k++;           }       }          }}
0
ayazmrz981 month ago
def matchPairs(self,nuts, bolts, n):
 nuts.sort()
 bolts.sort()
0
satyapani9991 month ago
Simple Hashset approach... 0.2/1.5
 
class Solution {   void matchPairs(char nuts[], char bolts[], int n) {       // code here   HashSet<Character> set = new LinkedHashSet<>();   set.add('!');   set.add('#');   set.add('$');   set.add('%');   set.add('&');   set.add('*');   set.add('@');   set.add('^');   set.add('~');   HashSet<Character> hs= new HashSet<>();   for(int i=0;i<n;i++)   {       hs.add(nuts[i]);   }   int j=0;   for(char i:set)   {       if(j<n && hs.contains(i))       {           nuts[j] = i;           bolts[j] = i;           j++;       }   }   }}
0
yashkotalwar101 month ago
//JAVA
// 1
HashMap<Character, Integer> rule = new HashMap<>();       rule.put('!',0);       rule.put('#',1);       rule.put('$',2);       rule.put('%',3);       rule.put('&',4);       rule.put('*',5);       rule.put('@',6);       rule.put('^',7);       rule.put('~',8);              char ch;       for(int i=0; i<n-1; i++){           for(int j = i+1; j<n; j++){               if(rule.get(nuts[i])>rule.get(nuts[j])){                   ch = nuts[i];                   nuts[i] = nuts[j];                   nuts[j] = ch;               }               if(rule.get(bolts[i])>rule.get(bolts[j])){                   ch = bolts[i];                   bolts[i] = bolts[j];                   bolts[j] = ch;               }           }                  }
 
// 2
Arrays.sort(nuts);
Arrays.sort(bolts);
0
narendranathmaddikeri20072 months ago
#JAVA CODE
class Solution {   void matchPairs(char nuts[], char bolts[], int n) {         Arrays.sort(nuts);        Arrays.sort(bolts);   }}
0
mohammadtanveer75402 months ago
class Solution{
public:	
	void matchPairs(char nuts[], char bolts[], int n) {
	    sort(nuts, nuts+n); sort(bolts, bolts+n);
	}
};
-1
mdsaif04052 months ago
class Solution{
public:	
  void matchPairs(char nuts[], char bolts[], int n) 
  {
        vector<char>v1;
        vector<char>v2;
        sort(nuts,nuts+n);
        sort(bolts,bolts+n);
  }
};
+1
shineinsa2 months ago
#include<vector>class Solution{public:
void matchPairs(char nuts[], char bolts[], int n) {    // code here// int m=nuts.length();// int n=bolts.length();vector<char>v1;vector<char>v2;sort(nuts,nuts+n);sort(bolts,bolts+n);for(int i=0;i<n;i++){    if(nuts[i]==bolts[i]){        v1.push_back(nuts[i]);        v2.push_back(bolts[i]);    }}
}
};
0
sharmasheli222 months ago
//JAVASCRIPT 
 
//const nuts = [ '@','%','$','#','^'];
//const bolts = ['%','@','#','$','^'];
//let a = ['!#$%&*@^~'];
let nut = nuts.sort();
let bolt = bolts.sort();
console.log(nut);
console.log(bolt);
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.