-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathNut and Bolt Matching Problem.cpp
More file actions
122 lines (112 loc) · 2.6 KB
/
Nut and Bolt Matching Problem.cpp
File metadata and controls
122 lines (112 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Nut and Bolt Matching Problem based on QuickSort Approach - Google Interview
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std ;
class Bolt; //Forward Declaration
class Nut{
int Size;
public:
Nut(int Size){
this->Size = Size;
}
int getSize(){
return this->Size;
}
friend int::compare(const Nut*,const Bolt*);
friend int::compare(const Bolt*,const Nut*);
};
class Bolt{
int Size;
public:
Bolt(int Size){
this->Size = Size;
}
int getSize(){
return this->Size;
}
friend int::compare(const Nut*,const Bolt*);
friend int::compare(const Bolt*,const Nut*);
};
// Return -1 Nut < Bolt , 0 equal , else 1
int compare(const Nut* n,const Bolt*b){
if( n->Size < b->Size)
return -1;
else if(n->Size==b->Size)
return 0;
else
return 1;
}
//Return -1 Bolt < Nut , 0 equal , else 1
int compare(const Bolt*b,const Nut*n){
if( b->Size < n->Size)
return -1;
else if(n->Size==b->Size)
return 0;
else
return 1;
}
//Since we need two types of partition function , hence better to use a template
template<typename X,typename Y>
int partition(X** a,int low,int high,Y*b){
int i,j,k;
i = low - 1;
for(j=low;j<high;j++){
int c = compare(a[j],b);
// a < b
if(c<0){
i++;
X* temp = a[i] ; a[i] = a[j] ; a[j] = temp;
}
else if(c==0){
// Move it to the last for future use
X*temp = a[j] ; a[j] = a[high] ; a[high] = temp;
j--;
}
}
//Bring it back to the i+1 th position
X*temp = a[i+1] ; a[i+1] = a[high] ; a[high] = temp;
return (i+1);
}
void matchNutsBolts(Nut **nuts,Bolt **bolts,int low,int high){
//There must be atleast 2 nuts and bolts to sort , hence the condition low<high.
if(low<high){
// Choose a random Nut as pivot in quickSort and Sort the bolts .
int nutIndex = low + (rand()%(high-low+1));
Nut *chosenNut = nuts[nutIndex];
int matchedBoltIndex = partition(bolts,low,high,chosenNut);
partition(nuts,low,high,bolts[matchedBoltIndex]);
matchNutsBolts(nuts,bolts,low,matchedBoltIndex-1);
matchNutsBolts(nuts,bolts,matchedBoltIndex+1,high);
}
return ;
}
int main(){
int n,i;
cout<<"Enter the Number of Nuts and Bolts : ";
cin>>n;
Nut **nuts;
Bolt **bolts;
cout<<"Enter Nut Sizes ,then Bolt Sizes :"<<endl;
nuts = new Nut*[n];
bolts = new Bolt*[n];
int s;
for(i=0;i<n;i++){
cin>>s;
nuts[i] = new Nut(s);
}
for(i=0;i<n;i++){
cin>>s;
bolts[i] = new Bolt(s);
}
matchNutsBolts(nuts,bolts,0,n-1);
cout<<"BOLTS : ";
for(i=0;i<n;i++){
cout<<bolts[i]->getSize()<<" ";
}
cout<<endl<<"NUTS : ";
for(i=0;i<n;i++){
cout<<nuts[i]->getSize()<<" " ;
}
return 0;
}