1
00:00:00,560 --> 00:00:01,580
Welcome back.

2
00:00:01,580 --> 00:00:05,720
Let's now code the brute force solution which we discussed in the previous video.

3
00:00:05,720 --> 00:00:08,000
Now for this we are going to write a function.

4
00:00:08,000 --> 00:00:10,850
Let's call it max area brute force.

5
00:00:12,990 --> 00:00:16,110
And this function is going to take in the array.

6
00:00:16,260 --> 00:00:16,560
All right.

7
00:00:16,560 --> 00:00:17,820
So we have array over here.

8
00:00:17,850 --> 00:00:22,920
Now once inside this function we will initialize the area to be equal to zero.

9
00:00:22,920 --> 00:00:23,340
All right.

10
00:00:23,340 --> 00:00:25,770
And then finally we will just return this area.

11
00:00:25,770 --> 00:00:30,180
We will see whether we are getting an area which is greater than what we have over here.

12
00:00:30,180 --> 00:00:33,750
And accordingly we will replace the area with the greater value.

13
00:00:33,750 --> 00:00:34,290
All right.

14
00:00:34,320 --> 00:00:43,020
Now let's say that the area, the array which is given to us is let's say three, seven, five, six,

15
00:00:43,020 --> 00:00:43,650
eight.

16
00:00:43,650 --> 00:00:47,520
And for the same one which we discussed in the video previously.

17
00:00:47,640 --> 00:00:48,060
All right.

18
00:00:48,090 --> 00:00:49,530
Now what are we going to do.

19
00:00:49,530 --> 00:00:55,560
As we discussed we are going to have a variable I and it will go from zero till this eight over here.

20
00:00:55,560 --> 00:00:55,920
Right.

21
00:00:55,920 --> 00:00:57,180
So let's write that over here.

22
00:00:57,180 --> 00:00:57,990
So for.

23
00:00:59,050 --> 00:01:02,650
Let I is equal to zero I less than array.

24
00:01:05,110 --> 00:01:06,370
Dot length.

25
00:01:07,770 --> 00:01:10,080
Minus one I plus plus.

26
00:01:10,770 --> 00:01:16,140
So we have arrived at length minus one because we just want to go up to this element over here.

27
00:01:16,140 --> 00:01:16,530
Right.

28
00:01:16,560 --> 00:01:20,550
Now once we are inside this for loop we are going to have another for loop.

29
00:01:20,550 --> 00:01:25,020
So let's say for let j is equal to I plus one right.

30
00:01:25,020 --> 00:01:28,290
So we're going to start from the next element after I.

31
00:01:28,320 --> 00:01:28,740
Right.

32
00:01:28,740 --> 00:01:32,730
And then we are going to check up to j less than array dot length.

33
00:01:34,880 --> 00:01:36,890
And then we are going to say J plus, plus.

34
00:01:38,900 --> 00:01:39,380
All right.

35
00:01:39,380 --> 00:01:40,760
Now, what are we going to do?

36
00:01:40,790 --> 00:01:44,420
We're going to find the minimum height at these two indices.

37
00:01:44,420 --> 00:01:44,780
Right.

38
00:01:44,780 --> 00:01:46,940
So let's say the height.

39
00:01:46,940 --> 00:01:48,980
Let's say let height.

40
00:01:49,930 --> 00:01:51,130
Or we can say const height.

41
00:01:51,130 --> 00:01:56,230
At this point, const height is equal to math dot min.

42
00:01:56,350 --> 00:02:01,930
And then we're going to find the minimum right height at these two indices at I and j.

43
00:02:01,930 --> 00:02:04,300
So let's say min array at I.

44
00:02:06,510 --> 00:02:07,470
And RJ.

45
00:02:09,400 --> 00:02:10,690
So that will give us the height.

46
00:02:10,690 --> 00:02:11,140
Right.

47
00:02:11,140 --> 00:02:12,520
And what about the width.

48
00:02:12,520 --> 00:02:13,930
So const width.

49
00:02:14,950 --> 00:02:16,480
Will be equal to.

50
00:02:17,280 --> 00:02:18,720
J minus I.

51
00:02:18,750 --> 00:02:19,020
Right.

52
00:02:19,020 --> 00:02:26,640
So if j is equal to one and I is equal to zero, then we know that the width is zero one -0 which is

53
00:02:26,640 --> 00:02:27,180
equal to one.

54
00:02:27,180 --> 00:02:29,670
So that's j minus I over here.

55
00:02:30,150 --> 00:02:32,430
So we have got the height and width right.

56
00:02:32,460 --> 00:02:36,180
Now we can just find the area by multiplying the height and width.

57
00:02:36,180 --> 00:02:42,870
And then we are going to say area is equal to math dot max because we want the maximum area right among

58
00:02:42,870 --> 00:02:46,590
the current area and height dot height into width.

59
00:02:49,350 --> 00:02:49,770
All right.

60
00:02:49,770 --> 00:02:53,250
So this will give us the maximum area.

61
00:02:53,250 --> 00:02:53,400
Right.

62
00:02:53,400 --> 00:02:56,940
So the maximum area will always be stored into area.

63
00:02:56,940 --> 00:02:58,560
And then we can just return area.

64
00:02:58,560 --> 00:02:59,310
So that's it right.

65
00:02:59,310 --> 00:03:00,600
So we're done with our function.

66
00:03:00,600 --> 00:03:02,580
Now let's go ahead and test this.

67
00:03:02,580 --> 00:03:04,500
Now to test it's easy.

68
00:03:04,500 --> 00:03:09,180
Let's just pass this array over here because we have already discussed this in the previous video.

69
00:03:09,180 --> 00:03:13,230
So the expected maximum area is 21 right.

70
00:03:13,230 --> 00:03:16,650
So when we take seven and eight so that's seven into three.

71
00:03:16,650 --> 00:03:20,790
So let's just call the function so max area brute force.

72
00:03:20,790 --> 00:03:23,820
And we're going to pass in this array over here.

73
00:03:24,760 --> 00:03:26,860
Now let's see what we get as the output.

74
00:03:26,860 --> 00:03:28,420
So let's just run this.

75
00:03:28,420 --> 00:03:31,690
And the output that we're getting is 21 which is correct.

76
00:03:31,690 --> 00:03:32,020
Right.

77
00:03:32,020 --> 00:03:33,910
So yes our function is working.

78
00:03:33,910 --> 00:03:35,770
Now let's take another example.

79
00:03:35,770 --> 00:03:38,740
Also let's say we are passing an array where it's obvious.

80
00:03:38,740 --> 00:03:41,890
Let's say we have the largest values at the two ends.

81
00:03:41,890 --> 00:03:43,930
So 9123.

82
00:03:43,930 --> 00:03:45,610
And then let's say we have ten right.

83
00:03:45,610 --> 00:03:48,370
So what is the expected area.

84
00:03:48,370 --> 00:03:51,310
In this case it should be the height is nine right.

85
00:03:51,310 --> 00:03:53,080
It should be the minimum of nine and ten.

86
00:03:53,080 --> 00:03:55,840
And the width will be 123 and four.

87
00:03:55,840 --> 00:03:57,400
So we should get 36 right.

88
00:03:57,400 --> 00:03:59,920
So yes we are getting 36 as the output over here.

89
00:03:59,920 --> 00:04:02,020
So yes this function is working.

90
00:04:02,020 --> 00:04:08,470
Now let's take a sample input and walk through the code and relook at the time and space complexity.

91
00:04:08,470 --> 00:04:10,690
Remember in the interview this is an important step.

92
00:04:10,690 --> 00:04:15,040
Once you have written the code, you always have to take some sample input and walk through the code.

93
00:04:15,040 --> 00:04:16,870
So let's go ahead and look at the code.

94
00:04:16,870 --> 00:04:17,680
Walk through now.
