Question

Consider a variant of the array in previous problem. An array A[1..n] has a property that...

Consider a variant of the array in previous problem. An array A[1..n] has a property that it first rises monotonically up to some point i then it monotonically decreases until some point j and then rises again. That is A[k] < A[k + 1] for all k ∈ [1..i−1]∪[j..n−1] and A[k] > A[k + 1] if i ≤ k < j. Can we still find i in O(logn) time? If not what is the minimum time required?

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

We cannot find i in O(logn) time. Consider the below scenarios.

Let's say we have array A [1,2,3,4,5,4,3,2,1,2,3,4,5]

Now in above array we first find the middle element which is 7th(3). We can analyze(compare with adjacent) that this middle sequence is decreasing so now we will consider only left sub array because i will be present there.

Again we will find middle and compare with adjacent elements and figure out whether it is increasing or decreasing. If increasing then we will consider right sub array and if decreasing then left sub array. Repeat steps till the middle element is i (when its left and right both elements are smaller). So, in this case we can findout solution in o(logn).

Now consider array A [1,2,3,4,5,6,7,8,4,3,5,6,7]

In above array middle element is 7th (7). Now when we compare it with adjacent elements we see it is increasing but we don't know which increasing sequence it is : first or last. So, we can not decide which sub array left or right we should consider to search i.

So, we cannot solve it with binary search. The best possible way to search with linear search in o(i) time.

Add a comment
Know the answer?
Add Answer to:
Consider a variant of the array in previous problem. An array A[1..n] has a property that...
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
  • 6. Consider the following basic problem. You're given an array A consisting of n integers A[1],...

    6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple 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 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n -...

    Question 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n - 1], K) count = 0 for i = 0 ton - 1 do for j = i +1 to n - 1 do if A[i] + A[j] == K then count = count +1 end if end for end for return count What is the best case time complexity of the above algorithm?! (log(n)) O(1) (n) (na) Previous o H H 9

  • 5. (2 points) In this problem, consider the following variant of linear search, called search: static...

    5. (2 points) In this problem, consider the following variant of linear search, called search: static int search(int[] a, int x) { for (int i = 0; i <= a.length / 2; i++) { if (a[i] == x) return i; if (a[a. length - i - 1] == x) return a.length - i - 1; return -1; (a) (1 point) Which of the following describes the worst-case input for search? A. An array of size N in which x is...

  • Consider the following algorithm. ALGORITHM Enigma(A[0.n - 1]) //Input: An array A[0.n - 1] of integer...

    Consider the following algorithm. ALGORITHM Enigma(A[0.n - 1]) //Input: An array A[0.n - 1] of integer numbers for i leftarrow† 0 to n - 2 do for j leftarrow†† i +1 to n - 1 do if A[i] = = A[j] return false return true a) What does this algorithm do? b) Compute the running time of this algorithm.

  • Consider the Java program below to sort an array A in an ascending order. M, N,...

    Consider the Java program below to sort an array A in an ascending order. M, N, and K are positive integers, and A is an array of N nonnegative integers where 0 < A[i] < M for all i e {0,..., N -1}. In this program, list is a class of an integer list with the following methods. 1st.size(): returns the number of elements in the list lst 1st.get(i): returns the element at the i-th position in the list lst...

  • 7. (10) Given an array of integers A[1..n], such that, for all i, 1 <i< n,...

    7. (10) Given an array of integers A[1..n], such that, for all i, 1 <i< n, we have |Ali]- Ali+1]| < 1. Let A[1] = and Alny such that r < y. Using the divide-and-conquer technique, describe in English algorithm to find j such that Alj] z for a given value z, xz < y. Show that your algorithm's running time is o(n) and that it is correct o(n) search an 2 8. (10) Solve the recurrence in asymptotically tight...

  • Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below....

    Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below. Count-Occurrence(A, n, k). count 0 for j = 1 to n if A[j] ==k count = count+1 print count Which of the following is the correct loop invariant for the for loop? At the start of each iteration jof the for loop, count represents the number of times that k occurs in the subarray A[1.j]. At the start of each iteration of the for...

  • Make a sorted integer array a[i]=i, i=0,...,n-1. Let bs(a,n,x) be a binary search program that returns...

    Make a sorted integer array a[i]=i, i=0,...,n-1. Let bs(a,n,x) be a binary search program that returns the index i of array a[0..n-1] where a[i]=x. Obviously, the result is bs(a,n,x)=x, and the binary search function can be tested using the loop for(j=0; j<K; j++) for(i=0; i<n; i++) if(bs(a,n,i) != i) cout << “\nERROR”; Select the largest n your software can support and then K so that this loop with an iterative version of bs runs 3 seconds or more. Then measure...

  • Consider an array A[1...n] which is originally unsorted. One method to sort the array is as...

    Consider an array A[1...n] which is originally unsorted. One method to sort the array is as follows: First find the largest key in the unsorted portion of the array (initially the entire array s unsorted) and swap this with the last position in the unsorted section. This last position is now considered part of the sorted portion. The procedure is repeated until the entire array is sorted. (a) Write an algorithm to sort A as outlined above (in pseudo-code, no...

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