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
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Let A[1, . . . , n] be an array of n distinct numbers. If i...
Let A[1, . . . , n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. 1. Which arrays with distinct elements from the set {1, 2, . . . , n} have the smallest and the largest number of inversions and why? State the expressions exactly in terms of n. 2. For any 0 < a < 1/2, construct an array for...
We are given n distinct balls and m distinct boxes. m and n are non-negative integers....
We are given n distinct balls and m distinct boxes. m and n are non-negative integers. Every ball must be placed into a box, but not every box must have a ball in it. Each box can hold any number of balls. Let's also assume that the order in which we put the balls into the boxes does matter. (Ex: assume we have 2 balls, a and b, and 3 boxes, 1 2 and 3. two distinct distributions would be...
We are given an array A of size n containing n positive and negative integers (the...
We are given an array A of size n containing n positive and negative integers (the array is indexed starting from 0). Our goal is to find two indices i and j such that 0 ≤ i ≤ j ≤ n and Pk=j k=i A[k] is maximized. Input sequence: [2, -4, 1, 9, -6, 7, -3] Output: [1, 9, -6, 7] or i = 2 and j = 5 Input sequence: [1, 2, 3, 4, 5, 6, -3] Output: [1,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT