fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<int> occ(int n, vector<int>& a, int t) {
  6. // Base case
  7. if (n == 0) {
  8. return vector<int>();
  9. }
  10.  
  11. // Get the vector from the recursive call first
  12. vector<int> ans = occ(n - 1, a, t);
  13.  
  14. // Check the current element and append if it matches
  15. if (a[n - 1] == t) {
  16. ans.push_back(n - 1);
  17. }
  18.  
  19. return ans;
  20. }
  21.  
  22. int main() {
  23. int n = 5;
  24. vector<int> a = {1, 3, 5, 6, 5};
  25. int t = 5;
  26.  
  27. vector<int> ans = occ(n, a, t);
  28.  
  29. // Output should be: 24 (since 5 is at index 2 and index 4)
  30. for (int i = 0; i < ans.size(); i++) {
  31. cout << ans[i] << " ";
  32. }
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
2 4