fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void helper(int n,vector<int>a,int& tar,int idx){
  4. if(n==0 ){
  5. //cout<<"not found"<<endl;
  6.  
  7. return;
  8.  
  9. }
  10.  
  11. //cout<<"down "<<a[n-1]<<endl;
  12.  
  13. helper(n-1,a,tar,idx);
  14. if(a[n-1] == tar){
  15. idx = n-1;
  16. return;
  17.  
  18. }
  19. //cout<<"up "<<a[n-1]<<endl;
  20. }
  21. int main() {
  22. int n,tar;cin>>n>>tar;
  23. vector<int>a(n);
  24. for(int i = 0;i<n ;i++){
  25. cin>>a[i];
  26. }
  27. int idx =0;
  28. helper(n,a,tar,idx);
  29. cout<<"idx "<<idx<<endl;
  30. // your code goes here
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5328KB
stdin
5 2
1 2 9 2 8
stdout
idx  0