BS-26. Find Peak Element-II | Binary Search

Поділитися
Вставка
  • Опубліковано 5 січ 2025

КОМЕНТАРІ • 141

  • @kathakalisaha9735
    @kathakalisaha9735 10 місяців тому +40

    Those who watches your videos completely will never say you use cpp , they are the ones who dont watch the intuition part, directly jump into the code

  • @iammysterious-ud9cf
    @iammysterious-ud9cf Рік тому +198

    Striver..... PLEASE CONTINUE A TO Z DSA Course....From Step 5(strings) , it is incomplete(no videos for the questions)... Your videos are very useful in intuition building and finding approach... PLEASE DONT LEAVE US IN THE MIDDLE😢😢😢...I hope you respond to the requests for the guys like me..

  • @ritikadhangar2979
    @ritikadhangar2979 3 місяці тому +3

    Sir I can't express my job while taking your lecture. before that I am scared of DSA but now I enjoy due to you thankyou so muchhh

  • @sundaramkumarsingh8448
    @sundaramkumarsingh8448 Рік тому +7

    clear cut explanation. Thank you Striver

  • @LemesaElias
    @LemesaElias 3 місяці тому +4

    Even if you didn't provide the pseudo code and just went to implement the C++ code, I don't think it matters because it's actually your explanation of the solutions that actually helps anyone that considers him/herself a problem solver or anyone that's learning DSA. And as someone who uses/used multiple languages, I think given the explanation , any code from any programming language is pretty much understandable for someone that knows at least one programming language.

    • @divyanshusingh6008
      @divyanshusingh6008 Місяць тому

      Yup , thats why i dont even watch his pseuodo code , because i also consider this as a cheating

  • @saibingi6980
    @saibingi6980 Рік тому +65

    I request you to please make videos in Bit Manipulation, Heaps, Strings, and Disjoint-set in the upcoming sessions.

    • @godson200
      @godson200 Рік тому +5

      He already has about 10 videos on disjoint set in the graph series. Maybe that can help.

    • @rekhabisht6709
      @rekhabisht6709 Рік тому +9

      Phle itna khatm krle

    • @dumpster-jackson
      @dumpster-jackson 6 місяців тому

      @@rekhabisht6709 Ab??

  • @zerobear-xf7qh
    @zerobear-xf7qh 5 місяців тому +6

    nice solution i dont think i would come up with this solution on my own

  • @ayushagarwal5271
    @ayushagarwal5271 6 місяців тому +6

    This man is a genius!

  • @smitj5404
    @smitj5404 Місяць тому

    JUST WOW! SO GREAT INTUITION BUILDING, MUCH LOVE TO YOUR WORK SIR :)

  • @cenacr007
    @cenacr007 Рік тому +20

    Array and Strings are the most important for interviews, please make a playlist on strings.

  • @stith_pragya
    @stith_pragya 10 місяців тому +1

    Thank You Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @securelyinsaycure
    @securelyinsaycure 8 місяців тому

    Striver you are a godsend to so many of us! Thank you 🙏

  • @LearnwithEase20
    @LearnwithEase20 11 місяців тому +1

    please make videos on string and other topic it is much needed no one explains like you!🙂

  • @shwetachoudhary9003
    @shwetachoudhary9003 2 місяці тому +1

    Sir plz complete this series🥺🥺 i have learned a lot from this series so plz don't leave me in middle🙏🏻🥺

  • @JothiprakashThangaraj
    @JothiprakashThangaraj 5 місяців тому

    sudo code is sufficient for any developer to write code in any language , understood thanks a lot !!

  • @utkarshtrivedi9866
    @utkarshtrivedi9866 6 місяців тому +1

    Understood, even just looking at pseudo code, I was able to code it myself.

  • @adilkevin6220
    @adilkevin6220 9 місяців тому +1

    ARTICLE column is empty for this problem. Please attach it.

  • @tanvirkekan6111
    @tanvirkekan6111 10 місяців тому +3

    This is an interesting soln:
    x,y = 0, len(mat[0]) - 1
    while x < len(mat) or y >= 0:
    if x + 1 < len(mat) and mat[x + 1][y] > mat[x][y]:
    x += 1
    elif y - 1 >= 0 and mat[x][y - 1] > mat[x][y]:
    y -= 1
    else:
    break
    return [x, y]

  • @jeehub041
    @jeehub041 Рік тому +3

    What an approach mind blown 🙌🙌

  • @anonymous-ms2en
    @anonymous-ms2en 6 місяців тому +1

    love you striver ,clear explaination

  • @AshishSingh-he2qo
    @AshishSingh-he2qo 5 місяців тому +1

    Understood 🎉🎉

  • @iammysterious-ud9cf
    @iammysterious-ud9cf Рік тому +9

    Please Start Linked Lists

  • @Fe-ironman
    @Fe-ironman 2 місяці тому +1

    finally didn't understood one of your videos lol....understood all of the rest in series one remaining tho

  • @JagadeeswarN-ur7of
    @JagadeeswarN-ur7of Місяць тому

    Great Explanation!! Thanks!!

  • @GeetainSaar
    @GeetainSaar 5 місяців тому

    your skin glowing in whole video

  • @momentcoder
    @momentcoder Місяць тому

    Understood 😀

  • @harshit.53
    @harshit.53 8 місяців тому +2

    i don't understand why people complain about only using cpp...first of all he discusses the idea behind the code and then writes a simple pseudo code...secondly, even if he uses cpp, any good coder can easily convert that code into any other language, maybe in other languages the function name and syntax is different, so u just need to use the functions provided by ur language

  • @shivamdubey3798
    @shivamdubey3798 Рік тому +2

    Bhaiya please video continue to upload Kara Karo mera ye week topic haa🙏🙏

  • @rahulgoel7652
    @rahulgoel7652 10 місяців тому +2

    I am unsure if first method would have O(mn *4) TC. You are traversing the matrix once, i.e. O(mn). For each element, checking the neighbors is constant time, so it wont be 4 times the number of elements

    • @tusharyadav5874
      @tusharyadav5874 4 місяці тому

      We will say it as O(m*n*4). So thats why he said we can we can slightly improve this by just finding the largest element in matrix . Then will be not using that 4 .

    • @rahulgoel7652
      @rahulgoel7652 4 місяці тому

      @@tusharyadav5874 I am sorry but I didn't understand what you wrote.

  • @vcfirefox
    @vcfirefox Місяць тому

    superb

  • @AYUSHNIGAM97
    @AYUSHNIGAM97 2 місяці тому

    Can't we find the vertical peak with Binary search as well and reduce complexity to O(logn * logm)?

  • @TheAI-Tutor
    @TheAI-Tutor 5 місяців тому

    But instead of traversing vertically, can't we just Traverse horizontally and find the maximum element to do STL and then check for the upper and lower element? and discard the upper half/lower half then?

  • @gugulothsrilakshmi8729
    @gugulothsrilakshmi8729 11 місяців тому +1

    written article not there in description

  • @vikassinghbisht7305
    @vikassinghbisht7305 8 місяців тому

    striver you are just too good

  • @sidharthsharma341
    @sidharthsharma341 Рік тому +4

    What are the upcoming topics?after this

  • @NazeerBashaShaik
    @NazeerBashaShaik 8 місяців тому

    Understood, thank you.

  • @MJBZG
    @MJBZG 6 місяців тому +1

    didn't really understand but will try more

  • @ashish7604
    @ashish7604 3 місяці тому

    don't you think if we keep udpating ele with bigger ele found then it will take O(m+n) time better than current time comp

  • @ddevarapaga5134
    @ddevarapaga5134 5 місяців тому

    Understood brother

  • @amanpatel6203
    @amanpatel6203 Рік тому

    long awaited

  • @hashcodez757
    @hashcodez757 10 місяців тому

    Understood!!

  • @RitikaMajumdar__F
    @RitikaMajumdar__F 10 місяців тому

    bhaiya the java cpp notes's link is not given in the description. Please do share it.

  • @nishantsatere9350
    @nishantsatere9350 7 місяців тому

    understoddddddddd

  • @t3ch_r4id
    @t3ch_r4id 4 місяці тому

    class Solution {
    public:
    int maxElement(vector& arr,int n,int m, int col){
    int index = -1;
    int maxele = INT_MIN;
    for (int i = 0; i < m; i++){
    if(arr[i][col] > maxele){
    maxele = arr[i][col];
    index = i;
    }
    }
    return index;
    }
    vector findPeakGrid(vector& mat){
    //n no of cloumns are there
    int n = mat.size();//size of each row
    //m no of rows are there
    int m = mat[0].size();//size of each column
    int low = 0, high = n - 1;
    while(low = 0)? mat[row][mid - 1] : -1;
    int right = (mid+1 < n)? mat[row][mid + 1] : -1;
    if(left < mat[row][mid] && mat[row][mid] > right){
    return {row, mid};
    }
    else if(left > mat[row][mid]){//cut out right part
    high = mid - 1;
    }
    else{//cut out the left part
    low = mid + 1;
    }
    }
    return {-1, -1};
    }
    }; can anyone find the mistake please🙏 it's showing runtime error (something overflow) for a very large input matrix

  • @DeadPoolx1712
    @DeadPoolx1712 4 місяці тому

    UNDERSTOOD;

  • @vishious14
    @vishious14 11 місяців тому

    Understoood !!!!!

  • @sanketatmaram
    @sanketatmaram 4 місяці тому

    understood!

  • @mithileshkumbhar7968
    @mithileshkumbhar7968 8 місяців тому

    Striver......The link to this problem on your page is not opening to view the code and intuition.

  • @viveksoni3269
    @viveksoni3269 7 місяців тому

    Very Nice!!!!

  • @swagcoder
    @swagcoder Рік тому +1

    For this problem, in leetcode , it’ll fail one test case where there are multiple peaks in the same row, that one is different scenario though, in this problem it is assumed that there will be only one peak per row , because you can’t eliminate directly in case of multiple peaks.

    • @takeUforward
      @takeUforward  Рік тому +10

      this works for it also, because even if it has multiple peaks, it will find ,watch the first video to understand why it works

    • @swagcoder
      @swagcoder Рік тому +3

      Thanks for replying Stiver, Understood

  • @SurajYadav-bt9jb
    @SurajYadav-bt9jb 6 місяців тому

    I have one doubt regarding BS that it can apply on sorted ones, but here 2D Matrix is not sorted so how can we apply BS on it , please resolve my confusion

  • @mahimagautam1810
    @mahimagautam1810 Рік тому +3

    class Solution {
    public int findMaxIndex(int[][] mat,int mid,int n) {
    int maxi = -1;
    int ind = -1;
    for (int i = 0; i < n; i++) {
    if (mat[i][mid] > maxi) {
    maxi = mat[i][mid];
    ind = i;
    }
    }
    return ind;
    }
    public int[] findPeakGrid(int[][] mat) {
    int ans[] = {-1,-1};
    int n = mat.length;
    int m = mat[0].length;
    int low =0;
    int high = m-1;
    while(low=0 ? mat[index][mid-1]:-1;
    int right = mid+1left && mat[index][mid]>right){
    return new int[]{index, mid};
    }
    else if (mat[index][mid]

  • @DeepakPatel-d5v
    @DeepakPatel-d5v 8 місяців тому

    Thanks a lot Bhaiya

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 10 місяців тому

    Thank you Bhaiya

  • @VarshaSingh-hi2sb
    @VarshaSingh-hi2sb 4 місяці тому

    Can there be a case where the part which have omitted peak element was present there and not where we are heading towards?

    • @brp3522
      @brp3522 4 місяці тому

      11:35 This is the part that answers your question

  • @lokeshnegi5051
    @lokeshnegi5051 3 місяці тому

    even if you are writing the code in cpp what's wrong in that it's so easy nowadays to convert it to any language of your choice with so many AI tools, the main part is the intuition

  • @nayankhuman1043
    @nayankhuman1043 5 місяців тому

    Understood :)

  • @oyeshxrme
    @oyeshxrme 4 місяці тому

    thanks bhaiya

  • @Harshitbejsbsksjnw
    @Harshitbejsbsksjnw 6 місяців тому

    Just a little doubt, when you say we can find the maximum element in the matrix and it will surely be the peak as well. What about the fact that there can be two maximums in the matrix and both lie adjacent to each other? And the question says that the element should be strictly greater than its adjacents......

  • @VishalKumar-uk5uc
    @VishalKumar-uk5uc Рік тому

    Ek month wait Kiya ek video ke liye

  • @soumiyamuthuraj3516
    @soumiyamuthuraj3516 5 місяців тому

    awesome

  • @chandandasari6351
    @chandandasari6351 Рік тому +1

    bro plzz make a videos on string problems it was missed in play list bro strivers a to Z sheet plzz upload videos

  • @anusmitahait2329
    @anusmitahait2329 Рік тому

    Problem seemed so intimidating at the begining, later on was simple variation of peak.

  • @daniyalhussain5231
    @daniyalhussain5231 Рік тому

    Solution in C++:
    class Solution {
    public:
    int findMaxIndex(vector&mat,int col,int rows)
    {
    int maxi=INT_MIN;
    int maxIndex;
    for(int i=0;imaxi)
    {
    maxi=mat[i][col];
    maxIndex=i;
    }
    }
    return maxIndex;
    }
    vector findPeakGrid(vector& mat)
    {
    vectorans(2);
    int rows=mat.size();
    int cols=mat[0].size();
    int low=0,high=cols-1;
    while(low=0)left=mat[maxElementRowIndex][mid-1];//edge case
    int right=-1;
    if(mid+1left&&curr>right)
    {
    ans[0]=maxElementRowIndex;
    ans[1]=mid;
    return ans;
    }
    else if(curr

  • @AbdulRehman-ee8zc
    @AbdulRehman-ee8zc Рік тому

    sir khan thay aap
    aap hi k lecture ka intizar tha

  • @binarybardic4667
    @binarybardic4667 Рік тому

    Thanks a lot......!

  • @anand_yv
    @anand_yv Рік тому

    Understood

  • @learn_in_shorts
    @learn_in_shorts Рік тому

    Bhaiya mey aapka binary tree ka playlist se tree padh rha hu but jab aap iss video mey jaise padhete hai to sab samjh aata hai but tree wala mey aisa kuch samjh nahi aa rha hai jab jab aap board pr padhye jab tak to sab samjh aaya but uske baad kuch jyada nahi aaya 😊 plz bhaiya aap iss video ki format mey videos banaya kariye plz and do something trees also

  • @guttulavybhav1030
    @guttulavybhav1030 6 місяців тому

    striver please upload videos on strings it is difficult to go to others videos....

  • @surbhirathore._
    @surbhirathore._ 6 місяців тому

    Done!!!

  • @Gokul-gkl
    @Gokul-gkl Місяць тому

    is it a sorted matrix?

  • @codeman3828
    @codeman3828 11 місяців тому

    Undertsood

  • @viratlover6206
    @viratlover6206 Рік тому

    understood

  • @saketjaiswalSJ
    @saketjaiswalSJ Рік тому +3

    linked list striver bhaiya

  • @ranjeetkumaryadav2967
    @ranjeetkumaryadav2967 4 місяці тому

    bhaiya please upload article of this problem

  • @GeetainSaar
    @GeetainSaar 5 місяців тому +1

    give its article also

  • @prikshit_sharma4071
    @prikshit_sharma4071 Рік тому +2

    Bhaiya linkedlist start krdo iske badh

  • @charuprabha9696
    @charuprabha9696 6 місяців тому

    🙇‍♀🙇‍♀🙇‍♀🙇‍♀🙇‍♀🙇‍♀

  • @shaiksoofi3741
    @shaiksoofi3741 7 місяців тому

    tq

  • @lakshsinghania
    @lakshsinghania 4 місяці тому

    bhaiya i did convert from 2d array to 1d array but only 45/55 test cases is passing pls help
    class Solution {
    public:
    vector findPeakGrid(vector& matrix) {
    // binary search on answer
    int m = matrix.size();
    int n = matrix[0].size();
    // tc = o(log(m*n))
    int start = 0;
    // created an imaginery 1D array of size from 0 to n*m-1
    int end = (n * m) - 1;
    while (start 0) ? matrix[i - 1][j] : -1;
    int bottom = (i < m - 1) ? matrix[i + 1][j] : -1;
    int left = (j > 0) ? matrix[i][j - 1] : -1;
    int right = (j < n - 1) ? matrix[i][j + 1] : -1;
    if (mid > start && mid < n * m - 1) {
    if (matrix[i][j] > top && matrix[i][j] > bottom &&
    matrix[i][j] > left && matrix[i][j] > right) {
    return {i, j}; // Peak element found
    }
    // If left or top neighbor is greater, move to the left or top
    // half
    if (left > matrix[i][j] || top > matrix[i][j]) {
    end = mid - 1;
    }
    else {
    // Else move to the right or bottom half
    start = mid + 1;
    }
    }
    else if (mid == start) {
    // Only move right if there's no left neighbor
    if (matrix[i][j] > right && matrix[i][j] > bottom) {
    return {i, j};
    }
    else {
    start = mid + 1;
    }
    }
    else if(mid == end){
    // Only move left if there's no right neighbor
    if (matrix[i][j] > left && matrix[i][j] > top) {
    return {i, j};
    }
    else {
    end = mid - 1;
    }
    }
    }
    return {-1, -1};
    }
    };

  • @vijeshsshetty
    @vijeshsshetty Рік тому

    linked list series plss

  • @dhruvdonsahu9972
    @dhruvdonsahu9972 7 днів тому

    i nearly did it on my own , only failed in the case of multiple peaks :(

  • @PrinceKumar-ef1xf
    @PrinceKumar-ef1xf 7 місяців тому +1

    vector findPeakGrid(vector& mat) {
    int startCol = 0, endCol = mat[0].size()-1;
    while(startCol = startCol+1 && mat[maxRow][midCol-1] > mat[maxRow][midCol];
    bool rightIsBig = midCol mat[maxRow][midCol];
    if(!leftIsBig && !rightIsBig) return vector{ maxRow, midCol};
    else if(rightIsBig) startCol = midCol+1;
    else endCol = midCol-1;
    }
    return vector{-1,-1};
    }

  • @crazybro4383
    @crazybro4383 8 місяців тому

    8:00

  • @mohitsinghnitjsr2652
    @mohitsinghnitjsr2652 Рік тому

    how many videos more to come

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому

    public class solution {
    public static int []find Peek Grid(int [][]grid){
    int n=grid. length,m=grid [0].length;
    int l=0,r=m-1;
    while (lgrid [idx][mid +1]){
    r=mid;
    }else {
    l=mid-1;
    }
    return new int []{l,find max(grid [l])};
    }
    public static int find max(int []col){
    int index =0,n=col length;
    for(int i=0;icol[index])
    index =i;
    }
    return index;
    }
    };
    🎉❤

  • @infinity2creation551
    @infinity2creation551 Рік тому

    You are calling col ko row ...

  • @AkashBisht-cq3ys
    @AkashBisht-cq3ys 6 місяців тому

    1st july mon 9.13

  • @shivamkumar-hz7jt
    @shivamkumar-hz7jt Рік тому

    strings ke video upload krdo pls

  • @dhakadkumman4239
    @dhakadkumman4239 5 місяців тому

    100th comment of video

  • @rahulhembram4519
    @rahulhembram4519 Рік тому

    UnderStood

  • @RAJPATEL-ir7ly
    @RAJPATEL-ir7ly 6 місяців тому

    O(n+m) BFS
    class Solution {
    public:
    vector findPeakGrid(vector& mat) {

    int i = 0 ;
    int j = 0 ;
    int n = mat.size() ;
    int m = mat[0].size() ;
    while(true){

    int flag = 0 ;
    vector coordinates = {{1,0} , {0,1} , {-1,0} , {0,-1}} ;
    int maxi= -1 ;
    int newRow =0 ;
    int newCol = 0 ;
    for(auto coordinate : coordinates){
    int x = i+coordinate.first ;
    int y = j+coordinate.second ;
    if(x>=0 && y>=0 && x

  • @PriyashiChoudhary-l5k
    @PriyashiChoudhary-l5k 6 місяців тому

    Jeetu bhaiya equivalent

  • @Lucifer0872
    @Lucifer0872 Рік тому

    Biltu kaka Zindabad

  • @adityauditsingh5986
    @adityauditsingh5986 Рік тому +2

    first view xD

  • @cenacr007
    @cenacr007 Рік тому

    us

  • @AvaneeshSrivastava-lm5vj
    @AvaneeshSrivastava-lm5vj Рік тому

    understood. thank you. you should sleep a bit more.

  • @sanjayp.m7008
    @sanjayp.m7008 5 місяців тому

    why cant i use the logic of matching the row and col to mid and considering this matrix as a 2d array. i tried this but only 46 testcases passed in the leetcode qn.
    class Solution {
    public:
    vector findPeakGrid(vector& mat) {
    int n = mat.size();
    int m = mat[0].size();
    int low = 0;
    int high = n * m - 1;
    while (low = 0) ? mat[row][col - 1] : INT_MIN;
    int bottom = (row + 1 < n) ? mat[row + 1][col] : INT_MIN;
    int top = (row - 1 >= 0) ? mat[row - 1][col] : INT_MIN;
    if (el > right && el > left && el > top && el > bottom) {
    return {row, col};
    }
    if (right > el) {
    low = mid + 1;
    }
    else if (left > el) {
    high = mid - 1;
    }
    else if (bottom > el) {
    low = mid + 1;
    } else {
    high = mid - 1;
    }
    }
    return {-1, -1};
    }
    };

  • @ishangujarathi10
    @ishangujarathi10 Рік тому +1

    why am i getting a runtime error when i submit the solution on leetcode? @striver??

    • @ajithc5505
      @ajithc5505 Рік тому +1

      int left = mid-1>=0 ? mat[index][mid-1]:-1;
      int right = mid+1

    • @tejaswaniguttula5961
      @tejaswaniguttula5961 8 місяців тому

      @@ajithc5505
      Hi
      vector findPeakGrid(vector &g){
      // Write your code here.
      int rows = g.size();
      int cols = g[0].size();
      int low = 0, high = cols-1;
      while(low max_ele){
      max_ele = g[i][mid];
      index = i;
      }
      }
      int left = mid-1 >= 0 ? g[index][mid-1] : -1;
      int right = mid +1 < cols ? g[index][mid+1] : -1;
      if(max_ele > left && max_ele > right){
      return {index, mid};
      }
      else if(max_ele < g[index][mid-1]){
      high = mid-1;
      }
      else{
      low = mid+1;
      }
      }
      return {-1, -1};
      }
      };
      Though I applied edge cases why I am getting runtime error for this testcase??
      Test case:
      [[10,50,40,30,20],[1,500,2,3,4]]

    • @ParshvaDani
      @ParshvaDani 6 місяців тому

      @@ajithc5505 thanks brother it works now

  • @music-loverFam
    @music-loverFam 4 місяці тому

    Understood😊