Skip to content

Commit 7beb090

Browse files
authored
Nut and Bolt problem algorithm in C++
1 parent 87ace8a commit 7beb090

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

nut_bolt.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
void nutboltmatch(char nuts[], char bolts[], int n)
6+
{
7+
unordered_map<char, int> hash;
8+
9+
10+
for (int i = 0; i < n; i++)
11+
hash[nuts[i]] = i;
12+
13+
14+
for (int i = 0; i < n; i++)
15+
if (hash.find(bolts[i]) != hash.end())
16+
nuts[i] = bolts[i];
17+
18+
cout << "matched nuts and bolts are-" << endl;
19+
for (int i = 0; i < n; i++)
20+
cout << nuts[i] << " ";
21+
cout << endl;
22+
for (int i = 0; i < n; i++)
23+
cout << bolts[i] << " ";
24+
}
25+
26+
27+
int main()
28+
{
29+
char nuts[] = {'@', '#', '$', '%', '^', '&'};
30+
char bolts[] = {'$', '%', '&', '^', '@', '#'};
31+
int n = sizeof(nuts) / sizeof(nuts[0]);
32+
nutboltmatch(nuts, bolts, n);
33+
return 0;
34+
}

0 commit comments

Comments
 (0)