1
00:00:00,140 --> 00:00:04,580
Let's now get into the code for solving the monotonic array question.

2
00:00:04,760 --> 00:00:05,210
Okay.

3
00:00:05,210 --> 00:00:11,630
So first over here we're going to check whether the input array which is given to us does not have any

4
00:00:11,630 --> 00:00:12,260
elements.

5
00:00:12,260 --> 00:00:19,550
And remember in the previous video we had asked the interviewer that whether we should consider an empty

6
00:00:19,550 --> 00:00:21,140
array to be monotonic.

7
00:00:21,140 --> 00:00:25,910
And the reply that we got was that yes, an empty array should be considered monotonic.

8
00:00:25,940 --> 00:00:27,800
So let's have a check for that over here.

9
00:00:27,800 --> 00:00:34,610
So I will say if array dot length is equal to zero.

10
00:00:34,610 --> 00:00:38,060
So if this is the case then we will just return true.

11
00:00:39,860 --> 00:00:44,630
Now if this is not the case then we proceed with the remaining part of the solution.

12
00:00:44,630 --> 00:00:50,930
So as we have discussed in the previous video, we are going to first compare the first element and

13
00:00:50,930 --> 00:00:54,230
the last element in the array which is given to us.

14
00:00:54,230 --> 00:01:00,170
So I will say const first is equal to array at index zero.

15
00:01:01,070 --> 00:01:09,920
And similarly I will say const last is equal to array at index array dot length.

16
00:01:12,200 --> 00:01:13,340
Minus one.

17
00:01:13,370 --> 00:01:19,580
Okay, so we have got the first and last element in the array which is given to us.

18
00:01:19,610 --> 00:01:23,720
Now what are the three possibilities that we have discussed in the previous video.

19
00:01:23,750 --> 00:01:26,660
The first possibility is the first element.

20
00:01:26,660 --> 00:01:30,230
And the last element may be equal.

21
00:01:30,260 --> 00:01:30,890
Okay.

22
00:01:30,890 --> 00:01:36,800
Now before we go ahead and fill the details for this, let me just over here construct the skeleton

23
00:01:36,800 --> 00:01:37,130
okay.

24
00:01:37,130 --> 00:01:42,800
So the first possibility is that the first element and the last element could be equal.

25
00:01:42,800 --> 00:01:50,270
Now if this is not the case, the other possibility is that the first element could be less than the

26
00:01:50,270 --> 00:01:51,320
last element.

27
00:01:51,320 --> 00:01:51,830
Okay.

28
00:01:51,830 --> 00:01:53,810
So that's again one more possibility.

29
00:01:53,810 --> 00:02:02,210
And if these two are not true then the remaining possibility is that the first element is larger than

30
00:02:02,210 --> 00:02:03,650
the last element okay.

31
00:02:03,650 --> 00:02:08,390
So again we don't need to write that specifically because that's the only remaining case.

32
00:02:08,390 --> 00:02:08,780
Right.

33
00:02:08,780 --> 00:02:12,440
So this means that first is larger than last.

34
00:02:12,440 --> 00:02:18,920
Now the way that we are going to approach this is that we are going to check whether the criteria needed

35
00:02:18,920 --> 00:02:25,100
for each of these cases for us to conclude that the array which is given to us is monotonic.

36
00:02:25,100 --> 00:02:30,230
So we are going to check whether that particular condition, which is the needed condition, has been

37
00:02:30,230 --> 00:02:30,980
broken.

38
00:02:30,980 --> 00:02:38,330
And if we can find that it has been broken or it has been violated, then we will just return false.

39
00:02:38,330 --> 00:02:45,380
But even after going through whichever the case is, if you are not able to return false, then we would

40
00:02:45,380 --> 00:02:48,440
come over here and we will just return true.

41
00:02:48,560 --> 00:02:50,780
Okay, so this is the skeleton of the approach.

42
00:02:50,780 --> 00:02:53,570
Now let's go ahead and fill in the details.

43
00:02:53,570 --> 00:03:01,100
If the first element and the last element are equal, we would need every element in the array to be

44
00:03:01,100 --> 00:03:03,560
equal for it to be monotonic.

45
00:03:03,560 --> 00:03:10,430
So what we are going to do is we are going to iterate from index zero till the last index of the given

46
00:03:10,430 --> 00:03:17,960
array, and we are going to check whether array at index I plus one is not equal to array at index I,

47
00:03:17,990 --> 00:03:24,410
because if that is the case, then the required condition has been violated and we will just return

48
00:03:24,410 --> 00:03:24,950
false.

49
00:03:24,950 --> 00:03:34,640
So over here I will say for let I is equal to zero, I less than array dot length minus one.

50
00:03:34,640 --> 00:03:36,410
And we have I plus plus.

51
00:03:36,410 --> 00:03:47,450
And over here I'm going to say if array at index I plus one is not equal to array at index I then we

52
00:03:47,450 --> 00:03:49,370
will just return false.

53
00:03:51,770 --> 00:03:52,370
Okay.

54
00:03:52,370 --> 00:03:56,660
So again we are checking whether the required criteria has been violated.

55
00:03:56,660 --> 00:03:59,840
And if we find that this is true we will return false.

56
00:03:59,930 --> 00:04:00,440
Okay.

57
00:04:00,440 --> 00:04:01,640
Now let's proceed.

58
00:04:01,670 --> 00:04:09,260
Now if the case is that the first element is less than the last element, then again we will have the

59
00:04:09,260 --> 00:04:10,100
same for loop.

60
00:04:10,100 --> 00:04:20,150
So we will have for let I is equal to zero I less than array dot length minus one I plus plus okay.

61
00:04:20,150 --> 00:04:28,160
And over here the needed criteria is that array at I plus one has to be greater than array at I okay.

62
00:04:28,160 --> 00:04:33,830
Because the first element is less than the last element, or the last element is greater than the first

63
00:04:33,830 --> 00:04:34,250
element.

64
00:04:34,250 --> 00:04:37,400
So we are going to check whether that criteria has been broken.

65
00:04:37,400 --> 00:04:42,500
So over here I will say if array at index I plus one.

66
00:04:43,360 --> 00:04:46,390
Is less than array at index I.

67
00:04:46,420 --> 00:04:51,310
So if this is true at any instance then we will just return false.

68
00:04:53,140 --> 00:04:53,860
Okay.

69
00:04:53,860 --> 00:04:57,340
And finally, we'll have to do something similar over here as well.

70
00:04:57,370 --> 00:05:01,480
So over here the first element is greater than the last element.

71
00:05:01,480 --> 00:05:06,460
So we would need an array at I plus one to be less than array at I.

72
00:05:06,490 --> 00:05:11,050
So we will see or we will try to check whether that criteria has been broken.

73
00:05:11,050 --> 00:05:14,740
And if we find that it's broken we will just return false.

74
00:05:14,740 --> 00:05:16,540
So again I'll say for.

75
00:05:17,260 --> 00:05:22,360
Let I is equal to zero I less than array dot length minus one.

76
00:05:22,570 --> 00:05:23,680
I plus plus.

77
00:05:24,490 --> 00:05:33,490
And over here I will say if array at index I plus one greater than array at index I.

78
00:05:33,670 --> 00:05:36,160
Then we will just return false.

79
00:05:38,400 --> 00:05:39,870
Okay, so that's it.

80
00:05:39,870 --> 00:05:45,600
Now, if we were not able to return false, then over here we'll go ahead and return true.

81
00:05:45,600 --> 00:05:49,710
Because the array which has been given to us is in fact monotonic.

82
00:05:49,710 --> 00:05:52,740
So this is the solution to the monotonic array question.

83
00:05:52,740 --> 00:05:56,880
Now let's go ahead and run this code and see if it's passing all the test cases.

84
00:06:03,170 --> 00:06:06,350
And you can see that it's passing all the test cases.

85
00:06:06,350 --> 00:06:06,740
Okay.

86
00:06:06,740 --> 00:06:08,990
And again, do make use of the user logs.

87
00:06:08,990 --> 00:06:12,950
It will help you debug your code in case you're stuck at some point.
