Problem is here:
if(nums[mid] > nums[hi])
You cannot use this condition safely, and modifying it to using >= or in combination with nums[lo], to < or <= won't help either; consider the following two sequences:
{ 1, 1, 1, 2, 1 }
{ 1, 2, 1, 1, 1 }
0 2 4
lo mid hi
With any combination of lo or hi with any of above comparators, one of the two sequences will be evaluated the wrong way because of selecting the bad half! You ran into exactly such situation with your sequence { 1, 1, 3, 1 }.
So if you end up in such situation (nums[x] being equal for all of lo, hi and mid), you'd have to check both halfs! I suppose two recursive calls are easiest to implement. Be aware that you won't need to recurse if any of the values at the three indices differs; if you need to recurse, only for one of the two sub-results the real successor will be smaller, so this one will be the final result.
You could detect e. g. as follows:
if(nums[mid] > nums[hi] || nums[mid > nums[lo])
{
hi = mid - 1;
}
else if(nums[mid] < nums[lo] || nums[mid] < nums[high]
{
lo = mid + 1;
}
else
{
int idxLo = findPivotRecursively(nums, lo, mid - 1);
int idxHi = findPivotRecursively(nums, mid + 1, high);
return nums[idxHi] > nums[(idxHi + 1) % n] ? idxHi : idxLo;
}
Provided you turned the function into a recursive one with signature
int findPivotRecursively(std::vector const& nums, int high, int low);
As the array is sorted, this last condition can only be true for either of idxLo or idxHigh; the test I chose will result in selecting first element of vector, if all elements are equal (nums[idxLo] > nums[(idxLo + 1] ? /*...*/; would select the last one).
A helper function would provide the public interface:
int findPivot(std::vector const& nums)
{
return findPivotRecursively(nums, 0, nums.size() - 1);
}
With help of a lambda, you could hide away the working function entirely. Unfortunately, recursive lambdas are not supported directly, so you'd rely on a little trick as shown in this answer:
int findPivot(std::vector const& nums)
{
size_t n = nums.size();
auto findPivotRecursively = [&nums, &n] (int hi, int lo, auto& fpr) -> int
{
// ...
else
{
int idxLo = fpr(hi, lo, fpr);
// ...
}
// ...
};
return findPivotRecursively(0, nums.size() - 1, findPivotRecursively);
}