Question

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.

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.

- Compare x with the middle element.
- If x matches with middle element, we return the mid index.
- 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.
- Else (x is smaller) recur for the left half.

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...

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 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

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 6 minutes ago

asked 8 minutes ago

asked 19 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago