fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. // Function to find the pivot element
  5. int findPivot(std::vector<int>& arr, int n) {
  6. int left = 0, right = n - 1;
  7. while (left < right) {
  8. int mid = left + (right - left) / 2;
  9. if (arr[mid] > arr[left]) {
  10. left=mid;
  11. } else {
  12. right=mid-1;
  13. }
  14. }
  15. return left;
  16. }
  17.  
  18. int main() {
  19. std::vector<int> arr = {5, 8, 5, 2, 0};
  20. int n = arr.size();
  21. int pivotIndex = findPivot(arr, n);
  22. if (pivotIndex != n) {
  23. std::cout << "Index: " << pivotIndex << ", Value: " << arr[pivotIndex] << std::endl;
  24. } else {
  25. std::cout << "No pivot found" << std::endl;
  26. }
  27. return 0;
  28. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Index: 0, Value: 5