You are given an array Arr[1 .. n] with the special property that Arr[1] ≥ Arr[2] and Arr[n − 1] ≤ Arr[n]. We say that an element Arr[x] is a local minimum if it is less than or equal to both its neighbors. Derive an algorithm in O(log n)

For example in the array 9,6,2,14,5,7,4 – 2 is a local minima as it is smaller than its left and right number 6 and 14. Similarly 5 is another local minima as it is between 14 and 7, both larger than 5. You need to find any one of the local minima.

**Solution:**

If the array elements are not guaranteed to be distinct, then it’s not possible to do this in O(log n) time. The reason for this is the following: suppose that you have an array where all n > 1 values are the same. In this case, none of the elements can be local minima, because no element is less than its neighbors. However, in order to determine that all values are the same, you will have to look at all the array elements, which takes O(n) time. If you use less than O(n) time, you can’t necessarily look at all the array elements.

If, on the other hand, the array elements are guaranteed to be distinct and with the given properties, you can solve this in O(log n) time using the following observations:

- If there is just one array element, it’s a local minimum.
- If there are two array elements, check each. One must be a local minimum.
- Otherwise, look at the middle element of the array. If it’s a local minimum, return it. Otherwise, at least one adjacent value must be smaller than this one. Recurs in the half of the array containing that smaller element (but not the middle).

int findLocalMinima(int[] arr, int start, int end) { int mid = (start + end) / 2; if (mid - 2 < 0 && mid + 1 >= arr.length) return -1; if (arr[mid - 2] > arr[mid - 1] && arr[mid - 1] < arr[mid]) return arr[mid - 1]; if (arr[mid - 1] > arr[mid - 2]) return findLocalMinima(arr, start, mid); else return findLocalMinima(arr, mid, end); }

Complexity O(log N)

I did not understand the question. What if array doesn’t satisfy the given property. Arr[1] ≥ Arr[2] and Arr[n − 1] ≤ Arr[n]. Wont there be a local minima?

I can give an example where property will not be satisfied but there will be a minima:

eg 12 3 14 5 6 5 , local minima is 3 even it is not satisfying the given property.

Manoj,

you are right but its required for the question to be solved in o(log N), it satisfy given property. Else it will take O(N) ti me to solve this as u have to traverse all array elements.

Its like a binary search.. u need to traverse only half of the array each time.

please somebody explain this algorithm with example…

this will find only one local minima … what if the it has two minima of both sides of middle?? ..

according to the algorithm the code must check the middle as if this is minima or not ……… but in code implementation middle-1 th element is being checked???

@nidhi

mid-1 is middle element of the array. It is specified in ques that “You need to find any one of the local minima”

try[2,1,3], this fails.

No it doesnt.

1 is the local minimum and the 2nd if-statement returns it.

Thx for the solution.

It explained well