fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void findBoth(int i, const vector<int>& a, int tar, vector<int>&ans) {
  7. // Base Case
  8. if (i == a.size()) {
  9. return;
  10. }
  11.  
  12. if (a[i] == tar) {
  13. // Set first only if it hasn't been set yet
  14. ans.push_back(i);
  15. }
  16.  
  17. // Recurse to next index
  18. findBoth(i + 1, a, tar,ans);
  19. }
  20.  
  21. int main() {
  22. int n, tar;
  23. cin >> n >> tar;
  24.  
  25. vector<int> a(n);
  26. for (int i = 0; i < n; i++) {
  27. cin >> a[i];
  28. }
  29. vector<int>ans;
  30.  
  31. findBoth(0, a, tar, ans);
  32. for(int x : ans){
  33. cout<<x<<" ";
  34. }
  35.  
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5292KB
stdin
5 2
1 2 2 2 2
stdout
1 2 3 4