Java program pivot rotated search array -DSA

preview_player
Показать описание


Find out the pivot point using binary search. We will set the low pointer as the first array index and high with the last array index.
From the high and low we will calculate the mid value.
If the value at mid-1 is greater than the one at mid, return that value as the pivot.
Else if the value at the mid+1 is less than mid, return mid value as the pivot.
Otherwise, if the value at low position is greater than mid position, consider the left half. Otherwise, consider the right half.
Divide the array into two sub-arrays based on the pivot that was found.
Now call binary search for one of the two sub-arrays.
If the element is greater than the 0th element then search in the left array
Else search in the right array.
If the element is found in the selected sub-array then return the index
Else return -1.

Follow Me On Social Media
Comment "#BackCoding if you read this 😉 😃
#java
#shorts
#programming
Рекомендации по теме