Find an element in a sorted rotated array

preview_player
Показать описание
Problem:
Given a sorted integer array which is rotated any number of times and an integer num, find the index of num in the array.
If not found, return -1.

Solution:
1: Find index of pivot element (minimum element).
2: If num lies between start element and element at pivot-1 position, then find num in array[start…pivot-1] using binary search.
3: Else if num lies between pivot and last element, then find num in array[pivot…end] using binary search.

Рекомендации по теме
Комментарии
Автор

Dear Friends,

If you like our content and would like us to continue making great content for you, please spread the word about IDeserve.
A share/appreciation from you on social network would mean the world to us!


Thanks,
-Team IDeserve.

IDeserve
Автор

Very Good Explaination !!! Liked it!!!

DhananjayKumarThakur
Автор

does it same for ascending and descending?

Flower_withanshi
Автор

Instead of checking arr[mid+1] as pivot can we go for checking arr[mid] as pivot?

akshaygoyal
Автор

@IDeserve we can also find pivot by applying min heap which is logN complexity

priyaranjansingh
Автор

Hi Saurabh,
Nice video . Great Job :)

devkashyap
Автор

suppose we have [6, 13, 71, 72, 73, 73] -> [73, 6, 13, 71, 72, 73]. Now array[0]<=array[len-1] is True but it is rotated... I mean if you have any duplicates (not necessary at the end) your algorithm for a pivot will not work. There was said nothing in the description that all elements in the array were unique.

boloyeung
Автор

How do you find a pivot and as you said sorted array 10 is not in a sorted form in an array.

JimitRupani