21 Search in Row wise And Column wise Sorted Array

Sdílet
Vložit
  • čas přidán 9. 04. 2020
  • SEARCH IN A ROW WISE AND COLUMN WISE SORTED MATRIX:
    Given an n x n matrix and a number x, find the position of x in the matrix if it is present in it. Otherwise, print “Not Found”. In the given matrix, every row and column is sorted in increasing order. The designed algorithm should have linear time complexity.
    Example :
    Input : mat[4][4] = { {10, 20, 30, 40},
    {15, 25, 35, 45},
    {27, 29, 37, 48},
    {32, 33, 39, 50}};
    x = 29
    Output : Found at (2, 1)
    PROBLEM STATEMENT LINK:www.geeksforgeeks.org/search-...
    PLAYLIST LINK: • Binary Search | Interv... .
    ------------------------------------------------------------------------------------------
    Here are some of the gears that I use almost everyday:
    🖊️ : My Pen (Used in videos too): amzn.to/38fKSM1
    👨🏻‍💻 : My Apple Macbook pro: amzn.to/3w8iZh6
    💻 : My gaming laptop: amzn.to/3yjcn23
    📱 : My Ipad: amzn.to/39yEMGS
    ✏️ : My Apple Pencil: amzn.to/3kMnKYf
    🎧 : My Headphones: amzn.to/3kMOzM7
    💺 : My Chair: amzn.to/385weqR
    🛋 : My Table: amzn.to/3kMohtd
    ⏰ : My Clock: amzn.to/3slFUV3
    🙋🏻‍♀️ : My girlfriend: amzn.to/3M6zLDK ¯\_(ツ)_/¯
    PS: While having good gears help you perform efficiently, don’t get under the impression that they will make you successful without any hard work.

Komentáře • 214

  • @shubhamsth
    @shubhamsth Před 2 lety +18

    Bhai this is amazing, 4 minutes into the video and I was able to solve the problem all by myself. I learned something new today. Thanks Aditya.
    One more thing, at 15:31 we don't actually need to check for (i >= 0) and (j

  • @princegarg1086
    @princegarg1086 Před 3 lety +87

    every second is worth to spend watching Aditya Verma content :).

    • @mohammadanas1087
      @mohammadanas1087 Před 3 lety +2

      Absolutely true....his way of explanation is just way too awesome🔥

    • @adityaramakrishnan969
      @adityaramakrishnan969 Před 3 lety +1

      Absolutely 🔥
      Only thing I'm disappointed is that backtracking video's are not yet uploaded 🥺😭😭😭

  • @devanshmesson2777
    @devanshmesson2777 Před 2 lety +6

    I loved the way how you explained this approach by relating it to the method of binary search. Previously, I was not able to understand why are we starting from top-right corner, but now it is making sense while relating it to binary search!
    Thank you so much! ❤️

  • @adityaaggarwal7140
    @adityaaggarwal7140 Před 3 lety +1

    I started this question on my own before watching the solution, what I did was applied binary search on each column starting from top right corner to find the key. I thought I got a really good solution but then I saw your solution. Great approach!

  • @roshanraut2918
    @roshanraut2918 Před 4 lety +92

    Finally completed this playlist sequentially with practice problema on gfg and codeforces.
    Waiting for next playlist on backtracking.
    Please upload them soon.
    Thanks for such loaded content videos.
    Helping a lot for internship preparations.

    • @skullcode8856
      @skullcode8856 Před 3 lety +1

      1 left

    • @ayushop1921
      @ayushop1921 Před 2 lety +11

      lagi kya bhai internship

    • @danishraza3061
      @danishraza3061 Před 2 lety +8

      Bhai lagi thi k nhi internship? sahi sahi batana .....tbhi last wala video dekhnge

    • @pratikdas1780
      @pratikdas1780 Před rokem +1

      @@danishraza3061 🤣

    • @sankalparora9374
      @sankalparora9374 Před rokem +2

      Waiting for next playlist on Backtracking... Never gonna happen! Sadly.

  • @ankeshgoenka3680
    @ankeshgoenka3680 Před 2 lety +4

    Hi Aditya, I was struggling with DSA and CZcams recommended me your channel, thanks a lot to you and CZcams for such a quality content

  • @coder3912
    @coder3912 Před 4 lety +19

    Thank you so much brother for such an step by step learning approach..This was the thing I was looking on the internet, from months and Thank god! finally found your channel. Your way of teaching is incredible..especially when you build the concepts gradually and then the topic and problems based on it becomes so easy to understand & apply for us..Hope you make same playlist for [Tree,Graph,Backtracking,Recursion], It will really help students like me to get such a structured learning right from basic to advance in one playlist on this particular topics mentioned. Its a genuine request to make it as soon as possible because on internet information is available on each topics but it is in scattered way(no proper way of concept building is available).Hope you can understand our feelings and from your busy schedule you can devote some time & make a complete end to end playlist on the above mentioned topics.

  • @sakshiramsinghani5284
    @sakshiramsinghani5284 Před 3 lety +5

    The explanation was awesome. It would be really helpful if you upload a video on finding median in a row wise sorted matrix without using extra space.

  • @minakshikudalkar557
    @minakshikudalkar557 Před 3 lety

    I finally understood this one! Great explanation..Thank you :)

  • @praffulpatil007
    @praffulpatil007 Před 2 lety +2

    gone through the whole playlist this is pure gold, thanks Aditya for taking the effort

  • @imranwahid9871
    @imranwahid9871 Před 3 lety

    Thanks, sir. Your way of explanation is truly awesome.

  • @pallavighosh6988
    @pallavighosh6988 Před 3 lety

    very well explained! thank you for uploading these content.

  • @ameerulshah4098
    @ameerulshah4098 Před 4 lety +44

    Awesome! Please take up recursion/backtracking next.
    Also checking for while(i=0) will suffice.

  • @aceace343
    @aceace343 Před 3 lety +5

    Amazing playlist man, just one suggestion for this solution, it d be better if we use the floor function on j to determine the column to enter into and subsequently use the ceil for traversing the row, that way it actually becomes a question of binary search

  • @vesperbeats6673
    @vesperbeats6673 Před rokem +1

    Moreover, we can shorten the bounds for while loop i.e while ( i=0 )
    As we know i always increase & j always decrease
    But, vice versa is not 🚫

  • @DeepakGupta-zz1vf
    @DeepakGupta-zz1vf Před 3 lety +5

    what i suggest is since our matrix is sorted in both ways(row and col). element at array[0][0] will be smallest and array[n-1][m-1] will be largest . So before going into loop we can check if keyarray[n-1][m-1] return -1; After doing this you can also change your while condition to while(j>=0 && i

    • @ishitakumari3766
      @ishitakumari3766 Před rokem

      Yeah checking first arr[0][0]>tar || arr[n-1][m-1]=0 && r

  • @shivansh709
    @shivansh709 Před rokem

    hands down one of the best approach ive ever seen

  • @redwanniloy2068
    @redwanniloy2068 Před 3 lety

    This is so great..
    love ur videos so much...it helps me a lot❤️

  • @prakharsinha6915
    @prakharsinha6915 Před 2 lety

    Worthy! content literally 🔥

  • @SAIKRISHNA-vm9zr
    @SAIKRISHNA-vm9zr Před 2 lety

    The way you explain the solution is awesome👍
    Thank you Adithya Verma...

  • @bipul2138
    @bipul2138 Před 3 lety +26

    I was asked this question in PayPal interview for SDE-1 when I was at college. Well explained.

    • @giit85
      @giit85 Před 3 lety +4

      Now in which company you're??

  • @hritiksharma3295
    @hritiksharma3295 Před 2 lety

    Thank you Bhaiya !
    Keep it up !
    You are helping a lot of people ... may god bless you !

  • @ITSuyashTiwari
    @ITSuyashTiwari Před 3 měsíci

    loved the way you explained❤

  • @abc-ym4zs
    @abc-ym4zs Před rokem

    genrally if video length is grater than 20 min most people tend to skip some of the part but while watching your and striver content it is totally opposite really superb

  • @ritwik121
    @ritwik121 Před 3 lety

    this is the best video on this topic. thanks sir

  • @mandeep2192
    @mandeep2192 Před 4 lety

    thanx for such tutorials

  • @paragroy5359
    @paragroy5359 Před 3 lety

    Nice explanation sir....thanks for the playlist

  • @NayeemKhan-gg2nc
    @NayeemKhan-gg2nc Před rokem

    very well explained with code great work

  • @abdullahshamoon1081
    @abdullahshamoon1081 Před 2 lety

    Bhai .. ek no. Explanation.. watched your video for the first time , ab se aapka hi vdo dekhna h 🤟🤟😇

  • @kishorjha5028
    @kishorjha5028 Před 3 lety

    thanks bhaiya ....!
    no word for u....u are really amazing and very helpfull ....!

  • @bhuwaneshnainwal8323
    @bhuwaneshnainwal8323 Před 3 lety

    Thank you big brother. ❤️

  • @YashHalgaonkar
    @YashHalgaonkar Před 3 lety +1

    Please upload a playlist on Backtracking as soon as possible!

  • @AnandPandeyHere
    @AnandPandeyHere Před 4 lety +1

    Nice Explaination

  • @pankajsahu1624
    @pankajsahu1624 Před rokem

    woow bhiya aap pen ghumake concept clear kr dete ho
    love you!
    thanku
    kafi maja ara h 2 pointer sliding window tree etc pr bhi banao videos pen ghumake

  • @ankoor
    @ankoor Před 4 lety +8

    @Aditya Verma, please make video on "Median of 2 sorted arrays of different lengths using O(1) space", based on some videos it seems like Binary Search can be applied

  • @hasleindia7027
    @hasleindia7027 Před 2 lety

    Best playlist for bs. Thanks.

  • @varungoel6981
    @varungoel6981 Před 3 lety

    Traversing can also be started from bottom left and moving sideways or upwards.

  • @subhajitdutta5216
    @subhajitdutta5216 Před 4 lety +5

    Bhaiya, please make videos on string algorithms (advanced ones)

  • @VipinKumar-us1sr
    @VipinKumar-us1sr Před 3 lety

    can we have a better sol with time complexity of O(nlogn), If we binary search for the key in each row ?

  • @avinashmishra51
    @avinashmishra51 Před rokem

    Bht sahi hai yr bhai..tester ko bhi coding sikha diye

  • @rahul_singh_rajput3292

    bas explanation hi enough hai sir aapka 😏😍.. code toh hum 80% apki explanation se kr dete hai... ❤

  • @suyashsharmaable
    @suyashsharmaable Před 3 lety

    bahut achi approach, maza aya dekhne me.

  • @108_adityakumar6
    @108_adityakumar6 Před 2 lety +3

    Most optimal for this problem is using binary search with time complexity O( log(m ) + log (n) )

  • @poojagiri9884
    @poojagiri9884 Před rokem +1

    Really like your way of teaching. Can you please make a playlist for time and space complexity?

    • @SanjuKumar-hk8yy
      @SanjuKumar-hk8yy Před rokem +1

      Watch this for time complexity analysis czcams.com/video/oPvT1IBXNxM/video.html

  • @user-ni1bt3fx8y
    @user-ni1bt3fx8y Před rokem

    very nice explanation

  • @panavkumar9009
    @panavkumar9009 Před 2 lety +1

    If the array would have been sorted only row wise and also the last element of the previous row must be less than the first element of the current row then the thought process could have been better.
    We could have searched the first column first. If we found the key in that column then we could return it straight but if not then we could have first found the floor of the key in the first column and then in that row we could have again applied binary search to find the element. This would have used the concept of both finding the floor and binary search. The worst case time complexity of this solution would be (log n + log m) where n is the number of rows and m is the number of columns

  • @Kuriocity
    @Kuriocity Před 3 lety

    Next level 🔥

  • @khsprashanth
    @khsprashanth Před 3 lety

    BHAIYA PLEASE UPLOAD VIDEOS ON TREES AND GRAPHS! THOSE ARE MOST REQUIRED VIDEOS FOR US THAN ANYTHING ELSE!

  • @BidhaN...
    @BidhaN... Před rokem

    Thank you bhaiya very easy explanation

  • @shivimathur6167
    @shivimathur6167 Před rokem

    finding coding easy through ur lectures .

  • @ambastaaashishkumarsanjayk2729

    is simply applying the binary search on row and column a good approach?

  • @shreygarg2707
    @shreygarg2707 Před 3 lety

    This can be done in O(log(m*n)). It is basically an array with m*n elements (divided into m rows). So binary search on this array. for any mid the element to compare is matrix[mid/n][mid%n]. And O(log(m*n)) is O(log(m)+log(n)) which is lesser than O(m+n).

    • @uptonogood300
      @uptonogood300 Před 2 lety +8

      nope ! thats a different version of this problem which guarantees first element of a row is always greater than the last element of the previous row.

  • @ayusharyan683
    @ayusharyan683 Před 2 lety +2

    There could be 2 kinds of sorted 2d arrays. The one which is mentioned in the video there could not be any better approach than the approach which is being told in the video i.e staircase search.
    arr = {{10, 20, 30, 40},
    {15, 25, 35, 45},
    {27, 29, 37, 48},
    {32, 33, 39, 50}};
    Here array is sorted and but there is different kind of sorted arrays with a condition that the first integer of each row is greater than the last integer of the previous row. This you will find in question 74 2D matrix Leetcode.
    Here you can apply binary search on row and col and can reduce the time complexity to O (log n * log m)
    matrix = [[1,3,5,7],
    [10,11,16,20],
    [23,30,34,60] ]
    We can apply binary search to matrix but not to arr. for arr we will use staircase search which gives time complexity O(n + m)

    • @ayusharyan683
      @ayusharyan683 Před 2 lety

      Or You can store the whole matrix in a separate array and find the element using binary search and find row and col my doing mod with number of rows and col respectively.
      Time : log( m * n )
      as we are finding our ele in the whole matrix just stored in a single 1d array.
      ** We can also directly access the matrix by using mod and it will decrease the space to O(1)

  • @JameS00989
    @JameS00989 Před rokem

    bhai maja aa gaya yaar Aditya pls make more playlist like these

  • @ankitdas6009
    @ankitdas6009 Před 3 lety

    Applying binary search on every row is also an intuitive approach that can bring down the time complexity to O(nlog(m)). And thanks bhaiya for all the videos.

    • @revaanmishra
      @revaanmishra Před 2 lety +1

      complexity of the code is O(m + n) and its the best possible for this question.

    • @piyushaneja7168
      @piyushaneja7168 Před 2 lety

      @@revaanmishra can't we use binary search to find row first then the same method to find appropriate column number .so will complexity reduce to max(logn,logm)?

    • @kuntasunny8688
      @kuntasunny8688 Před 2 lety +2

      yes that is one of the intuitive approach, so in interview , first we can say O(m*n) and then optimise to nlogm and then optimise to o(m+n)

  • @akashkarn8429
    @akashkarn8429 Před rokem

    Thank you bade bhai :)

  • @anuritarawat8422
    @anuritarawat8422 Před 4 lety +1

    can we use the bottom left corner also I guess in this case it will give solution more quickly and what we are doing for columns we can do the same for rows also! I think it will be same pls correct me if I am wrong thanks☺️

  • @nrted3877
    @nrted3877 Před 22 dny

    Thanks aditya bhai

  • @lakshsinghania
    @lakshsinghania Před rokem

    but sir why did we started the searching of the element in the matrix from top right corner
    why not top left or bottom left/right ? any specific reason ??

  • @YASH_KHICHARIYA
    @YASH_KHICHARIYA Před měsícem

    This could be beautifully done with the help of recursion by tranversing diagonally and eliminating top left sub matrix and bottom right submatrix.

  • @viratkohli6572
    @viratkohli6572 Před 4 lety

    Thanks bhaiya

  • @jeal0uspengu1n
    @jeal0uspengu1n Před 3 lety +3

    Just wanted to ask , can't we apply binary search here .
    We'll start from arr[mid_row][mid_col] and then first check between which two columns does our element lie and then similarly which two row , finally arriving at the element , this way the time complexity would be log(n)*log(m)?

  • @gyandeepsharma3378
    @gyandeepsharma3378 Před 3 lety +1

    can we apply here binary search on column using floor and ceil after that binary search on the row?

  • @abhigyansharma9108
    @abhigyansharma9108 Před 3 lety

    Love you bhai....

  • @calderanoadi5982
    @calderanoadi5982 Před 2 lety

    Gawddd level teacher 🔥🔥🔥🔥🔥🔥

  • @Itachibatman
    @Itachibatman Před rokem

    best video !

  • @MMNayem-dq4kd
    @MMNayem-dq4kd Před rokem

    Thanks

  • @praxlannister7859
    @praxlannister7859 Před 4 lety +5

    Bhai Maja Aa gaya Approach Dekh Ke
    Waiting For Your Flipkart Coding Round BS Question Video

    • @TheAdityaVerma
      @TheAdityaVerma  Před 4 lety +3

      Will be uploading in few hours brother !!

    • @praxlannister7859
      @praxlannister7859 Před 4 lety

      @@TheAdityaVerma Thanks Bhai

    • @roshanraut2918
      @roshanraut2918 Před 4 lety +1

      @@TheAdityaVerma
      Waiting for recursion and backtracking videos.
      Good work. Keep going.

  • @yashrathore7507
    @yashrathore7507 Před 2 lety

    The Horizontal lines which you have created on the table are for us I know that :-)

  • @subhajitdutta5216
    @subhajitdutta5216 Před 4 lety +4

    Thank u bhaiya😌👐,

  • @md.ibrahimkhaleelullah9522

    Excellent

  • @mrditkovich2339
    @mrditkovich2339 Před 2 lety

    We can use binary search on this:
    when u are in a particular column, find the floor of the target --> this becomes the new column
    when u are in a particular row, find the ceiling of the target --> this becomes the new row

    • @coding8453
      @coding8453 Před 2 lety

      It will increase the Time Complexity

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi Před 4 lety +2

    waiting for backtracking tutorials !!

  • @naraindaskeswani8623
    @naraindaskeswani8623 Před 3 lety

    Once we found the item greater than key, then on that column we can apply binary search ( as column is also sorted ) leading to O(n + logm) complexity

    • @vishalsiddha6637
      @vishalsiddha6637 Před 3 lety

      Yes and i think this approch is better than this video's one.

    • @somiljain7996
      @somiljain7996 Před 3 lety +1

      Doesn't matter because asymptotically both are same.

    • @revaanmishra
      @revaanmishra Před 2 lety

      @@somiljain7996 complexity of the code is O(m + n) and its the best possible for this question.

  • @Raj10185
    @Raj10185 Před rokem

    This approach is good but i have one more approach for this problem => treating the 2D matrix as 1D only :-
    we have to just find out its equivalent row and column and formula for same is
    row = mid/m;
    column = mid % m ;
    code is :-
    class Solution {
    public:
    bool searchMatrix(vector& M, int target) {



    int n = M.size();
    int m = M[0].size();
    int low = 0;
    int high = m*n - 1;
    while(lowtarget)
    {
    high = mid -1;
    }
    else
    low = mid +1;
    }
    return 0;




























    }
    };
    Please like if you find it useful :)

  • @sumitprakash130
    @sumitprakash130 Před 3 lety

    Sir, Please make videos on trees and graph

  • @noobevsprorelation6838

    Sir ek series baisae question pae bhi banana jinko bit main sochh kar solve kartae hai
    Bit manipulation bala

  • @abhinavkumar6584
    @abhinavkumar6584 Před 2 lety

    Can anyone tell why did we started from top right. is it because that number will always lie in the middle . so can we also do it from bottom left? or is there some other reason for this? and please tell us the approach on how did u think of this. Thanks

  • @ankiiiiit7866
    @ankiiiiit7866 Před rokem

    Or vedios bnao aditya , osm yr, always bnate raho vedios bhai

  • @codetogether24x7
    @codetogether24x7 Před 2 lety

    One solution that came to my mind is:
    A.) First find the floor value index of the key in first row using binary search(time complexity will be O(log n)).Here index will be 1(20 is present).
    B.) Then once again apply binary search on that particular column.
    can we the problem this way???

  • @prateekshukla9017
    @prateekshukla9017 Před rokem

    we can further reduce the time complexity by using a binary search in every row and column.
    We'll now have a code which will run in O(log(n) + log(m)).

  • @KamleshSharma-si2rq
    @KamleshSharma-si2rq Před 3 lety

    is it two pointer approach or Binary Search?

  • @gauravraj2604
    @gauravraj2604 Před 3 lety

    Now I understand why they call u "god of DS & Algorithm"

  • @aakarshitrekhi8071
    @aakarshitrekhi8071 Před 3 lety

    awesome

  • @spi2der
    @spi2der Před 4 lety +1

    Bhai sliding window ka bhi video banade na pls.

  • @vatsalya3105
    @vatsalya3105 Před 4 lety +1

    More videos plz , sab dekh lia😭😭

  • @subscriber6571
    @subscriber6571 Před 2 lety +1

    can this be done in log n + log m ?

  • @shauryakumar6372
    @shauryakumar6372 Před rokem +1

    I think its a little better solution and better way for implementing binary search
    int m = matrix.size();
    int n = matrix[0].size();
    int low = 0;
    int high = n-1;
    int row = 0;
    while(low

  • @InfinteMotivation
    @InfinteMotivation Před rokem

    your thanks was very cold 😂

  • @rishabsoni256
    @rishabsoni256 Před 4 lety +8

    Thanks pls do code forces round question of dp using ur approach

    • @RudraSingh-pb5ls
      @RudraSingh-pb5ls Před 4 lety

      Can you the links for those codeforces problems ?

    • @rishabsoni256
      @rishabsoni256 Před 4 lety

      @@RudraSingh-pb5ls bro u can use dp tag in problemset in codeforces

    • @RudraSingh-pb5ls
      @RudraSingh-pb5ls Před 4 lety

      @@rishabsoni256 thnx buddy but I need one more help. If you have watched Aditya's DP video of min coin change problem then can you share your code for top down approach, cause I m terribly stuck at creating a large 2d global array of variable size depending upon user inputs.

  • @usmanaliansari5223
    @usmanaliansari5223 Před rokem +1

    we can do this in (log(n)*log(m))🥰

  • @jamtaranumber2841
    @jamtaranumber2841 Před 2 lety +1

    0:01 what an intro bruhhh :)

  • @mehershrishtinigam5449
    @mehershrishtinigam5449 Před 2 lety +1

    What happened to the 20th video?

  • @nehalahane457
    @nehalahane457 Před rokem

    nice

  • @pinkkitty6553
    @pinkkitty6553 Před 10 měsíci +2

    Java code:
    static boolean search(int matrix[][], int n, int m, int x)
    {
    int row = 0;
    int col = m-1;
    while(row < n && col > -1)
    {
    if(matrix[row][col] == x)
    return true;
    else if(matrix[row][col] < x)
    row++;
    else
    col--;
    }
    return false;
    }

  • @hue.huehuehue
    @hue.huehuehue Před rokem

    why do we need to start from top right?

  • @no-body7286
    @no-body7286 Před 3 lety

    i think we can reduce the complexity further

  • @ankoor
    @ankoor Před 4 lety +3

    @Aditya Verma, please make video on "Search Kth Smallest Element in a Row wise and Column wise Sorted Matrix using Binary Search"

  • @vishalsiddha6637
    @vishalsiddha6637 Před 3 lety

    if we use Binary search on every column isn't it become O(mlogn) in worst and O(logn) in best???

  • @anmolsharma6961
    @anmolsharma6961 Před 4 lety +3

    Bhaiya vo DP ki Grid aur LIS vali videos ayengi kya ?