17. BINARY SEARCH on 2d arrrays

 1.) ROW WITH MAX 1s-

given each row is sorted.
BRUTE- simple 2 loops
OPTIMAL- binary search lagalo to find index of 1st occurence of 1 in a row
--------------------------------------------------------------------------------------------------------------
2.) Search in 2d Matrix-(sorted rows consecutive)

BRUTE- use 2 loops
BETTER- check for only that row jisme target is between a[i][0] and a[i][c-1] 
OPTIMAL- 2d array ko hypothetically 1d me karke binary search lagalo
---------------------------------------------------------------------------------------------------------------
3.) Search in matrix(sorted row and col but not consecutive)
BETTER-use binary search for every row
OPTIMAL-(time: N+M)







---------------------------------------------------------------------------------------------------------------
4.) Peak elements in matrix-
BRUTE-
1.) we can do linear search and for each element compare with its 4 neighbours
2.) Or, we can find max element in matrix
BETTER-
col ke index pe binary search lagado, aur mid jo aayega uske mein se max element uthalo.
-------------------------------------------------------------------------------------------------------------------------
5.)  (Row wise sorted) Matrix Median-
---------------------------------------------------------------------------------------------------------------





























Comments

Popular posts from this blog

18. greedy algorithm

19. strings (LB)