fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int searchHelper(const int arr[], int start, int stop, int key)
  6. {
  7. if(start == stop) return -1;
  8. if(arr[start] == key) return start;
  9. return searchHelper(arr, start + 1, stop, key);
  10. }
  11.  
  12. int search(const int arr[], int size, int key)
  13. {
  14. return searchHelper(arr, 0, size, key);
  15. }
  16.  
  17. int search(vector<int> const& vec, int key)
  18. {
  19. return searchHelper(vec.data(), 0, (int)vec.size(), key);
  20. }
  21.  
  22. int main() {
  23. int arr[] = {3,10,2,5,6,1};
  24.  
  25. cout << search(arr, 6, 10) << endl;
  26. cout << search(arr, 6, 20) << endl;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
1
-1