Question

An array A[1,2,... ,n is unimodal if its consists of an increasing sequence followed by sequence a decreasing sequence. More precisely, there exists an index k є {1,2,… ,n} such that there exists an indes . AlE]< Ali1 for all 1 i< k, and Ai]Ali 1 for all k< i< n A1,2,..,n] in O(logn) time the loop invariant (s) that your algorithm maintains and show why they lead to the correctness Give an algorithm to compute the maximum element of a unimodal input array Argue the correctness of your algorithm by presenting a proof. If you use loops, give of your algorithm. If your algorithm is defined recursively, prove its correctness by induction Finally, prove the upper bound on its running time.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER :

array is called unimoda iff it can be split into an increasing sequence followed by a decreasing sequence 1 3 4 57810 12 13 1

ALGORITHM :

function unimodalMaximumElement(Array A, int minimum, int maximum) :

if minimum = maximum - 1:

return A[minimum]

let middleElement = ⌊(maximum + minimum) / 2⌋

if A[middleElement] < A[middleElement + 1]

return unimodalMax(A, middleElement + 1, maximum)

else:

return unimodalMax(A, minimum, middleElement + 1)

END FUNCTION

RECURRENCES FOR THE ABOVE ALGORTHM :

T(1) = Θ(1)

T(n) ≤ T(⌈n / 2⌉) + Θ(1)

COMPLEXITY :

O(log N)

22T+c 8心)-.

LOOP INVARIANTS :

We will find out the mid element of the array and compare it each time with the next element, i.e., with mid+1.

Thus, everytime loops run, we will get to an elelement which will be the required result.

WHEN we will get the element right in th middle by first attempt, then the UPPER BOUND will be :

T(1) = Θ(1)

AFTER THAT, loop variants offers us following recurrence upper bound on the array A :

T(n) ≤ T(⌈n / 2⌉) + Θ(1)

THUS MAKING THE TIME COMPLEXITY TO O(log N).

OR

ANS:

Please let me know in case of any issue.

We can modify the standard Binary Search algorithm for the given type of arrays.
i) If the mid element is greater than both of its adjacent elements, then mid is the maximum.
ii) If mid element is greater than its next element and smaller than the previous element then maximum lies on left side of mid. Example array: {3, 50, 10, 9, 7, 6}
iii) If mid element is smaller than its next element and greater than the previous element then maximum lies on right side of mid. Example array: {2, 4, 6, 8, 10, 3, 1}

int findMaximum(int arr[], int low, int high)
{

/* Base Case: Only one element is present in arr[low..high]*/
if (low == high)
return arr[low];

/* If there are two elements and first is greater then
the first element is maximum */
if ((high == low + 1) && arr[low] >= arr[high])
return arr[low];

/* If there are two elements and second is greater then
the second element is maximum */
if ((high == low + 1) && arr[low] < arr[high])
return arr[high];

int mid = (low + high)/2; /*low + (high - low)/2;*/

/* If we reach a point where arr[mid] is greater than both of
its adjacent elements arr[mid-1] and arr[mid+1], then arr[mid]
is the maximum element*/
if ( arr[mid] > arr[mid + 1] && arr[mid] > arr[mid - 1])
return arr[mid];

/* If arr[mid] is greater than the next element and smaller than the previous
element then maximum lies on left side of mid */
if (arr[mid] > arr[mid + 1] && arr[mid] < arr[mid - 1])
return findMaximum(arr, low, mid-1);
else // when arr[mid] is greater than arr[mid-1] and smaller than arr[mid+1]
return findMaximum(arr, mid + 1, high);
}

Time Complexity: O(Logn)

Add a comment
Know the answer?
Add Answer to:
An array A[1,2,... ,n is unimodal if its consists of an increasing sequence followed by sequence...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (20 points) You are given an array A of distinct integers of size n. The sequence...

    (20 points) You are given an array A of distinct integers of size n. The sequence A[1], A[2], ..., A[n] is unimodal if for some index k between 1 and n the values increase up to position k and then decrease the reminder of the way until position n. (example 1, 4, 5, 7, 9, 10, 13, 14, 8, 6, 4, 3, 2 where the values increase until 14 and then decrease until 1). (a) Propose a recursive algorithm to...

  • An m×n array A of real numbers is a Monge array if for all i,j,k, and l such that 1≤i<k≤m and ...

    An m×n array A of real numbers is a Monge array if for all i,j,k, and l such that 1≤i<k≤m and 1≤j<l≤n , we have >A[i,j]+a[k,l]≤A[i,l]+A[k,j]> In other words, whenever we pick two rows and two columns of a Monge array and consider the four elements at the intersections of the rows and columns, the sum of the upper-left and lower-right elements is less than or equal to the sum of the lower-left and upper-right elements. For example, the following...

  • Question 1 3+cos(n) 2n X Which of the following properties hold for the sequence an for...

    Question 1 3+cos(n) 2n X Which of the following properties hold for the sequence an for n 2 1? l. Bounded Il. Monotonic IIl. Convergent Selected Answer a. I only a. I only b. Il only c. I and Il only d. I and Ill only e. I, II, and III Remember what these conditions mean: Bounded means all terms of the sequence have to lie within a specific range of values. Monotonic means the sequence is ALWAYS increasing or...

  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

  • real analysis 1,3,8,11,12 please 4.4.3 4.4.11a Limits and Continuity 4 Chapter Remark: In the statement of T...

    real analysis 1,3,8,11,12 please 4.4.3 4.4.11a Limits and Continuity 4 Chapter Remark: In the statement of Theorem 4.4.12 we assumed that f was tone and continuous on the interval I. The fact that f is either stric tric. strictly decreasing on / implies that f is one-to-one on t one-to-one and continuous on an interval 1, then as a consequence of the value theorem the function f is strictly monotone on I (Exercise 15). This false if either f is...

  • help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow...

    help me answer the following questions please 1. Stack (LIFO) & its application 1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking Exercises: 1) Which of the following applications may use a stack? A. parentheses balancing program. B. Keeping track of local variables at run time. C. Syntax analyzer for a compiler. D. All of the above. 2) Consider the usual algorithm for determining whether a sequence of parentheses is balanced....

  • Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

    Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT