BS-9. Find Peak Element

  Рет қаралды 198,674

take U forward

take U forward

Күн бұрын

Пікірлер: 416
@takeUforward
@takeUforward Жыл бұрын
Sorry for the delay :( But I had a product launch, for which I had to spend most of my time in the office. I am back, will try to come with 1/2 videos daily now
@charan123rams3
@charan123rams3 Жыл бұрын
Thank you so much sir 🙏 Sir first take rest whenever you have free time then post sir 🥲
@shivanshumishra0560
@shivanshumishra0560 Жыл бұрын
Thankyou so much bhaiya for all videos ...🙏🙏❤️❤️ Your content is >>>> whole KZbin
@sudhanshushekhar4222
@sudhanshushekhar4222 Жыл бұрын
Understood
@sayakghosh5104
@sayakghosh5104 Жыл бұрын
Was trying this for the past 2 days, couldn't solve it. Took 1 day break came back at it again with a fresh mind, could solve it in one go by myself. Thanks, your series instills such confidence in me always.
@OrderEmperor
@OrderEmperor 6 ай бұрын
no one asked
@sayakghosh5104
@sayakghosh5104 6 ай бұрын
@@OrderEmperor 😂 ok bro.
@paraskashyap7362
@paraskashyap7362 5 ай бұрын
@@OrderEmperor no one asked nested(no one asked if no one asked or not)
@paraskashyap7362
@paraskashyap7362 4 ай бұрын
@@arunm619 fair enough
@Kj89865
@Kj89865 3 ай бұрын
@@paraskashyap7362 calling in return statement (recursion)no one asked if no one asked or not that no one asked :----) *without breaking condition
@anujrathore5726
@anujrathore5726 9 ай бұрын
NO ONE CAN TEACH LIKE STRIVER, HE GIVES HIS 100% to his lectures. he want to teach , he compleltly go into the state of teaching , like with full FOCUS & DEDICATION . he enjoy to teach . and all of this FREE OF COST , LEGEND AKA RAJ VIKRAMDITYA BHAIYA (STRIVER BHAI OP ) ❤
@saswatrath4646
@saswatrath4646 7 ай бұрын
at 21:15 correction* if(arr[o] > arr[1]) return 0; the first index is the peak
@ishanshah3309
@ishanshah3309 Жыл бұрын
the best about this video was the thinking process of how to arrive from linear to binary search in problems like this. execellent stuff.
@yashgarg3899
@yashgarg3899 Жыл бұрын
i have solved these questions already so before but still i watch every video of you because i know there will be something different which ultimately enhance my code writing or intuition skills.
@priyeshtandel2101
@priyeshtandel2101 3 ай бұрын
Are you from DAIICT ?
@shivanshugarg807
@shivanshugarg807 5 ай бұрын
Thanks striver I completed all your sheets , like twice , they boost my confidence , before watching your solution , i try every question for 30 mins , and regardless i am able to solve it or not , i watch your video for better understanding and side by side mark improtance of question and make notes for revision
@Kaif_Ali_Khan
@Kaif_Ali_Khan 3 ай бұрын
ok
@3lpme
@3lpme 3 ай бұрын
Then plz explain why should we consider binary search even though it's not
@girivardhanvelpula
@girivardhanvelpula 9 ай бұрын
00:04 Find the peak element in an array 02:16 Identifying peak elements using the concept of infinity assumptions. 06:42 Peak element finding in a sorted array using binary search 09:03 Binary search to find peak element 13:59 Identifying peak in decreasing curve 16:22 Dealing with the cases of the first and last elements as peak elements. 20:38 Finding peak element in an array using binary search 22:54 In a decreasing curve, the mid element is greater than the right element. 27:08 Binary search can efficiently find the peak element 29:08 Algorithm to find peak element in an array
@3lpme
@3lpme 3 ай бұрын
first of all it's not sorted array 06:42
@ritvikryadav9667
@ritvikryadav9667 Жыл бұрын
Hey Striver, Your videos are pure gem. Certainly the best DSA course on the planet. Keep going. God Bless You!
@kishoregowda8210
@kishoregowda8210 2 күн бұрын
Below is my code. You may find this more easier. Initially start with low -> 0 and high -> n-1. Whenever we calculate the "mid", we will check if this "mid" is part of uphill curve or downhill curve. If it is part of downhill, then i would reduce the search space to left of the array because for sure we know that we would find the larger element to it's left. And similarly if mid element is part of uphill, then move to low pointer to right side of mid. public int findPeakElement(int[] nums) { int low = 0, high = nums.length-1, n = nums.length; while (low < high){ int mid = (high - low)/2 + low; if(nums[mid] > nums[mid + 1]) high = mid; else low = mid + 1; } return low; }
@ankitdhattarwal2286
@ankitdhattarwal2286 Жыл бұрын
There are one mistake in the code when try solve problem on GFG then not pass all the cases the mistake is if(arr[n-1] >= arr[n-2]) return n-1; // use the equal sign also so that can pass all the cases and thank you so much to provide best content
@lavanya_m01
@lavanya_m01 7 ай бұрын
yes, that is because the definition of peak is different in gfg. It says the peak can be greater than or equal to its adjacent elements (a=b) but in leetcode, the peak should be strictly greater (ab)
@rakshan3323
@rakshan3323 3 ай бұрын
@@lavanya_m01 And that peak definition seems to be correct because here, it doesn't cover the case when all the elements are equal.
@GalaxyStart62
@GalaxyStart62 8 ай бұрын
it's a blessing that you are in youtube, but on the other side , people are enjoying DSA now, hence , competition is drastically increasing too.
@mriit3025
@mriit3025 6 ай бұрын
the extra afforts for the edgecases is appreciated! keep working on content like this...
@oguluribrahmaiah1301
@oguluribrahmaiah1301 10 ай бұрын
Commenting Understood is just a simple thing for your wonderful explanation !!! Thank you So much for your lectures Striver
@hitanshujena7470
@hitanshujena7470 23 күн бұрын
Accidently i just wrote the code to find the peak element if there are multiple peaks are available in the array but all these happens for you so thanks a lot
@iWontFakeIt
@iWontFakeIt Жыл бұрын
was waiting for these past two weeks and you showed up with another awesome explanation! Thanks dada ❤
@ishangujarathi10
@ishangujarathi10 Жыл бұрын
GOD LEVEL INTUITION AND EXPLANATION STRIVERRR TYSM < 3!!!
@3lpme
@3lpme 3 ай бұрын
Kaise develop hogi iss intuition from inside?😢
@tanaywhocodes
@tanaywhocodes 2 ай бұрын
Thank you ! wrote the same code without watching the video. All because of you.
@mahendrachourasiya7444
@mahendrachourasiya7444 Жыл бұрын
SUPERBBBB Explanation! Thankyou so much for your great efforts.
@AdityaGarg-hl7ri
@AdityaGarg-hl7ri 11 ай бұрын
Thankyou striver, still maintaining that low=m+1 and high =m-1 thing which makes it super easy to understand!!
@Dipanshutripathi2407
@Dipanshutripathi2407 Жыл бұрын
Filled with full confidence after watching ur videos.
@viveksinghrajput2725
@viveksinghrajput2725 Жыл бұрын
Best explanation on the KZbin thank you soo much....each and every second of this video is informative.
@RaunitJaiswal-s9v
@RaunitJaiswal-s9v 2 ай бұрын
for those who are facing problem with the if else part for the right side here is the easy way to think If the element to the right of mid is greater (arr[mid] < arr[mid + 1]), we move to the right half (since there must be a peak in that direction).
@Bigg_boss_trolls
@Bigg_boss_trolls 6 күн бұрын
NO ONE CAN TEACH LIKE STRIVER
@DeepikaKondamadugu
@DeepikaKondamadugu 6 ай бұрын
Didn't even look for completion time of the video!!!.....So that much interesting your videos are❤
@pizzagamingyt9712
@pizzagamingyt9712 Жыл бұрын
Bro u are the most helpful person for all engineering cse student 🧡❤
@Dreamer4lyf
@Dreamer4lyf 5 ай бұрын
Amazing explanation Striver my man! Hats off to you for all your hard work for the community.
@Manishgupta200
@Manishgupta200 Жыл бұрын
THe last one infinite loop example was very helpful to understand the problems condition clearly.
@Harsh-x5o9f
@Harsh-x5o9f 5 ай бұрын
The man who is making every CSE student's career bright
@madhulikaghosh3876
@madhulikaghosh3876 7 ай бұрын
Your explanation is crazy. You are really doing a very good job.
@theengineer8301
@theengineer8301 7 ай бұрын
Hi Striver, this was an epic explanation. Hats off! However I think we can only check the left element of mid. If greater then eliminate right. Else eliminate left. No need for the other if condition. class Solution { public: int findPeakElement(vector& arr) { int n = arr.size(); if(n == 1) return 0; if(arr[0] > arr[1]) return 0; if(arr[n-1] > arr[n-2]) return n-1; int low = 1; int high = n - 2; int mid; while(low arr[mid+1] ) return mid; //mid-1 greater than mid, peak on left,eliminate right if(arr[mid-1] > arr[mid]) high = mid - 1; //peak on right, eliminate left else low = mid + 1; } return mid; } };
@expanse8846
@expanse8846 4 ай бұрын
he explained it by the end. it may lead to an infinite loop if there are multiple peaks.
@deepanshudahiya8966
@deepanshudahiya8966 5 ай бұрын
An amazing question learning new patterns of conditions in binary search
@graviton001
@graviton001 3 ай бұрын
Was able to do it myself 😮 thnx for building up my logical thinking ❤
@rrt19254
@rrt19254 2 ай бұрын
Woah, solved this at once! Think I'm learning this concept!
@saambpatil9912
@saambpatil9912 Жыл бұрын
iNSTEAD OF ADDING THE LAST CONDITION WE CAN CHANGE THE ABOE CONDITIONS TO : else if (nums[mid] < nums[mid + 1]) { low = mid + 1; } else if (nums[mid] < nums[mid - 1]) { high = mid - 1; } COMPLETE CODE: class Solution { public: int findPeakElement(vector &nums) { int n = nums.size(); if (n == 1) { return 0; } if (nums[0] > nums[1]) { return 0; } if (nums[n - 1] > nums[n - 2]) { return n - 1; } int low = 1; int high = n - 2; while (high >= low) { int mid = low + (high - low) / 2; if (nums[mid] > nums[mid + 1] && nums[mid] > nums[mid - 1]) { return mid; } else if (nums[mid] < nums[mid + 1]) { low = mid + 1; } else if (nums[mid] < nums[mid - 1]) { high = mid - 1; } } return 0; } };
@gokulkrishnanj
@gokulkrishnanj 4 ай бұрын
Nice explanation on that hidden test case
@Josuke217
@Josuke217 4 ай бұрын
This was pretty easy, finally solved it on my own.
@Raj10185
@Raj10185 Жыл бұрын
code for the same :- int findPeakElement(vector& arr) { int n = arr.size(); //checking done for initial cases where finding and is very easy if(n==1) //edgecase1 return 0; if(arr[0] > arr[1]) //edgecase 2 return 0; if(arr[n-1] > arr[n-2]) //edgecase 3 return n-1; // now apply bS on remaining part int l = 1; int r = n-2; while(larr[mid+1] && arr[mid]>arr[mid-1]) { return mid ; } else if (arr[mid]>arr[mid-1]) //mid on the increasing path that means our ans will be in right side l = mid+1; else // mid is on the decreasing path peak will be on left side r = mid -1; } return -1; }
@AppaniMadhavi
@AppaniMadhavi 9 ай бұрын
not working for all testcases in coding ninjas
@noone1000_xyz
@noone1000_xyz Жыл бұрын
at the end of video i am just surprised.......understood very clearly.....thankssss a lottttt striver......
@ParkersFact_Finder
@ParkersFact_Finder 2 ай бұрын
how you think like that striver i mean your thought process is totally diff how can i achieve this type of thought process honestly Best DSA playlist
@ashishrajoria2251
@ashishrajoria2251 2 ай бұрын
public int findPeakElement(int[] nums) { int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[mid + 1]) { right = mid; } else { left = mid + 1; } } return left; }
@PiyushKumar-xe1ng
@PiyushKumar-xe1ng 4 ай бұрын
int start = 0; int end = n - 1; while (start < end) { int mid = start + (end - start) / 2; if (arr[mid] < arr[mid + 1]) { start = mid + 1; } else { end = mid; } } return start; (how about this ,easy and covers every edge case)
@RaunitJaiswal-s9v
@RaunitJaiswal-s9v 2 ай бұрын
done with easy part of binary search from monday will start with medium part lots of love raj bhaiya
@padmaja577
@padmaja577 8 ай бұрын
Firstly the brute force psudeo code will only for: 1) array which has one peak element. 2) array is in ascending order 3) array is in descending order.
@dank7044
@dank7044 Жыл бұрын
absolute beauty of a question
@hashcodez757
@hashcodez757 10 ай бұрын
Understood very well sir!! No words for your appeciation bhaiya
@socify4410
@socify4410 11 ай бұрын
solved by my own thanks a lot Striver Bro keep spreading the knowledge
@piyushwandile4152
@piyushwandile4152 Жыл бұрын
great content no one teach like this atleast for free
@Raj10185
@Raj10185 Жыл бұрын
Tysm first time i got clear cut intution of finding the peak element :) tysm again UNDERSTOOD EVERYTHING:)
@abhijittalukdar6995
@abhijittalukdar6995 Жыл бұрын
Bhaiya I solved this without seeing the video. Because i have learnt the concepts from your earlier videos. Thanks and LOTS of LOVE.
@Codro09
@Codro09 7 ай бұрын
This is the brute linear solution : class Solution { public: int findPeakElement(vector& nums) { //Brute force: int n = nums.size(); int ans = INT_MIN; int peak_index = -1; if(n==1) return 0; for(int i = 0; i < n; i++){ if (nums[i] > ans) { ans = nums[i]; peak_index = i; } } return peak_index; } };
@AtulKumar-c4x7l
@AtulKumar-c4x7l Жыл бұрын
understood Thank you striver for such an amazing explanation...
@manavsingh5919
@manavsingh5919 Жыл бұрын
Thank You Striver..Understood everything🙂
@hareshnayak7302
@hareshnayak7302 7 ай бұрын
Understood,thanks striver for this amazing video.
@knownforunknowns
@knownforunknowns 22 күн бұрын
Understand !! Thank u bhaiya
@arunm619
@arunm619 4 ай бұрын
Reversal of a peak is called trough. 30:29
@Mousam_Sharma
@Mousam_Sharma 11 ай бұрын
Understood .best Explanation 👍
@cinime
@cinime Жыл бұрын
Understood! Super awesome explanation as always, thank you very much for your effort, and congrats for your product launch!! Is this idea used to find the derivative local maximum / minimum?
@siddunihary
@siddunihary 5 ай бұрын
Strivers Sir You Are The Best
@casafurix
@casafurix Жыл бұрын
wow, the reversal peak, beautiful
@HitanshuRathi-zz5vn
@HitanshuRathi-zz5vn 7 күн бұрын
understood thanks brother
@TrinmoyDutta
@TrinmoyDutta 8 ай бұрын
while(low < high-1){ mid = (low+high)/2; if(arr[mid]>arr[mid+1] && arr[mid]>arr[mid-1]) return arr[mid]; if(arr[mid+1]>arr[mid]) low = mid+1; else high = mid-1; } if(arr[low]>arr[high]) return arr[low]; else return arr[high]; initially came up with this solution, it was working and was accepted in leetcode
@boomerofficial7177
@boomerofficial7177 10 ай бұрын
Kya question samjhaya hai bhai 👍👍👍
@kenkaneki669
@kenkaneki669 Жыл бұрын
Damn, was eagerly waiting for this!!!
@rashikhandelwal4121
@rashikhandelwal4121 5 ай бұрын
Bestest explanation ❤
@Ald0Abdiel
@Ald0Abdiel 7 ай бұрын
Amazing explanation! thanks teacher :)
@PrioritizingPeace
@PrioritizingPeace 3 ай бұрын
Bs-9 Done ✅ Weekend 27 July 2024 7:43 pm
@kingbadshah452
@kingbadshah452 9 ай бұрын
thanks striver understood everything
@Vivek-t9o8g
@Vivek-t9o8g 5 ай бұрын
Excellent explanation
@agamsrivastava8037
@agamsrivastava8037 11 ай бұрын
In the last example peak element will be 3 instead of 5 @3:34
@muntasirayan6006
@muntasirayan6006 9 ай бұрын
if we give last condition (else high = mid -1;) that will work for both 1 peak element and also multi peak element.
@snehashisratna9074
@snehashisratna9074 Жыл бұрын
luv the way u teach
@rohitsai806
@rohitsai806 Жыл бұрын
Fantastic teaching...!
@cursed_nerd
@cursed_nerd Жыл бұрын
welcome back, waited for you every single day
@ammanchhetri6716
@ammanchhetri6716 Жыл бұрын
there is also a brute force approach using stl ....that is we know whatever the case is.... the maximum element of the vector is always a peak element...so we just return the index of the max element.... int ind = max_element( nums.begin(), nums.end() ) - nums.begin() ; ig
@kayathrivenkatesan7712
@kayathrivenkatesan7712 Жыл бұрын
Great video sir..thank u for saving us
@LemesaElias
@LemesaElias 2 ай бұрын
Someone wrote and I quote "Striver can transfer his confidence to his students" .
@dipingrover1970
@dipingrover1970 5 ай бұрын
very good explanation .
@ashokyadavfqplmgxjia4668
@ashokyadavfqplmgxjia4668 Жыл бұрын
far better than other vedio
@charan123rams3
@charan123rams3 Жыл бұрын
Sir please complete as soon as possible please sir 🙏🙏😢
@riteshbisht94
@riteshbisht94 10 ай бұрын
God level content 🥵 🔥🔥
@Aman_Panchal27
@Aman_Panchal27 Жыл бұрын
understood, you are BEST !
@impalash_ag
@impalash_ag 11 күн бұрын
Hi Raj, could you help me understand how did you approach this problem which made you think of writing a solution considering there's only a single peak element in the array?
@t3ch_r4id
@t3ch_r4id 4 ай бұрын
Tried to code it by myself and it's working fine 🙂 but the only problem is it's returning 7 7 and only 7 no matter what changes you make it will give you seven 😢😐
@draxxgamingf.f6369
@draxxgamingf.f6369 3 ай бұрын
Thala for a reason
@sukritinarang5016
@sukritinarang5016 10 ай бұрын
Great Playlist . 👍
@hariomtiwari7605
@hariomtiwari7605 Жыл бұрын
UNDERSTOOD 👌👌👌👌👏👏👏👏
@akashddeepchitransh4537
@akashddeepchitransh4537 Жыл бұрын
Mind blowing video..
@LokeshKumar-lv3zq
@LokeshKumar-lv3zq 4 ай бұрын
Compiling your Code... > Success! Running Test Cases... > TestCase - Easy Failed Wrong Answer Your program's output doesn't match the expected output. You can try testing your code with custom input and try putting debug statements in your code. Your submission failed for the following input Arg 1: An Integer Array, For e.g [1,2,3] [1,1000000000,1000000000] Test As Custom Input The expected return value: 1000000000 Your function returned the following: -1 Final Verdict > Wrong Answer Failing for input - [1,1000000000,1000000000] can someone help me Identify the error(Note: the code is same as mentioned in this video)
@dayashankarlakhotia4943
@dayashankarlakhotia4943 Жыл бұрын
Very well explained. Understood
@SatyaMantha
@SatyaMantha Ай бұрын
Striver you are amazing
@mehrabhossain8225
@mehrabhossain8225 10 ай бұрын
you are really great one ..
@harshitsingh3510
@harshitsingh3510 7 ай бұрын
can we simply find the largest and return that index , it will also give the peak😜
@mohitt_amv
@mohitt_amv 3 ай бұрын
it will take o(n) if im not wrong!?
@KR_Technical-hj3bf
@KR_Technical-hj3bf Ай бұрын
@@mohitt_amvif i am not wrong we have given a condition is that it is should be greater then their next element
@harshithapogala8845
@harshithapogala8845 Ай бұрын
The condition you siad is right but we are assuming that ithe first elements left and last elements right would always be-infinity so automatically it will be the greatest element in the edge cases and@@KR_Technical-hj3bf
@ganeshjaggineni4097
@ganeshjaggineni4097 5 ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@AnmolGupta-oj4lm
@AnmolGupta-oj4lm Жыл бұрын
Understood Very Well!
@BrownMonster17
@BrownMonster17 Ай бұрын
love you struggle bro
@anshpalekar152
@anshpalekar152 4 ай бұрын
But what if question is [1,2,1,3,5,6,4] , it will eliminate left half and go to right where peak is not there
@NewtonVarma
@NewtonVarma 5 ай бұрын
If we are to return an array of indices where peaks are found(all peak) can we still apply binary search by making some modifications and applying if else checkups. 🤔
@RajNamdev_19
@RajNamdev_19 4 ай бұрын
Understood.
@aayushshrestha1655
@aayushshrestha1655 Жыл бұрын
TO AVOID THAT INFINITE LOOP CONDITION WE CAN JUST CHANGE THE LAST IF CONDITION TO : if(a[mid] < a[mid-1]) { .....} this will prevent the infinite loop
@NazeerBashaShaik
@NazeerBashaShaik 7 ай бұрын
Understood, thank you.
BS-10. Finding Sqrt of a number using Binary Search
17:11
take U forward
Рет қаралды 144 М.
Find Peak Element - Leetcode 162 - Python
11:02
NeetCodeIO
Рет қаралды 48 М.
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 45 МЛН
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 111 МЛН
Maximum Product Subarray - Best Intuitive Approach Discussed
20:27
take U forward
Рет қаралды 233 М.
DBA wet & Code Review voor Gemeente Nijmegen
15:28
Sayf Jawad
Рет қаралды 11 М.
ML Was Hard Until I Learned These 5 Secrets!
13:11
Boris Meinardus
Рет қаралды 340 М.
BS-8. Single Element in Sorted Array
22:16
take U forward
Рет қаралды 172 М.
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 487 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 438 М.
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 45 МЛН