• 2 days ago
Transcript
00:00The next question is I want to find out the maximum number in my array list
00:04So the logic will remain the same
00:05The logic in the array which we find out the maximum number in the array
00:09We are applying the same logic here
00:11In fact most of the questions are
00:13Do you want to use the array or the array list?
00:17The logic is completely the same
00:18But what happens is that the operations change
00:20The methods which we call them change
00:23So the question here is I want to find out a maximum in my array list
00:27So what will I do in the beginning? I will define a max which will start from minus infinity
00:32What happens in java? Integer.minValue
00:39I call it minus infinity in java
00:41Minus infinity will be stored first
00:44So I will go into all indexes
00:47First I have come here
00:482 compared with minus infinity
00:50I have stored it in max
00:54Then I will compare with 5
00:55Then I will compare with max
00:56Which is bigger with 5
00:58So I have stored it in max
01:00Then I will add 9 or 5
01:039 bigger then I have stored it in 5
01:06Then I have stored it in 5
01:07Then it is bigger with 3 and 9
01:08That is bigger in max
01:11So I have no operation to perform
01:13and then I will come to 6 and 9
01:15so no change will not come and last
01:18I will print the maximum element
01:20that will be equal to 9
01:22so I am going to implement this same logic in my code
01:25I have already made a list, 1, 2, 3, 4, 5
01:28I will change the numbers
01:302, 5, 9, 6,
01:388
01:38now I know that the number in my list is the biggest number
01:42exists on index number 2 which is equal to 9
01:45let's remove this code
01:48and now I will print the maximum
01:51first I will make a max
01:54integer.min value
01:55I am going to initialize it
01:57after this I am going to add a loop
01:59for int i equals 0
02:01i less than list.size
02:06i++
02:07I will compare every time
02:09that if my max is small
02:12from my list.get i
02:15so in this case I will make a max
02:17list.get i equal
02:21and in the last
02:22I am going to print
02:25max element is equal to
02:29my max
02:30i will save and run
02:33I have a max element
02:36that is 9
02:36in fact
02:37this comparison
02:38in this place
02:39I can write a single line
02:40I can write
02:42max is equal to
02:43math.max
02:46mymax
02:47and mylist.get i
02:50i
02:50this line
02:51means that
02:52max
02:52will come into the value
02:53from math.max
02:55and the maximum
02:56so I have
02:57max and list.get i
02:59i
03:00or i
03:00index
03:00stored in the list
03:01and the value
03:02which is maximum
03:03will be stored in the max
03:05I will give the same output
03:08which will be
03:09maximum element is equal to 9
03:11and this time
03:13is also my
03:14linear time
03:15so i will also
03:16have linear time
03:16I will also have linear time
03:17in the ground