# Find a peak element in an array

Drawback:

Given an array of dimension n, discover a peak aspect within the array.

For instance:

In Array [1,4,3,6,7,5] four and seven are Peak Components. Return anyone Peak Ingredient.

Answer:

1: Initialize begin = 0, finish = array.size – 1

2: Repeat following steps until peak aspect is discovered:

(a) Discover mid = (begin+finish)/2

(b) If mid is peak aspect, return array[mid]

(c) If array[mid-1] is bigger than array[mid], discover peak in left half of array set finish = mid – 1

(d) Else discover peak in proper half of array set begin = mid + 1

Time Complexity: O(log n)

Area Complexity: O(1)

Code: http://www.ideserve.co.in/be taught/find-a-peak-element-in-an-array

Web site: http://www.ideserve.co.in

Fb: https://www.fb.com/IDeserve.co.in

source

Got the website link from Quora.. Went to the website and came to YouTube, killed the subscribe button. Watching the video now.

using a binary search… if the peak is in the left half of the array and the mid element is less than its right element then how binary search will be able to find the peak?

consider this array for example: [ 5, 45, 65, 50, 15, 25, 30, 45, 46, 47, 48, 49, 50, 51 ]. The expected peak is 65 which is in the left half of the array and the mid element is 45 which is greater than 30 and smaller than 46.

Maybe I have missed something here.

Regards

Finally I got a perfect channel to start comp prograaming.. thanks for your videos

This algorithm doesn't work at all cases. if the mid element is equal to its adjacent values. it assumes it found the peak. ex {1,2,1,1,1,2,1} outputs 1 as maximum, but this is not correct. if we try to change the code to search on both sides for this particular case, its worst case goes to O(n). This technique can be used when all the elements in array are unique.

Great video. Just gonna point out that "mid = (start+end)/2" can cause overflow. It's better to write "mid = start + (end-start) / 2"

If you need to check as few elements as possible because it's slow or something, use Golden Section Search https://en.wikipedia.org/wiki/Golden-section_search

Binary search works on sorted data, and the case if data is sorted its easy to find the pick element. Are you sure Saurav(IDeserve) that your solution in this video will cover all scenario of pick element. I don't think so. Anyways nice explanation but solution is awaited.

excellent

Does it mean in order to use binary search to find a peak value, the array should have peak values on both the left and the right side of the middle point?

How can I find multiple all peak elements using Binary Search? Is it possible?

Great job man subbed

1- You are using binary search, doesnt binary search requires a sorted array. 2- You are using a specific case where the values in this example are always higher in the next value. What if the value is equal on the left and on the right,

How about if you need to find all Peaks in a set?

does it work for all kind of unsorted arrays? if arr is {1,2,50,3,100,6,7} then we will get 50 as ans which is wrong, please correct me

ðŸŽ¯ðŸŽ¯ best interpretation !!

what if elements are greater than arr[mid] at both sides left and right?

sir, u r really amazing.

i do not know how to thank you,

previously i do not under stand the concepts.

but now it really helps.

pls keep doing ,what u r doing,

pls do not stop.

thank you

Love the way you present the material. Thank you from Southern California!!

what about next secuence? 5 90 15 25 30 45 65 50 35 1 if i apply the algorithm the response still 65… so whats mean here?

It should be greater than or equal and less than or equal because if you have [50,50,50,50,50,50], 50 will be a peak element!

just too good, best tutorials available on youtube

What happens when we're given 15, 10, 10, 10, 10 ?

what if in the discussed example the second element is of value 30?I think then your algorithm goes wrong.Please clarify

Ohh !! So, you are returning only one peak element from the array … What if i want to find all ?? You need a linear scan then in that case.

nice video broo:) thanks for ur effort keep helping us ..god bless u:) bt for example 10,9,8,7,20 it is giving answer 10 why???

The binary search approach time complexity would be O(n), consider the scenario, where all numbers in array are same. For example, 10, 10, 10, 10, ultimately you will reach to each element. and hence 0(n). But the examples you have taken, will find such number in 0(logn) time. I appreciate your response.

your video deserves way more views and likes and subcribers!!

sir, can you suggest me about machine learning statring guide for a beginner programmer?/

thanks

Fantastic videos! I really appreciate the quality of your animations and the simplicity of your explanation.

Good explanation…thanks…

I changed the array value of array[0] to 70

and still it gives me a peak value of 65

Great job…. simply awesome… way u explained peak element using graph…

Dear Friends,

If you like our content and would like us to continue making great content for you, please spread the word about IDeserve.

A share/appreciation from you on social network would mean the world to us!

Also, do like our Facebook page: https://www.facebook.com/IDeserve.co.in ðŸ™‚

Thanks,

-Team IDeserve.

i have one doubt. suppose if array contains [1, 2, 100, 4, 5, 6, 8, 9, 10]. here mid is 5 which is not a peek element ,also a[mid -1] is not greater than a[ mid] then how can you find the peak here ?

Awesome Explanation….good work! gonna refer your channel to my friends…keep it up ðŸ™‚

Well explained and constructive method to solve patterns

As Last Array is not in increasing order so 65 can be at up front like 5 65 10 15 25 1 45 30 50.

in that mid will be 25 and which is greater then both side so how will you iterate through binary search and I read somewhere that it should be in sorted order if u want to search for binary search method.