-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinding_pairs.cpp
More file actions
59 lines (50 loc) · 1.49 KB
/
finding_pairs.cpp
File metadata and controls
59 lines (50 loc) · 1.49 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
//
// Created by Mayank Parasar on 2019-12-12.
//
#include <iostream>
#include <vector>
using namespace std;
using namespace std;
int num_pairs(int size, vector<int>& vec) {
int num_pairs = 0;
vector<int>::iterator ptr;
ptr = vec.begin();
while(ptr != vec.end()) {
// define another iterator 'ptr2' and loop through the end
vector<int>::iterator ptr2;
for( ptr2 = ptr+1; ptr2 < vec.end(); ptr2++) {
if (*ptr == *ptr2) {
num_pairs++;
// remove both of these elements
if(ptr == vec.end())
vec.pop_back();
else
vec.erase(ptr); // when you erase ptr autoomatically points to the next element
if(ptr2 == vec.end())
vec.pop_back();
else
vec.erase(ptr2);
// reset ptr to point to the next element
// ptr++;
// break
break;
}
}
// if the control comes here that there was no matching element.
// reset ptr to point to the next element
if (ptr2 == vec.end()) {
ptr++;
if (ptr == vec.end())
break;
}
}
return num_pairs;
}
int main () {
vector<int> a = {10,20,20,10,10,30,50,10,20};
vector<int> b = {10, 10, 10, 10, 10};
cout << num_pairs(a.size(), a);
cout << endl;
cout << num_pairs(b.size(), b);
return 0;
}