-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
57 lines (49 loc) · 1.58 KB
/
test.cpp
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
#include <bits/stdc++.h>
using namespace std;
vector<int> allIndex(vector<int> a,int target, int start)
{
// If the start index reaches the
// length of the array, then
// return empty array
if (start == a.size()) {
vector<int> ans; // empty array
return ans;
}
// Getting the recursive answer in
// smallIndex array
vector<int> smallIndex = allIndex(a, target, start + 1);
// If the element at start index is equal
// to target then
// (which is the answer of recursion) and then
// (which came through recursion)
if (a[start] == target) {
vector<int> result(smallIndex.size()+1);
// Put the start index in front
// of the array
result[0] = start;
for (int i = 0; i < smallIndex.size(); i++) {
// Shift the elements of the array
// one step to the right
// and putting them in
// result array
result[i + 1] = smallIndex[i];
}
return result;
}
else {
// If the element at start index is not
// equal to x then just simply return the
// answer which came from recursion.
return smallIndex;
}
}
int main()
{
vector<int> a={1,2,3,2,2,5};
int target=2;
vector<int> output = allIndex(a, target, 0);
// Printing the output array
for (int i = 0; i < output.size(); i++) {
cout<<output[i]<<" ";
}
}