Question

1a. Given an array a of n distinct integers, a[i] = m is a local minimum...

1a. Given an array a of n distinct integers, a[i] = m is a local minimum of a iff a[i − 1] > m and a[i + 1] < m. Suppose that n ≥ 3, a[0] > a[1], and a[n − 2] < a[n − 1]. Explain why these “end point” conditions guarantee that a has a local minimum. Hint: argue by way of contradiction. (10 points)

1b. Assuming that a satisfies the conditions described in part a, clearly and concretely describe each step of a divide-and-conquer algorithm for find a local minimum of a. Provide the recurrence for the running time T(n), and defend it.

Homework Answers

Answer #1

1a.

From the above condition, we see that if we were to plot the array on an X/Y graph (X=index, Y = value), local minimums would be at the valleys. Therefore, to ensure there is a local minimum, we must guarantee that a change in slope sign (from decreasing to increasing) exists.

If you know the endpoint slope behavior of a range, you know if there is a local minimum within. In addition, your array must have the behavior decreasing slope sign from a[0] to a[1] and increasing slope sign from a[n-1] to a[n] or the problem is trivial. Consider:

arr= [1,2,3,4,5] (increasing , increasing) arr[0] is an LM

arr= [5,4,3,2,1] (decreasing , decreasing) arr[n] is an LM

arr= [1,3,3,3,1] (increasing , decreasing) arr[0] and arr[n] are LMs

1b.

Given an array arr[0 .. n-1] of distinct integers, the task is to find a local minima in it. We say that an element arr[x] is a local minimum if it is less than or equal to both its neighbors.

a. For corner elements, we need to consider only one neighbor for comparison.

b. There can be more than one local minima in an array, we need to find anyone of them.

An efficient solution is based on Binary Search. We compare the middle element with its neighbors. If the middle element is not greater than any of its neighbors, then we return it. If the middle element is greater than its left neighbor, then there is always a local minimum in left half (Why? take a few examples). If the middle element is greater than its right neighbor, then there is always a local minimum in right half (due to same reason as left half).

Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n)

We basically ignore half of the elements just after one comparison.

  1. Compare x with the middle element.
  2. If x matches with middle element, we return the mid index.
  3. Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half.
  4. Else (x is smaller) recur for the left half.
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT