edited by
969 views
1 votes
1 votes

Let A =[a1, a2, …, an] be a one-dimensional array of integers define a MEGA-PEAK in A to be an element ai belongs A such that ai >= aj for all aj with | j – i | <= 2. What is the time complexity to find MEGA-PEAK?

  1.   O(n)
  2.   O(n2)
  3.   O(log n)
  4.   O(n log n)
edited by

1 Answer

1 votes
1 votes

Getting O(logn).

  1. Find mid as (low+high)/2.
  2. Check a[mid] with exactly 2 rightmost & leftmost elements. If it is maximum you get MegaPeak. If it's not then,
  3. If maximum element you got in step 2 was in right side set low = mid. If it was in left side set high = mid.
  4. Keep repeating 1, 2 & 3 till you get MegaPeak.
edited by

Related questions

1 votes
1 votes
0 answers
1
Sahil Gupta asked Nov 25, 2014
765 views
Answer the following part:a) Show that, under the assumption that the input is equally likely to be any of the n! permutations of theseintegers, the average number of com...
0 votes
0 votes
1 answer
3
radha gogia asked Aug 15, 2015
701 views
The database can be configured to do ordered indexing on Ap or hashing on Ap. Which of the following statements is TRUE?(A) Ordered indexing will always outperform hashin...
0 votes
0 votes
0 answers
4
. asked Feb 24, 2017
754 views
Let X={a1,a2,...,a7} be a set of seven elements and Y={b1,b2,b3} a set of three elements. The number of functions f from X to Y such that {i} f is onto and {ii}there are ...