Skip to content

Commit 9ee63ae

Browse files
author
Ravi Mandliya
committed
Added find common in 3 vectors.
1 parent 669e734 commit 9ee63ae

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 135 |
10-
| Current Streak | 5 days |
9+
| Total Problems | 136 |
10+
| Current Streak | 6 days |
1111
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |
1212

1313
</center>
@@ -170,6 +170,7 @@ Include contains single header implementation of data structures and some algori
170170
| Given an array A of n elements, find three indices i, j and k such that A[i]^2 + A[j]^2 = A[K]^2. O(n2) time complexity and O(1) space complexity | [squareSum.cpp](sort_search_problems/squareSum.cpp)|
171171
| Given an unsorted array arr[0..n-1] of size n, find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted. |[minLengthUnsortedArray.cpp](sort_search_problems/minLengthUnsortedArray.cpp)|
172172
| Find the missing number in Arithmetic Progression | [missingNumber2.cpp](sort_search_problems/missingNumber2.cpp) |
173+
| Find the common elements in 3 sorted arrays | [commonIn3Arrays.cpp](sort_search_problems/commonIn3Arrays.cpp) |
173174
### Graph Problems
174175
| Problem | Solution |
175176
| :------------ | :----------: |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
void printCommonElements(const std::vector<int>& vec1,
5+
const std::vector<int>& vec2,
6+
const std::vector<int>& vec3)
7+
{
8+
std::vector<int>::const_iterator it1 = vec1.begin();
9+
std::vector<int>::const_iterator it2 = vec2.begin();
10+
std::vector<int>::const_iterator it3 = vec3.begin();
11+
12+
while (it1 != vec1.end() && it2 != vec2.end() && it3 != vec3.end())
13+
{
14+
if (*it1 == *it2 && *it2 == *it3)
15+
{
16+
std::cout << *it1 << " ";
17+
it1++;
18+
it2++;
19+
it3++;
20+
}
21+
else if (*it1 < *it2)
22+
{
23+
++it1;
24+
}
25+
else if (*it2 < *it3)
26+
{
27+
++it2;
28+
}
29+
else
30+
{
31+
++it3;
32+
}
33+
}
34+
std::cout << std::endl;
35+
}
36+
37+
38+
int main()
39+
{
40+
std::vector<int> vec1 {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
41+
std::vector<int> vec2 {3, 6, 9, 12, 15, 18, 21};
42+
std::vector<int> vec3 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
43+
44+
printCommonElements(vec1, vec2, vec3);
45+
return 0;
46+
}

0 commit comments

Comments
 (0)