fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int findMin(vector<int> & arr) {
  8. int low = 0;
  9. int high = arr.size() - 1;
  10. while (arr[low] > arr[high]) {
  11. int mid = (low + high) >> 1;
  12. if (arr[mid] > arr[high]) {
  13. low = mid + 1;
  14. } else {
  15. high = mid;
  16. }
  17. }
  18. return arr[low];
  19. }
  20.  
  21. int main() {
  22. vector<int> a = {1,2,3,5,6,7,8,9,10};
  23. rotate(a.begin(), a.begin() + 4, a.end());
  24. for (int & i : a) cout << i << " ";
  25. cout << endl;
  26. cout << "Min is " << min(a[0], *is_sorted_until(a.begin(), a.end())) << endl;
  27.  
  28. cout << "Min is " << findMin(a) << endl;
  29. return 0;
  30. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
6 7 8 9 10 1 2 3 5 
Min is 1
Min is 1