# Using binary search in different scenarios

I have to give my one of the biggest interviews next week and I am working hard for that. I have also enrolled myself for some mock interviews for that. So, coming to the problem last day I had this mock interview and I was asked this question:

Search an element in a sorted and rotated array

at first I was clueless but the interviewer asked me to break the problem at hand and then solve the smaller problem first so I decided to write binary search to find the element in the sorted array, surprisingly I was able to write the algorithm within a go since I had clear mental model of the problem at hand now I was asked to proceed and solve the given question. I thought for a second and told him that if I somehow know the peak point I can decide which direction to go to find the given element, he told me that I am on the right track and then boom I wasn't able to think properly on how to convert my logic into code, he gave me a couple of hints and it was too embarrassing for me to just sit and think.

The problem is after knowing the solution and knowing that my intuition was correct I am not sure why I failed, I need to know is there a different way of thinking of such problems to better understand the logic? Why I feel discomfort when I found a new problem on binary search although I knew and solved the original one? is there a gap in my mental modal?

• I haven't looked at the Geeksforgeeks solution at all, but my first instinct would be to write a class or method that returns the elements from the original array unrotated or adjusts the index accordingly. It would then yield to an ordinary binary search. Dec 30, 2016 at 16:24
• @RobertHarvey so how would you do that? Dec 31, 2016 at 2:41
• 1. Find out pivot point and divide the array in two sub-arrays. (pivot = 2) /*Index of 5*/ 2. Now call binary search for one of the two sub-arrays. (a) If element is greater than 0th element then search in left array (b) Else Search in right array (1 will go in else as 1 < 0th element(3)) 3. If element is found in selected sub-array then return index Else return -1. Dec 31, 2016 at 13:44
• @RobertHarvey for finding pivot the criteria should be the left and right element should be small, right? Dec 31, 2016 at 15:04
• If you scan the array from left to right, you will find the pivot point when the right element is suddenly smaller than the left element. Read the GeeksForGeeks article carefully. Dec 31, 2016 at 15:06

it was too embarrassing for me to just sit and think.