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? O(n) O(n2) O(log n) O(n log n) Algorithms algorithms time-complexity + – Akriti sood asked Dec 27, 2016 • edited Jun 29, 2022 by makhdoom ghaya Akriti sood 969 views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply Aghori commented Dec 27, 2016 i reshown by Aghori Dec 27, 2016 reply Follow Share Getting 3. Right? 0 votes 0 votes Akriti sood commented Dec 27, 2016 reply Follow Share Yea right.. Please tell. Me. Ur approach 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes Getting O(logn). Find mid as (low+high)/2. Check a[mid] with exactly 2 rightmost & leftmost elements. If it is maximum you get MegaPeak. If it's not then, If maximum element you got in step 2 was in right side set low = mid. If it was in left side set high = mid. Keep repeating 1, 2 & 3 till you get MegaPeak. Aghori answered Dec 27, 2016 • edited Dec 27, 2016 by Aghori Aghori comment Share Follow See all 7 Comments See all 7 7 Comments reply Show 4 previous comments Aghori commented Dec 28, 2016 reply Follow Share It seems as if they are asking for any 1 MegaPeak. But if you want to find all the Mega Peaks then complexity would be O(n). 0 votes 0 votes Akriti sood commented Dec 28, 2016 reply Follow Share okthanks rohit :-) 0 votes 0 votes Aghori commented Dec 28, 2016 reply Follow Share Question would have used the word all had it asked for all Mega Peaks. :) 0 votes 0 votes Please log in or register to add a comment.