-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockMerchent.cpp
More file actions
31 lines (26 loc) · 870 Bytes
/
sockMerchent.cpp
File metadata and controls
31 lines (26 loc) · 870 Bytes
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
#include <vector>
#include <algorithm>
using namespace std;
int sockMerchant(int n, vector<int> ar) {
vector<int> arrayOfSocks;
int noOfPairs = 0;
for (int i = 0; i < n; ++i) {
if (std::find(arrayOfSocks.begin(), arrayOfSocks.end(), ar[i]) != arrayOfSocks.end()) {
arrayOfSocks.erase(remove(arrayOfSocks.begin(), arrayOfSocks.end(), ar[i]), arrayOfSocks.end());
noOfPairs++;
} else {
arrayOfSocks.push_back(ar[i]);
}
}
return noOfPairs;
}
// Info: Testcases Worked
//int main() {
// vector<int> socks = {1,1,2,2,4,4,5,6,3,1,2};
// cout << sockMerchant(6 ,socks);
// return 0;
//}
/*
There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock,
determine how many pairs of socks with matching colors there are.
*/