Lecture 23: Introduction to 2D Arrays in C++ || LeetCode Questions

Lecture 23: Introduction to 2D Arrays in C++ || LeetCode Questions


Lecture 23: Introduction to 2D Arrays in C++ || LeetCode Questions

In this Video, we are going to learn about 2D Arrays and solve the following questions:
- Row-wise Input/Column wise input
- Linear Search over 2D arrays
- Row-wise Sum/Column wise Sum
- Largest Row sum
- Wave Print
- Spiral Print
- Rotate a Matrix by 90 degrees
- Search in a 2D Matrix I LeetCode
- Search in a 2D Matrix II LeetCode

There is a lot to learn, Keep in mind “ Mnn boot karega k chor yrr apne se nahi yoga ya maza nahi para, Just ask 1 question “ Why I started ? “

Visit Relevel: https://relvl.co/prwb

Stack Overflow Link: https://stackoverflow.com/questions/1

Discord Server Link: https://discord.gg/feSQvVXMrd

Course Flow: https://whimsical.com/dsa-4-placement

Homework: timestamps added below

Notes Link: https://drive.google.com/file/d/1Vng3

Code Links: https://github.com/loveBabbar/CodeHel

Question Links:
- Wave Print: https://bit.ly/329Le3K
- Spiral Print: https://leetcode.com/problems/spiral-
- Rotate a Matrix by 90 degrees: https://leetcode.com/problems/rotate-
- Search in a 2D Matrix I LeetCode: https://leetcode.com/problems/search-
- Search in a 2D Matrix II LeetCode: https://leetcode.com/problems/search-


Do provide you feedback in the comments, we are going to make it best collectively.

Telegram Group Link: Love Babbar CODE HELP
https://telegram.me/lovebabbercodehelp

Connect with me here:

Instagram: https://www.instagram.com/lovebabbar1/
Twitter: https://twitter.com/lovebabbar3


My Editor: https://www.instagram.com/rishu.rsingh

Intro Sequence: We have bought all the required Licenses of the Audio, Video \u0026 Animation used.

Timestamps:

00:00 - Introduction
00:50 - Why we need 2-D Arrays ?
03:03 - How 2D stored in memory ?
04:22 - Promotion
05:26 - How 2D stored in memory ?
07:47 - Creating an 2D Array
09:51 - taking input in 2D Array
10:42 - Output in 2D Arrays
12:44 - Coding - Creating, Input \u0026 Output 2D Array
14:44 - Row-wise Input ion 2D Arrays
15:12 - Column-wise input in 2D Arrays
18:25 - Initialising 2D Arrays
20:45 - Linear Search in 2D Arrays
25:15 - Row-wise Sum Problem
32:22 - Largest Row Sum Problem
37:24 - Wave Print Problem
39:00 - Approach
41:00 - Code
45:22 - Time Complexity of Solution
46:20 - Spiral Print Problem
47:33 - Approach
50:45 - Code
59:17 - Time Complexity of Solution
01:00:35 - Homework
01:01:44 - Binary Search on 2D Arrays
01:02:44 - Search a 2D Matrix LeetCode Level1 Problem
01:03:55 - Approach
01:07:08 - Code
01:16:45 - Time Complexity of Solution
01:17:30 - Search a 2D Matrix LeetCode Level2 Problem
01:18:17 - Approach
01:23:29 - Code
01:28:10 - How to approach a problem ?


#DSABusted #LoveBabbar


Content

0 -> Hello ji. How are you all. This is love babbar and welcome to the channel CodeHelp
2.902 -> In this video we are going to talk about 2d arrays
5.751 -> About 2-Dimensional arrays
7.712 -> we have already studied arrays linear
10.513 -> We have studied character, strings
12.937 -> But still we have not discussed 2D array
16.212 -> we will discuss what, how is 2D array, and we will also study why do we need 2D array
24.001 -> what is the need of 2D array
27.408 -> we will see how it is stored in the memory
29.756 -> we will solve many questions
31.281 -> Binary search which we have seen on linear array, now we will see how it can be applied to 2D array
35.438 -> what is the scope, we will discuss it
37.505 -> so we will enjoy a lot, phase 1 will also get over
40.136 -> phase 1 will be completed after basic math, so be with us and we will start this video with 2D array
45.301 -> A very sweet example which you would have used while playing in childhood
53.244 -> we will play 0 and X
55.963 -> it used be something like this
57.639 -> I will put 0 over here then I will put X over here
60.711 -> I will put 0 over here, I will cut over here
63.968 -> I will put 0 over here, and did this like over here
66.919 -> I put 0 over here, now I will do like this
72.729 -> This way I will win
74.08 -> here there is nothing like winning or losing
75.53 -> we have to understand that we had 9 cells
79.775 -> Those how can not understand by 0 and X, it is also called tip-tap-toe
82.591 -> so we came to know that it was a very simple thing
86.15 -> and we understood that there was one table which was having 9 cells
93.448 -> In this way
98.037 -> is it fine? I had 9 cells
100.958 -> So if I tell you to implement this in code, then how will you do it?
105.234 -> So you will say it is very simple, I will make 3 array
107.901 -> Upto know what every you have understood, by that you will say I will make 3 arrays
111.616 -> And I will name them as arr1, arr2 and arr3
117.824 -> This way I have made 3 arrays, with cells each
121.234 -> and I will track them, and I will design a game
123.721 -> I said, yes fried you will be able to do it
127.954 -> but if I give a question in which I need 10 rows and 10 columns
137.156 -> meaning? over here you can see I need 3 rows and 3 columns
143.334 -> Now I want 10 rows and 10 columns
144.796 -> you will say no problem, I will create an array of 10 cell, arr1
148.97 -> In this way I will make 10 arrays
150.876 -> 1 to 10,I will say OK friend
153.887 -> now I need 1000 row and 1000 columns
158.952 -> now what you will do?
159.921 -> now will not make 1000 rows and 100 columns array, right?
163.781 -> Now you will think this a bit problematic, now what should I do?
168.532 -> There is one solution, suppose we have 1000 rows and 1000 columns
174.874 -> So I will create a linear array
177.542 -> I will make to small so that you can understand
179.551 -> suppose you have 10 rows, we will still make it small
183.454 -> suppose we have 3 rows and 3 columns
187.833 -> I will explain it to you with a small example so that you can understand it properly
189.365 -> So I know what I have to create at the end
193.232 -> at the end I have to create a table with 3 rows and 3 columns
198.86 -> This is what you want
201.416 -> now I will make a linear array
205.068 -> I will make a linear array with 9 cells
209.347 -> now I have 9 cells
219.81 -> with index from 0 to 8
224.977 -> now we have made 9 cells over here
228.005 -> now if want to put any value over here then I can put
232.602 -> If I want to put some value in 5th cell I will do it like this
238.644 -> now if you want to put a value in last cell you can put it like this
241.167 -> basically you have to visualize it in this way but but I will store like this in the memory
248.662 -> I will use some mathematical formula so that I can map this visualization with the execution
263.283 -> This video is sponsored by Relevel, if you want to enter into software domain
266.748 -> and get a high CTC job, this is the platform for you
271.021 -> over here many good companies appoint their employees like CRED,1mg, meesho, urban company, upGrad
276.293 -> there are many good examples
277.958 -> Relevel test are completely free, so there are many startup that are hiring
283.433 -> One new feature of sample question paper has been added, so that you can take the mock test
287.96 -> before appearing for the real test, so you will understand what all can be asked in real test, so that you can practice accordingly
292.076 -> up to now they have already provided 7 crore + jobs
295.933 -> This are some of the name on leader-board
299.154 -> and benefit of leader-board is that you get exciting prices
303.288 -> now you want one job simultaneously on leader-board with exciting prices
307.488 -> for that you have to click on the link in the description, and sign up
312.903 -> and register for test
314.332 -> now there are many categories in which you can register, so this all are the categories
319.67 -> a new category is added over here full stack developer
322.296 -> so register, take job, and give me a party, bye bye
326.622 -> what will be the formula?
329.018 -> You said it will be very simple
330.647 -> I will do column*i+j
333.94 -> I asked what is i and j? you said it is very simple i is for row and j is for column
341.608 -> I will explain how, this is 0,1,2 index and again 0,1,2
348.148 -> this is our 0th row, 1st row and 2nd row. This is our 0th column, 1st column and 2nd column
352.97 -> suppose, I am seeing this element
355.467 -> 1st row and 0th column
366.816 -> now if I add it to this formula, then what can I understand over here
371.056 -> total number of column, c means column and r means row
378.78 -> total number of column is 3, i was for row, it was 1
386.723 -> j=0
388.553 -> so 3*1+0=3
389.259 -> now I will go to index 3, so this element will be mapped with this
395.202 -> one time you can do this, 2nd row and 1st column
399.203 -> let write it down, 2nd row and first column, according to formula
407.124 -> c=3, i=2 and j=1
416.438 -> so it will be 3*2+1=7
418.145 -> so we will have to map this with 7th index
424.742 -> so you told to visualize it like this, but the storage and updating going on in the memory
431.239 -> you add something, you delete something, it will be done in this way
433.792 -> You told I know the formula so I will do it this way
437.826 -> If I tell you that it is good you know the formula , you know the internal working
441.657 -> but if I show you a way in which you will visualize it in the same way
447.691 -> but in memory the work is going on by this logic
451.369 -> so the logic which you told to implement 2D array in linear way, it is already implemented
457.73 -> I will show you the way, you will say this is a joke, now my work is so simple, I don't have to do mapping
463.004 -> So now we will talk about creation of 2D array
465.79 -> How to create it
472.034 -> up-to know we saw how that work is being done in memory
475.455 -> how 2D array is stored in memory
476.888 -> we saw if I write update this element in the memory then how it will be done
481.025 -> you are visualizing it in the form of 2D array but in the memory it is a linear array
486.328 -> so if you visualize a table, a matrix, but in memory it is a linear array
493.291 -> a mapping is there where you can say add element in 2nd rows 1st column
496.922 -> So it will search in the memory that in which index of linear it should be assigned
501.36 -> so it was fooling you
503.372 -> now how we will create it?
505.982 -> how we used to create simple array, int arr[10], this created a linear array
513.661 -> now I ask you to create 2D array, how will you do it?
521.547 -> int arr[3][3], 3 indicates row and 3 indicates column
529.658 -> so now let's see after writing this statement what will be created
538.193 -> when you write int arr[3][3],it will create this type of array
545.931 -> In which these are your rows 0th,1st and 2nd row
553.027 -> and these are your column 0,1,2
557.08 -> 0th column,1st column, and 2nd column
559.846 -> OK 1,2,3,4,5,6,7,8,9 I understood
564.498 -> when you use this statement int arr[3][3], or you use any number
569.98 -> you create a matrix, you can visualize this, but was is going on internal memory
574.266 -> same this way a linear array is created but we need not think about that, forget it
579.034 -> you have to assume that something like this is created
581.25 -> so this way you have created 2D array
584.484 -> now lets see how we take the input, it is very easy
588.248 -> input->?How did we take input in simple form?
596.42 -> it was very simple in 1D, cin>>arr[i];
614.422 -> this is way we have to take input in 2D array also
616.172 -> what will you write for 2D, cin>>arr[i][j];
629.929 -> this way we took the input
634.965 -> this was an experiment, now we will implement and see
638.591 -> now we are just trying to understand small things
641.373 -> now how will we get the output?
643.589 -> it is very simple, there is no rocket science, it is very easy
648.071 -> now we will do it and see
650.578 -> in 1D array we use to do cout>>arr[i]
660.449 -> now for 2D cout>>arr[i][j];
672.293 -> this way we learned to output 2D array also
674.792 -> now let's understand the concept i and j once again
679.753 -> suppose this your 2D array
688.675 -> this is 2D array, with index 0, no sorry
693.242 -> this 0,1,2, index, these are your rows
697.578 -> and these are your columns, index 0,1,2
701.189 -> if I say arr[2][1], then what does this indicate?
709.716 -> it indicates 2nd index of row and 1st index of column
714.906 -> 2nd index of row, OK
719.652 -> now go to 1st index of column, so we are representing this element
724.189 -> we represent this element through arr[2][1]
726.128 -> understood? now when you take arr[2,2]
730.685 -> sorry arr[2][2], what do you mean by this?
733.763 -> 2nd index of row and 2nd index of column
737.911 -> 2nd index of row is here, and 2nd index of column is here, so we are representing this element
743.028 -> understood?so now we understood how to represent any element in 2D array
750.789 -> now lets take input and try it to print, we will do simple things first
757.356 -> lets see, how this is executed
759.463 -> now lets start with intro
764.525 -> we created 2D array int arr[3][4];
772.855 -> now how to take input
777.177 -> I will execute 2 loops
780.184 -> for(int i=0;iarr[i][j];}}
803.023 -> this way we have taken input in a 2D array
809.835 -> we took a loop which will execute from 0th index to row-1
815.959 -> as we have seen here here number of rows were 0,1,2 so went up to
822.69 -> same thing we have done here also
825.24 -> i=0 to i
830.4 -> and take the input
831.923 -> fine, very good, lets do in once
835.18 -> now lets print and see it
837.982 -> for printing it is simple, we have to use same 2 loops
846.167 -> for(int i=0;i
860.802 -> now we understood, over here we have created, over here we have taken input, and here we have printed
867.328 -> now lets execute it
868.511 -> now it is asking for input, I have given input from 1 to 12< I have entered 12 elements 3*4=12
885.663 -> now I pressed the enter key, and see I got the output over here
889.021 -> how did it print 1 2 3 4, 5 6 7 8,9 10 11 12.
893.098 -> so it is taking row wise input
902.28 -> now suppose if I have not done it like this, i am adding first for loop in comment
906.179 -> suppose if I take it like this, I want column wise input
916.241 -> So what I will have to do is, i will execute first for loop for column, i=0;i
933.652 -> now, it will be cin>>arr[j][i];
941.604 -> now if I will execute this
942.96 -> I copied the same input, and printed it, now see carefully
948.118 -> it is 1 2 3, 4 5 6, 7 8 9, 10 11 12. Now our input is column wise
956.843 -> so when you take input in i and j, this is where the main change occurs
962.146 -> if you use it properly, first we executed 1st loop for rows we indicated it with i and 2nd loop for columns we indicated it with j
972.227 -> then we assigned i for rows and j for columns
979.485 -> so this was our row wise input
980.659 -> in second case we executed first loop for column which is indicated by i so we used it in second place arr[j][i]
987.672 -> we executed second loop up to 3 , i.e row
990.015 -> now number of row is 3 so we executed it up to
994.275 -> to make it more simple, I will replace all the i with row and all j with col
1030.446 -> now we will again execute it, we can see it is taking column wise input, how did you understand?
1036.784 -> first loop we have executed for columns, you can see
1039.348 -> where is column? In second place.
1041.752 -> second loop we executed for row, and we put it into first place
1045.274 -> now you have understood, how to take input row wise and column wise, how to print?
1051.496 -> I will again replace all i with row, and j with col, first loop is for row and second loop is for column, and we printed it
1069.693 -> we can see as per our visualization
1073.78 -> this is what we were trying to visualize
1077.553 -> I am talking about this
1080.264 -> so we printed the output and saw that, yes we are getting it in form of table
1084.374 -> now we say how you take 2D array input, how it is stored in memory, how you get the output, how you can get row wise input and column wise input
1094.192 -> what else can we learn
1098.837 -> now if wanted to hard code the input then?
1100.656 -> OK we will do it now
1102.602 -> I have added for loop for input in comment
1104.818 -> now it is very easy
1107.758 -> at the time of initialization only int arr[3][4]={1,2,3,4,5,6,7,8,9,10,14,16}
1118.589 -> now I have added 12 elements in this, why 12? 3*4=12
1125.337 -> i have executed this, I have got an error
1129.267 -> I have to add ;
1132.554 -> now again I will run this, now this has printed
1134.518 -> what we can see if I hard code the value then the values are assigned row wise
1141.501 -> 1 2 3 4 5 6 7 8 9 10 14 16, this is row wise initialization
1153.189 -> you only have to decide, which element you have to enter in which row
1155.849 -> what you will do? it is very easy
1157.367 -> I will comment the hard code initialization, now I will tell enter int arr[3][4]={{1,11,111,1111},{2,22,222,2222},{3,33,333,3333},{4,44,444,4444}};
1198.537 -> now we have completed our game
1201.725 -> but here we have 3 rows only so we cannot add{4,44,444,4444}, I will remove it
1206.188 -> now lets print it once
1213.47 -> we have got all the elements in each row as we wanted
1222.655 -> OK, so now you have understood the game
1224.639 -> you have understood, that if I want to initialize in starting it can be done in two ways,if I want I can also take the input row wise and column wise, and I can get it printed
1236.453 -> it was very easy game, we understood it
1238.436 -> now we will still improve it
1240.635 -> I will add this into comment
1245.425 -> now I will just create int arr[3][4]; and take row wise input
1250.811 -> and also get in printed
1254.82 -> and lets write a function for search
1258.212 -> we will write a function to search(), we will be given an element target
1262.244 -> cout
1272.444 -> int target; cin>> target;
1282.471 -> I said I will make a function isPresent()
1293.11 -> if( isPresent(arr,target, 3,4)){ cout
1323.173 -> now we will make the function
1326.916 -> bool isPresent(int arr[][],int target, int row, int col)
1351.021 -> but why is this giving an error?
1352.214 -> let's understand
1353.144 -> it is saying an array may not have element of this type
1356.742 -> what do you mean by this?
1358.153 -> if I specify the number of columns will it do?
1360.808 -> it's done!
1363.39 -> So that means in C++ if we do no specify number of columns, it will give an error
1371.442 -> now i will try to execute it, return 1;
1373.624 -> it again gave an error that no matching function of call isPresent()
1380.186 -> he is not able to understand what is going on
1382.447 -> if I specify the number of columns as 4 and then try to run this, this is working properly
1391.55 -> are you able to understand this?
1393.48 -> so if you are passing a function you will have to at least specify the number of column
1397.337 -> so now we will write our function, what we will do?
1401.608 -> we will compare each element with the target if it equal to the target or not
1405.041 -> as soon as we get something equal to target we will return true
1408.81 -> we just have to traverse the array, and check if(arr[row][col]==target){ return 1;}
1427.289 -> else {return 0;}
1433.541 -> our function is ready
1434.859 -> what did we do, we will compare each element with target if it is equal I will return 1 else I will return 0;
1443.32 -> now let's run and see
1445.066 -> now the input will be 1 to 12
1454.921 -> now the array is printed
1458.606 -> now which element do you want to search? I said 11
1460.364 -> now the output Element found, very good
1462.567 -> Let us try it once more, I will search 13 this time
1465.145 -> I searched for 13, not found, very good
1469.44 -> we have written the function to search
1470.942 -> this is bit similar to linear search
1476.001 -> we are checking each and every element id it is equal to target or not
1480.034 -> complexity is same row*column
1485.046 -> Big O of row*column
1487.545 -> if you are indicating row or column by n and m, then you can say row(m)*n, or if you are indicating both by n then row(n2)
1499.163 -> you might have understood this
1501.573 -> you have learned to create, to search, you know to print, you know how to take input
1505.687 -> and how is 2D array stored in memory that also you have understood
1509.219 -> now can we move a bit ahead?
1511.247 -> lets move on to next question
1513.665 -> OK, let's do next question
1516.016 -> I said next question is I want row wise sum
1523.001 -> I want you to calculate row wise sum
1525.63 -> what do you mean by this?
1527.279 -> means, suppose I have a matrix, there are some elements in that matrix
1534.754 -> 3 4 11, 2 12 1, 7 8 7
1542.29 -> now I want row wise sum, So what will be the sum of first row 3+4+11=18
1550.419 -> sum of 2nd row is 2+12+1=15
1553.264 -> sum of third row is 7+8+7=22, this way you have give me these three sum in the answer
1559.145 -> i hope I have done correct addition
1561.724 -> so, I want this is answer, row wise sum
1574.431 -> what will be the input? input will be your array
1579.691 -> now I will comment this, or let it be, because we are going to take the input
1587.314 -> I will write it properly cout
1602.401 -> cout
1616.784 -> now I have to make a function that will give me a row wise sum
1623.446 -> so I made a function void printSum(int arr[][4],int row, int col)
1637.029 -> now how will come to know if it is 4 or which number, I will not understand
1644.041 -> So I will say wait for 2 lectures then you will not need this
1646.801 -> I will teach you something that there will be no need to write this
1649.021 -> wait for 2 lectures, I am telling all the things where they are to be told,I am not skipping anything, that is my guarantee, you will taught everything,
1655.373 -> wait for 2 lectures, you will not need this
1657.769 -> but right now we will see to understand it
1661.176 -> printSum, we have to print sum row wise
1663.841 -> just for your understanding we have written to print row wise sum
1669.959 -> OK, we know the way to get row wise input
1674.724 -> we used to execute a loop, in the same way I can calculate row wise sum
1679.653 -> int sum=0; sum +=arr[row][col];
1690.069 -> cout < sum
1704.559 -> first loop is for rows, when you went inside the loop for one row, you calculated the sum, and then printed it
1713.995 -> done, we had to do this much only
1715.317 -> I will just add a statement before loop, cout
1733.035 -> this way we have printed the sum
1736.278 -> now lets run this
1737.593 -> I have given the input 3 4 11, 2 12 1, 7 8 7
1747.9 -> oh we have made array 3*3, so we will change the code according to 3*3
1756.234 -> we will change the code according 3*3, OK
1770.692 -> we have changed code according to 3*3
1774.139 -> now we will run
1775.577 -> entered the input, which element is to be searched I said 8, element found
1781.286 -> element is found is removed, why did it happen like this?
1787.159 -> because we have not called the function
1788.68 -> printSum(arr,3,3); we will clear the output screen and copy the array input
1804.191 -> we executed it, entered the elements, array is printed, which element is to searched, I entered 23, not found
1817.938 -> now the sum is printed, when it went to print the sum there was some mistake
1822.047 -> where is the mistake, lets check
1823.557 -> we changed it according 3*3 , and got the output 18,15,22
1841.825 -> So our sum is printed
1843.043 -> this is how we can write a function to print row wise sum in 2D array
1853.243 -> if I wanted column wise sum then what should I do?
1854.443 -> I will show you that also
1855.643 -> so it is very easy, you will go inside the loop for each column, so your first loop is for column for(col=0;col
1867.84 -> and second loop would be for row for(row=0;row
1874.636 -> now your work is done
1876.699 -> if I calculate your answer would be 12 24 19
1886.665 -> let's run and see
1888.557 -> I said run the code
1892.783 -> you some there is some mistake
1894.922 -> we have to change function name printColSum(); and also got it printed
1905.41 -> now we run the code
1906.395 -> search element 123, not found
1908.954 -> the output is 12 24 19, so our code is correct and executing properly
1914.188 -> we have also learned to calculate column wise sum
1917.591 -> how? we 12 24 19 as sum of columns
1925.42 -> it was very easy, first time loop was executed to calculate sum of first column and print it, that's it
1937.523 -> so we have learned to both calculated row wise sum, column wise sum
1941.44 -> now what is the next question?
1945.533 -> In next question we have to calculate which is the largest row
1949.453 -> which is the largest row sum?
1953.762 -> In this case it is 18 15 22, so my answer will be 22, i.e row 2nd
1969.922 -> 2nd is our index
1975.413 -> so I asked you to calculate the largest row sum
1978.893 -> you have to answer 2 questions, what is the largest row sum, and in which row it is
1986.099 -> ok, so lets write a function int largestRowSum(int arr[][3], int row, int col){}
2013.172 -> int maxi=INT_MIN; now just copy the code to calculate sum
2032.121 -> so in this you will calculate sum of each row, after the sum is calculated you have to compare
2042.384 -> if sum(>maxi){sum=maxi;}
2051.449 -> this way your maximum sum will be stored in this
2055.154 -> so we understood this, now we print it, cout
2068.556 -> but you have not added row number
2070.37 -> we will take int num=-1;
2076.528 -> now whenever my sum will get updated, i.e. I have got a new row with sum maximum, I will store it, i will rename num to rowIndex=-1;
2101.6 -> now in if condition rowIndex=row;
2113.442 -> now we will return row;
2117.793 -> now we will run this
2119.126 -> I will comment the code which is not required
2122.862 -> fine? we have to call largestRowSum()
2134.58 -> so, cout
2158.938 -> we executed and saw max row is at index, o when we have called the function, I will write it properly
2171.065 -> I will store the output in a variable int ansIndex=largestRowSum(arr,3,3);
2182.347 -> and I will run it once more
2184.656 -> I will copy it first
2187.24 -> I have copied,I cleared it, I executed it, I clicked enter, I got the answer maximum row sum is 22, it is on index 3, here there is some mistake
2196.735 -> lets understand why is it so
2197.713 -> come, see what is happening here, row
2220.114 -> we will run it, we entered the input, he said, maximum row sum index is 2, correct and maximum sum is 22
2230.047 -> this way we have written a function which can tell which is the largest row and at which index it is present and what is the largest row sum
2235.61 -> very good, one more code we have done
2237.252 -> now we can move to our next code where wee will pass test case
2242.662 -> and it is a very sweet question
2244.724 -> lets go
2247.556 -> lets read it first, you are given a 2D array, what is it size n*m
2257.692 -> you have to print in it wave order, what is wave order?
2259.797 -> we will print top to bottom of first column, next column we have to do bottom to top
2271.455 -> in this case, we have to print it like this
2278.88 -> answer will be 3 2 7, 8 12 4, 11 1 7
2289.701 -> this way.
2295.949 -> we understood that it very easy, I can do it
2304.075 -> I have one array of this type, now I told you wave print, you said it is very easy, I will do it this way
2322.516 -> yes correct, we have to do it that way only
2324.576 -> but how to do that?
2327.115 -> what is the way to solve this?
2328.564 -> if we talk about code then it is vector of vector
2341.199 -> how is 2D array? like a table, how is vector? like a linear array. how is vector of vector, it is same like 2D array
2360.576 -> so there is nothing to get scared of vector of vector
2362.841 -> so it is simple like 2D array
2368.082 -> so you have understood this
2370.268 -> now lets move ahead
2371.552 -> I will we solve this?
2374.256 -> we can see over here that we here to come down and then go up
2381.392 -> so what we can do?
2382.603 -> I can understand the index is from 0 to 4, I can understand that for every odd index I have to go upwards, correct, for index 1 and 3
2407.631 -> and wheneer the index is even or 0 you have to go top to bottom
2420.491 -> see in the case of 0,2 and 4 , this is what you have observed
2427.729 -> now I can solve this
2429.705 -> now I want to go from top to bottom how will I go? with the help of for loop
2433.885 -> how will I go from bottom to top? with the help of for loop
2435.891 -> is it easy?
2437.22 -> very easy
2438.42 -> lets do it
2439.411 -> if I say, that we have to code it, first of all we have to iterate all the columns, so we have to do some process for each column
2458.386 -> so first I will write for loop for column
2460.354 -> for(int col=0;col
2477.941 -> now we have to check if our index in odd or even
2481.045 -> if it is odd we have to bottom to top, if it is even we have to go top to bottom
2487.385 -> if( col&1){//odd index--> bottom to top } else{// even index--> top to bottom }
2520.59 -> if I want to go from bottom to top, we will use a for loop, from last index to 0th index
2525.905 -> for(int row=nRows-1; row>=0; row--){cout< arr[row][col]
2553.69 -> now in the same way, if I want to go from top to bottom
2559.127 -> for(int row=0; row
2588.093 -> now you will go inside for each column, if the column index is odd I printed from bottom to top, else I printed from top to bottom
2596.848 -> we will read it once again, this is fine, I will print it once and see what is the output
2608.786 -> So now what ever is the answer we have to store it into a vector and send
2619.084 -> so I created a vector, very simple
2620.898 -> vector ans;
2626.199 -> ans.push_back(arr[row][col]);
2648.693 -> now we have to return ans;
2654.812 -> lets run in
2659.537 -> I will execute it, there is some problem
2663.94 -> spelling of vector is wrong, now execute it
2666.845 -> I am getting wrong answer, why?
2673.928 -> Lets to one custom test
2675.914 -> I will copy paste it
2677.36 -> where is the mistake, look carefully
2689.612 -> we have printed this, I will remove it first
2691.67 -> now I will run it once again
2694.475 -> come on fast fast
2699.295 -> look carefully everything is working properly
2701.789 -> we will do it second time, we had only done cout, nothing else
2705.203 -> friend get executed, i will submit, and check it
2710.701 -> our answer, is correct
2720.398 -> our logic as correct and we have solved this, now lets see care fully what will be its complexity
2724.29 -> what are we doing, one time we are coming down then going up, then again coming down, i.e. we are traversing each element one time
2734.607 -> and total number of element is n*m so our complexity is O(n*m)
2741.298 -> understood? this is our time complexity, O(n*m), n is number of rows, m is number of columns
2747.913 -> this way we have done this code
2750.133 -> it was very easy
2751.723 -> OK, this wave print was very simple
2753.614 -> it is asked very frequently, sometime it is asked in online test
2759.411 -> now lets go to next question, spiral print
2761.711 -> which is asked a lot in amazon
2763.933 -> I have seen it been asked 3 times
2766.871 -> spiral printing
2768.805 -> this question is simple, but they are asked very frequently
2771.781 -> OK, sometime Pseudo code is asked in spiral print, write the approach
2776.832 -> write the code
2777.764 -> question is not difficult, it is easy we will do it
2779.328 -> lets see what will happen, come on!
2782.47 -> next question is spiral printing
2785.561 -> spiral print, if you look carefully in this example, which you can see on your screen
2797.284 -> in example we have 1 2 3, 4 5 6, 7 8 9, you might have seen spiral is like this
2808.375 -> we have to do this only, 1 2 3 6 9 8 7 4 5, this way
2816.448 -> so if i print it, it wil be printed like this, if the matrix is big then it will be printed like this
2827.595 -> so we have to print our elements in this way
2830.153 -> in our case 1 2 3 6 9 8 7 4 5, this way it will be printed
2834.911 -> this is called spiral print
2836.418 -> it is very simple, you might find it difficult, use your brain pause a bit and start it again
2843.915 -> I will start, I told you it is simple, what will be the approach, how will you solve it?
2852.174 -> approach, first I will print the first row, OK
2872.852 -> now print ending column, now if any element is printed twice then? we will take care of it
2880.158 -> but I said print ending column
2881.871 -> is it looking logical? yes
2882.942 -> ending column print it
2892.437 -> now, print ending row
2909.29 -> now print starting column
2914.121 -> starting row, ending column, ending row, starting column
2918.705 -> print starting column, I can see duplicate elements, we will handle them
2929.8 -> you understood this four things? starting row, ending column, ending row, starting column
2938.74 -> now, suppose if we have a big array
2943.363 -> like this, you will print starting row, ending column, ending row, starting column
2955.681 -> again starting row, ending column, ending row, starting column, this way you have to do it
2961.299 -> as you traverse through it, move it a step forward
2964.317 -> i.e. in first case this was your starting row, but as you have traversed now your starting row will be this
2969.098 -> how you print starting row, ending column, ending row, starting column
2974.068 -> now again you have to print starting row, but this row is already printed, so this is my starting row
2977.905 -> now I have to print ending column, but this one is already done, so I will print the next one
2982.464 -> in the same way for ending row and starting column
2985.868 -> now what ever you are printing, after that next row should be your starting row
2990.215 -> understood? again we will try to understand
2993 -> as soon as I had printed starting row, now starting row should be initialized to next row
3013.402 -> in the same way once when I have printed ending column now my ending column will also be assigned to next column
3023.197 -> Now if I have printed ending row, and again , I am asked which is the ending row then I will say next row is now my ending row
3032.125 -> In this way only we are going to do the question and it will be done very easily
3034.551 -> after writing the code you will realize that it was very easy
3038.054 -> ok so lets start, first I will take number of rows, int row=matrix.size();
3059.887 -> to calculate size of column, int col=matrix[0].size();
3077.136 -> we found number of row, number of column now lets find number of elements, int count=0; int total=row*col;
3095.046 -> now i added to a loop while( count < total), I will keep on adding the number of elements that I am printing, so that I will come to know when I have to exit from the loop
3113.682 -> so while(count
3119.938 -> now I have already told you some things, what were they
3123.961 -> //index initialization int startingRow=0; int startingCol=0; int endingRow=row-1; int endingCol=col-1;
3161.858 -> we have written these 4 things
3163.566 -> now our main game starts, now you will enjoy coding
3166.043 -> what did I say? I said first of all print them, //print starting row, how it will be done? lets understand
3175.195 -> we have 4 things, we will write them down, then it will be very easy for us
3178.665 -> we have startingRow, startingCol, endingRow, endingCol. now when I want to print startingRow
3190.694 -> we will go from startingCol to endingCol and print all the elements
3199.009 -> for(int index=startingCol; index
3219.89 -> now I have to return a vector, so vector ans; now in for loop{ ans.push_back(matrix[startingRow][index]);}
3241.382 -> now my starting row is printed
3244.408 -> so I can point startingRow to next row, startingRow++;
3253.031 -> in the same way we have to do 3 more things, lets see
3254.659 -> //print ending column
3257.916 -> how will you print ending column, simple go from starting row to ending row, and print it, very good
3265.543 -> we have to execute same type of loop
3267.987 -> for(int index=startingRow; index
3296.274 -> now this column is printed so now endingCol will point to next column from backwards
3302.319 -> so, endingCol--;
3309.68 -> now, I have to print endingRow, I have to go from endingCol to startingCol
3315.006 -> for(int index=endingCol; index>=startingCol; index--) { ans.push_back(matrix[endingRow][index]); } endingRow--;
3350.132 -> this much part is done
3352.15 -> I will write it down //printing ending row
3356.016 -> in the same way we will do the fourth part
3357.763 -> what is the 4th part? //print starting column
3364.011 -> how will you do that, I will explain it first
3372.055 -> you have to print starting column, so you have to go from endingRow to startingRow
3381.03 -> for(int index=endingRow; index>=startingRow; index--){ ans.push_back(matrix[index][startingCol]); } startingCol++;
3420.1 -> we have skipped one thing, when ever we were printing an element we had to increase the count++; in all for loops
3440.2 -> now we have to return ans;
3452.093 -> now lets run and see what happens, it is not able to understand what is total
3458.212 -> why is so, why it not able to understand?
3460.255 -> we have written spelling of total incorrect? now run
3463.625 -> it is saying startingCol, capitalize C in column, again run and check
3474.559 -> it is accepted, now execute all the test case one time
3479.554 -> run it one time
3481.748 -> it is saying wrong answer, HAW!
3485.449 -> why is it so, we will check it once
3488.981 -> 6 is coming 2 times, lets understand why this has happened
3491.919 -> OK, over here if we add a condition in all for loop count
3518.863 -> OK fine, it is executed correctly, so what was the problem, we were not checking our count and the loop was being executed
3527.173 -> we had missed the condition, when ever we are doing count++, we also have to check count should not be > total
3533.256 -> we have checked that, so it got accepted
3536.048 -> we will run it, and submit, lets see what will happen
3539.945 -> success!
3541.666 -> 100% faster, so you would have understood this logic and code
3545.329 -> we have written it in a very simple way
3547.339 -> with comments, you would have understood
3548.908 -> there is something in this code, we enjoyed
3553.117 -> you will not get simple code then this
3557.028 -> what is the complexity, look carefully, what we have done? we have traversed each element only once
3563.406 -> so if we have n rows and m columns, then O(n*m)
3569.976 -> you should understand the logic
3572.75 -> it was very simple, I asked you to print from startingCol to endingCol with the help of loop
3579.306 -> then print from startingRow to endingRow, with the help of for loop
3593.084 -> now print from endingCol to startingCol, with the help of for loop
3602.002 -> now print from endingRow to startingRow, with the help of for loop
3611.949 -> code was very nice
3614.841 -> you do not have to memorize it, simple you know what to print, take 4 loops and finish
3620.093 -> simple, O(n*m) it can not be done in a better way
3624.133 -> there is no meaning, no one can do it
3628.343 -> ok, so you can see our submission is also 100% faster, so we have written a very nice code
3633.522 -> we have also learned spiral print
3635.263 -> now we have one more question, i will explain it, but you take one question as homework
3639.338 -> spiral print is asked very often
3642.349 -> this question is also asked very frequently
3644.504 -> it is known as rotate your matrix by 90 degrees
3649.514 -> if you learn 90 degrees, remaining also you will do it by yourself
3652.041 -> OK, so you will be given one matrix, 1 2 3, 4 5 6, 7 8 9
3658.791 -> now you have to rotate it by 90 degrees
3662.661 -> so what will be the output, I will show you
3664.843 -> this is your matrix, you have to rotate it by 90 degree, and this will be the output
3670.251 -> 1 2 3 over here, 4 5 6 over here and 7 8 9 over here
3672.077 -> you had one matrix and you rotated it 90 degree
3675.848 -> OK, so this is your homework
3678.676 -> even you need 1.5 hours for this then also no problem, but use your brain, and solve this, if will solve this then you have understood 2D array. i,j, loop everything is clear to you
3692.615 -> use your brain take up to 1.5 hours no problem
3696.649 -> you will enjoy it
3698.927 -> that is my guarantee
3700.622 -> done? now we will solve such questions in which we will learn binary search, we have already learned it
3707.682 -> but we had done only in linear array, in 1D array
3715.184 -> now we will see it in 2D array
3718.056 -> so how are we going to use binary search in 2D array, we will see that
3726.729 -> I had told you earlier that we will not skip anything, but we will learn it at proper time
3730.046 -> we will learn everything when the right time will come, I know which thing is to be taught where, so you do not take tension
3737.814 -> nor will I skip anything, I will teach you properly, will make a flaming course, and this course will be discussed after 3 months,
3743.453 -> that there is free course on CodeHelp channel, love babbar has taught, study from that you will get placement
3749.498 -> understood this thing?
3751.557 -> now we will see binary search, how it done in 2D array
3756.551 -> our next question is search 2D matrix, we will understand how are we going to do this
3761.348 -> OK, so we have received a message of promotional mail
3769.091 -> according to the question, you are given one matrix
3775.421 -> you are given this type of matrix
3786.038 -> with elements 1 3 5 7, 10 11 16 20, 23 30 34 60
3799.725 -> now you have to write an efficient algorithm which will search in this m*n matrix
3806.113 -> what is the property of this matrix, that all the integer are sorted row wise,
3811.411 -> from left to right and 1st integer of every row is> then last integer every previous row
3816.236 -> so 1st integer of every row is> then last integer every previous row
3821 -> so if we see it carefully what do we understand?
3823.167 -> we understand that 1 3 5 7, 10 11 16 20, 23 30 34 60, it is a sorted array
3830.374 -> if I move further, I understand that this matrix is sorted row wise
3841.603 -> suppose we have to represent this matrix in linear form
3844.916 -> then this will be, 1,3,5,7,10,11,16,20,23,30,3,60, this is the linear form
3868.69 -> if i write down the index it will be from 0 to 11, so is this a sorted linear array? yes it is
3883.763 -> and if I want to search in sorted array, then can I use binary search? yes we can use
3886.918 -> then use it
3888.367 -> see it is simple, not very difficult
3890.07 -> if we had made it difficult then yo might not have understood it
3893.83 -> but we have understood that if i see this array in linear form the it is sorted array and we can use binary search
3897.95 -> how do we use binary search, this is start and this is end
3903.12 -> now start=0, and now understand carefully, this is your row m, and column is n, so end=(row*col)-1
3931.155 -> i.e. row=3, col=4, so end = (3*4)-1 = 12-1 = 11
3938.658 -> now end = (row*col)-1, this way we found start and end index
3949.977 -> now we will find mid, now there are 3 cases
3959.815 -> 1st case:- arr[mid]== target, then found, return it.
3975.555 -> 2nd case:- arr[mid]
3994.203 -> 3rd case:- arr[mid] >target, then target is on the left part, so end=mid-1
4008.585 -> now you code is over
4009.509 -> there is no rocket science in this
4012.755 -> so we are using the same things that we have already learned, we are not doing anything extra
4018.38 -> so from here your game is very easy, lets write down the code and see
4022.608 -> now a days we are discussing complexity also so you need not take tension, you might be understanding all the things
4028.756 -> OK, so it is simple, first I will find the row, int row=matrix.size(); int col=matrix[0].size();
4055.099 -> int start=0; int end=row*col-1; int mid=start +(end-start)/2;
4076.254 -> while(start
4094.963 -> now we are having a little problem, we have found the mid, but how we will fetch the element from mid?
4101.435 -> it is very simple, just wait for 1 second
4104.379 -> if I ask to fetch this element, this 16 element, then how would you do it?
4112.234 -> index of 16 is 6, OK, now I need to find in which row is this element present
4121.552 -> if I want to fetch any element in matrix , then I will have to specify row index and column index
4125.934 -> if I say this whole part specifies, the length of row
4135.053 -> i.e. number of columns
4140.978 -> so if I do 6/4=1, this indicates, that the element is on 1st index
4152.18 -> which is correct
4153.562 -> 16 is on first index, it is just by fluke
4156.982 -> lets get index of 60, index of 60 was 11
4162.7 -> 11/4=2
4170.109 -> so if I want a matrix over here, int element=matrix[mid/col][mid%col]; if(element ==target){ return 1;}
4207.226 -> lets see next 2 cases
4211.933 -> if(element < target) { start=mid+1;} else {end=mid-1;} mid=start+(end-start)/2;} return 0;
4247.487 -> now lets execute it, OK lets try
4252.13 -> we saw, it is accepted, we run all the test case, we executed it, and, execute it once again, this is also done
4266.425 -> now lets take risk and submit
4268.114 -> we submitted, our answer is accepted, with 74.63%
4276.434 -> what did we do, we learned to apply binary search on 2D array
4279.443 -> was this easy for you? yes it was very easy
4282.426 -> except line 15 whole code was easy for you? it is the same which we have already done
4288.202 -> it is exactly same
4291.501 -> now you have understood everything, except line 15.
4298.476 -> ok lets understand line 15, then you will clearly understand binary search in 2D array
4304.798 -> see if we want to access any element, I will have to give 2 things, row index, and column index
4318.748 -> what do have? we have mid
4322.306 -> we have calculated mid=start+end/2
4327.303 -> now understand it carefully, we have an element at this index
4330.44 -> we had learned about start and end, start was here and end was here, because it was linear array
4336.323 -> so the mid will,be over here
4338.57 -> so in this case when we are calculating mid, it will be over here, it is showing linear index
4345.555 -> I have to map this linear index in 2D array and find i and j
4350.386 -> understood? we found start and end because of this linear array
4354.605 -> so mid will also be in linear array
4358.965 -> suppose mid is 16, its index 6, so mid=6
4362.912 -> so now I have to search this 6 in this 2D array, the value 16 is matching to which i value and which j value
4373.014 -> if I will get that i and j, I will be able to fetch that element
4375.308 -> now lets understand, if I divide that index from this size, then can I get it?
4383.802 -> think it in the form of a table
4386.465 -> if I divide it by n, then I will get this in quotient
4392.064 -> are you able to understand?
4395.802 -> so it would be very easy for me
4397.773 -> see carefully, size of column is 4, so if you divide it by 4 you will get the row number
4406.611 -> and how much is it ahead in that row? it is simple MOD(%) it by 4
4410.845 -> what will be the index 0,1,2,3
4414.767 -> if I MOD(%) by 4 I will always get 0,1,2,3
4417.466 -> so we did MOD(%) by 4 or n
4421.708 -> if we had to find index of row, we divided it, and we will get quotient as answer
4426.637 -> answer will be quotient
4434.727 -> so we understood, if I will divide it then quotient will be my answer
4439.964 -> and if I want to find in which position does it exist, then I found it by MOD(%)
4445.045 -> why did I do MOD(%) with column, because we have column as 0,1,2,3,4
4450.906 -> understood friend?
4453.594 -> so this is the game, if you understood then very good, and if you did not understand then repeat the video
4458.243 -> I will explain it once again
4460.181 -> what we know is we should now row index and column index in matrix
4468.901 -> we had applied binary search on what basis?
4470.971 -> that it is a linear array, which is sorted, I assume this as start and this as end
4476.266 -> now mid is somewhere here
4478.271 -> if I say 6 is my mid, so what we want to do is, the value is there on index 6 i.e 16,
4484.372 -> what is the i and j index of 16, if I know that then I can find the value in the matrix
4491.741 -> is this correct? yes
4493.847 -> if you see carefully, this size i.e column, if I divide it with this size the index value, the I will be able to find where does it exist
4506.213 -> what is the size 4, index 0 to 11
4513.366 -> so when ever I will divide it, so my answer will be 0
4522.681 -> if I assume next 4, and divide it by 4, I will get 1
4527.821 -> if I assume last 4, and divide it by 4, I will get 2
4533.201 -> so if I will divide it by column I will get row index
4537.941 -> we have got the index of row
4539.624 -> now I have to find the column index for 16, how did we get this 2? it is very easy
4544.101 -> 0,1,2,3. if I MOD(%) any one of them what will be the answer? 0 to n-1
4549.927 -> so if do %4, what will be the answer 0 to 3, very good
4555.662 -> understood, so when I do MOD with 4, 6%4=2, I will get the index
4562.83 -> this way we had divided, and calculated MOD
4567.492 -> the whole game is of observation,understood?
4572.147 -> now binary search is easy?
4574.001 -> you have to solve this, these type of questions are asked very frequently
4577.599 -> you should know these type of questions
4579.531 -> ok, so now we will go to the next part, i.e. search a 2D matrix
4584.385 -> level 2
4587.375 -> what is this question?
4591.918 -> first we will see complexity of last one, see carefully what we have done
4596.111 -> we had gone over here, and lets understand it once
4604.35 -> if this my array, then what will I say O(log(n)), what is n? n is the size of array
4617.076 -> what is its size, row*col
4620.655 -> so when I say O(log(n)), n=row*column
4630.448 -> so complexity is, O(log(row*column)), finish
4635.573 -> if suppose this is m and n, O(log(m*n)), this is your time complexity
4641.328 -> understood? very good
4645.874 -> we have already learned time complexity of binary search, that is O(log(n))
4649.015 -> we will see the next question, we have got a matrix m*n, in which each integer is sorted row wise and column wise
4661.882 -> so it sorted row wise and column wise, like this
4665.06 -> now, May be I cannot arrange it in the form of linear array
4670.782 -> if I try to write it will be 1,4,7,11,15,2. it is unsorted
4675.541 -> we will have to use some other trick, that is why it is level 2
4678.938 -> so this question is very easy
4681.681 -> you can do this
4684.817 -> how? that we have to understand
4686.599 -> according to me this question should be first and that question should have been after this
4691.697 -> we will understand how we can do it
4694.389 -> we will try to find out the pattern
4698.123 -> suppose the target is 8, no lets assume my target is 10
4708.96 -> and I am at 9, if I am 9 and my target in 10, what does this indicate?
4714.206 -> can I discard anything?
4719.954 -> can I say that 9
4726.218 -> because we have many elements before 9
4728.523 -> so we will have to do something, we will have to find a pattern
4734.737 -> as soon as find the pattern the game is over
4737.287 -> lets start writing the condition
4740.143 -> case 1, I will write the question first, search 2D matrix-second
4753.603 -> now case 1:- if (arr[mid]== target) return it, your answer is done
4767.045 -> now, from where did mid come, we are going just randomly, we are trying to check, if I can reduce the search space
4779.301 -> if I can then I will be able to do it with binary search or else wil not able to do it
4782.817 -> I am just trying, that I can do it or not
4787.065 -> case 2:- arr[mid]
4823.824 -> it is guaranteed that it does not exist before 9, because it is sorted row wise
4827.539 -> so if arr[mid]
4842.054 -> row++;
4843.977 -> understood? row++, we do not have answer in this row
4849.73 -> case 3:- arr[mid]>target, then 17>10
4866.15 -> so can I can say that the answer is not on the right side, answer is somewhere behind only
4872.21 -> so col--;
4875.492 -> understood? what did we see case for 9, 9
4886.069 -> now for 17, 17>10, so answer is not after that so we have to search before 17, so col--
4893.184 -> so are we able to reduce the search space? yes
4901.96 -> so can we code? lets give it a try
4904.858 -> lets try
4908.254 -> from where will you start?
4909.446 -> this is a very correct question, because we have done the mid game so it will be difficult for us
4917.719 -> right? we have written mid but we do not know how we will play the game of mid
4921.952 -> when we tried to do it in start and end form, we had written it somewhere
4928.561 -> OK, so when we wrote this array in start and end form it was 1,2,4,11,15,2, this was not sorted, so we cannot use this
4942.256 -> so I will not be able to find the mid because I cannot find start and end because it is not sorted
4947.995 -> now what should I do?
4949.383 -> if I start with this element, 15, last element of first row
4957.418 -> so will we be able to solve? lets understand
4959.588 -> so if I say that if any element is smaller then 15, then it a guarantee that it will not be in this column
4966.555 -> understood? it will be before that only
4971.858 -> I can say it with guarantee
4974.348 -> if any element is smaller then 15, that means this condition, in this we had done column--
4984.925 -> correct, but if there is any element which is greater then 15, so I know that it will not be in this row
4993.265 -> because this row is over
4995.594 -> so I did row++
4998.087 -> so if I start my solution from here, where row is 0 and last column
5005.823 -> I already have the solution, understood?
5007.57 -> we tried the condition and came to know that if I will apply it on last element it should work
5012.643 -> lets try and see
5013.782 -> what did we say, int row=matrix.size(); int col=matrix[0].size(); int rowIndex=0; int colIndex=col-1;
5055.14 -> while(rowIndex=0)
5071.009 -> where did we start the row=0, we will go up to last row so we did rowIndex
5078.033 -> column is starting from here, so colIndex>=0
5085.77 -> what we have said in this case, is that if(matrix[rowIndex]), I will write it properly
5096.818 -> int element=matrix[rowIndex][colIndex];
5109.952 -> if(element==target){return 1;}
5121.217 -> if(element
5148 -> this way we have played out game
5149.671 -> if you did not get even that, then return false;
5154.671 -> now execute it and see, we executed it, and this is accepted
5160.938 -> now I will run all the test case, again we executed, and this is accepted, now I will submit it
5167.749 -> you submitted, and this is accepted 99.09%
5172.81 -> with accuracy
5175.996 -> we will say it as accuracy, no faster then
5180.497 -> so this we played a game, that we have understood, but we did not get the observation
5185.807 -> I said no problem, may be you not practiced enough, so you did not get it
5191.818 -> no problem, we sat to solve a question, could not do it, we saw a bit of hint
5195.726 -> we went to description, as saw that OK this is to be done
5202.645 -> OK, now which is the similar question? OK it is of this type
5207.037 -> now I will go to discussion form, I will see it from here, I will take a bit of idea how it is to be done
5213.203 -> can I do it with 2 pointer approach, OK now I used a bit of my brain to do it with 2 pointer
5217.299 -> so we can do it like this
5222.26 -> we did understand. what we did before that, we tried to sort it
5226.906 -> 1,4,7,11,15,2, if I write it in the form of linear array then this not sorted
5233.955 -> so it is difficult to apply binary search on this
5235.44 -> then we told that the way to apply binary is not only on sorted array
5239.743 -> there is one more way, to reduce search space
5241.346 -> as we done in Book Allocation problem
5246.653 -> so what we did, suppose 15 is > target, then we do not have to search anything in this column
5260.656 -> so we moved the column backwards, so now the search space is reduced
5263.747 -> if 15< target, that means we are not suppose to do anything in this row so move to next row
5269.329 -> understood both the condition? this is the way we reduced the search space
5273.846 -> while playing the game you prepared 3 condition
5279.385 -> the main catch was that from which element you have to start, if I will start from anywhere else I will not be able to apply the logic
5291.47 -> it was a trial
5292.522 -> dont be afraid, if you will be afraid then you will stay here only, and you will not be able to watch remaining 50-60 videos
5297.686 -> and you will not be able to complete DSA
5300.113 -> If you will move forward with courage, and understand the logic, suppose you are not able to understand it over here, you have to work hard
5307.267 -> go to discussion forum, there are solution given by people with explanation
5313.063 -> comment is written, it is very well written
5317.289 -> I went to solution, over here solution is written, and full intuition is written you have to read it
5329.203 -> you do not have to be dependent at all, that you will get spoon feeding and then only you will study otherwise I will not study by myself
5334.434 -> you have to see solution of others, you have to see the code
5336.877 -> no problem if there are no videos, you know the question, and that is important
5340.208 -> we will see someone else solution
5341.939 -> it is important to understand from anywhere
5345.557 -> so you have done this question also, so what all we did up to now?
5348.605 -> you understood what is 2D array
5350.291 -> you understood how to create it, initialize it, print it
5355.637 -> you calculated sum of largest row, you calculated sum of all the row
5358.462 -> you calculated sum of all the columns
5363.653 -> after that we learned, how can we get row wise input, column wise input
5373.81 -> we learned how o do wave print, spiral print, how to rotate 90 degree is your homework
5379.71 -> 2 questions of search in 2D matrix,which are premium questions
5383.43 -> we saw both the questions
5385.442 -> so from here on wards yo will be a bit clear in 2D array part
5388.313 -> now are you champion in 2D array? No
5390.89 -> you have to go on LeetCode, and solve at least 10 questions of 2D array
5396.924 -> first you have to do rotate 90 degree, no alternate for that, you have to do it
5403.018 -> second, you have to solve at lest 10 questions of 2D array
5404.77 -> if you cannot understand which questions to solve, no problem go on main channel, lovebabbar, and search lovebabbar 450 questions
5412.352 -> you will get one video, there will be one excel sheet, download that excel sheet
5416.569 -> in that there are many questions of 2D array, with link
5419.973 -> click it, solve it!
5421.457 -> you can do this much?
5423.689 -> do it, fine? see you in next video. bye bye
5427.157 -> what is the time I will show it to you once 5:35, I think I started the shoot at 3:47, OK bye bye

Source: https://www.youtube.com/watch?v=1CdolnvxLs0