1
00:00:00,770 --> 00:00:02,390
Let's do this question.

2
00:00:02,390 --> 00:00:09,020
An array is monotonic if it is either monotone increasing or monotone decreasing.

3
00:00:09,140 --> 00:00:16,130
An array is monotone increasing if all its elements from left to right are non-decreasing, an array

4
00:00:16,130 --> 00:00:20,840
is monotone decreasing if all its elements from left to right are non-increasing.

5
00:00:20,840 --> 00:00:26,870
Given an integer array, return true if the given array is monotonic or false otherwise.

6
00:00:26,900 --> 00:00:28,760
All right, so this is the question at hand.

7
00:00:28,760 --> 00:00:30,920
Let's try to understand this over here.

8
00:00:30,920 --> 00:00:32,330
This is a mathematical concept.

9
00:00:32,330 --> 00:00:34,340
When we say that an array is monotonic.

10
00:00:34,340 --> 00:00:39,350
Now an array is monotonic if it's either monotone increasing or monotone decreasing.

11
00:00:39,350 --> 00:00:40,670
So let's understand these two.

12
00:00:40,670 --> 00:00:44,780
So this is monotone increasing and this is monotone decreasing.

13
00:00:44,780 --> 00:00:51,110
Now if an array is monotone increasing then it means that when we go from left to right the elements

14
00:00:51,110 --> 00:00:52,370
are non decreasing.

15
00:00:52,370 --> 00:00:55,460
For example let's take an example like one.

16
00:00:55,460 --> 00:00:57,500
And then you have um two.

17
00:00:57,500 --> 00:01:00,050
And then you have three right over here.

18
00:01:00,050 --> 00:01:02,720
When you go from 1 to 2 that is you're going from left to right.

19
00:01:02,720 --> 00:01:03,050
Right.

20
00:01:03,050 --> 00:01:05,240
So the elements are non decreasing.

21
00:01:05,240 --> 00:01:08,270
So we can say that this is monotone increasing.

22
00:01:08,270 --> 00:01:11,270
Now why is it mentioned over here as non decreasing.

23
00:01:11,270 --> 00:01:13,520
Now let's say we add an element three over here.

24
00:01:13,520 --> 00:01:16,460
Now over here we can see that these two elements are equal right.

25
00:01:16,460 --> 00:01:17,720
So it's not increasing.

26
00:01:17,720 --> 00:01:20,840
But still because it is non-decreasing right.

27
00:01:20,840 --> 00:01:23,120
From 3 to 3 it's not decreasing.

28
00:01:23,120 --> 00:01:23,300
Right.

29
00:01:23,300 --> 00:01:24,710
So it's non decreasing.

30
00:01:24,710 --> 00:01:28,940
Therefore we can say that this array over here is still monotone increasing.

31
00:01:28,940 --> 00:01:31,040
Now what about monotone decreasing.

32
00:01:31,040 --> 00:01:32,150
Let's take an example.

33
00:01:32,150 --> 00:01:33,770
Again we take a simple example.

34
00:01:33,770 --> 00:01:35,300
Let's say we have 321.

35
00:01:35,330 --> 00:01:38,300
Now in this case when we go from left to right right.

36
00:01:38,300 --> 00:01:40,460
So the elements are non increasing.

37
00:01:40,460 --> 00:01:43,910
So from 3 to 2 the the value is not increasing right.

38
00:01:43,910 --> 00:01:46,910
And similarly from 2 to 1 the value is not increasing.

39
00:01:46,910 --> 00:01:49,220
So the elements are non increasing.

40
00:01:49,220 --> 00:01:51,500
Again we add an element over here.

41
00:01:51,500 --> 00:01:52,910
Let's say we have one over here.

42
00:01:52,910 --> 00:01:57,020
In this case when we go from 1 to 1 the value is not decreasing.

43
00:01:57,020 --> 00:01:57,890
It's equal right.

44
00:01:57,890 --> 00:01:59,900
But then it is not increasing.

45
00:01:59,900 --> 00:02:04,610
So that's why we can say that this array over here is also monotone decreasing.

46
00:02:04,610 --> 00:02:05,060
All right.

47
00:02:05,060 --> 00:02:06,320
So we have understood the question.

48
00:02:06,320 --> 00:02:10,670
We have understood what we mean with monotone increasing and monotone decreasing.

49
00:02:10,670 --> 00:02:13,040
Now the question says that we are given an array.

50
00:02:13,520 --> 00:02:15,560
And then we have to do some operations.

51
00:02:15,560 --> 00:02:19,700
And we have to identify whether the given array is monotonic.

52
00:02:19,700 --> 00:02:23,570
That means whether it is monotone increasing or monotone decreasing.

53
00:02:23,570 --> 00:02:26,600
And if it is, we have to return true, else we have to return false.

54
00:02:26,600 --> 00:02:32,360
So that's the question now in the interview when you get this question, always feel free to ask clarifying

55
00:02:32,360 --> 00:02:37,340
questions so that you thoroughly understand the question and you and the interviewer are on the same

56
00:02:37,340 --> 00:02:37,730
page.

57
00:02:37,730 --> 00:02:41,240
Now, what are some possible questions possibly you could ask?

58
00:02:41,240 --> 00:02:43,070
Is an empty array monotonic?

59
00:02:43,070 --> 00:02:47,390
And let's say that the interviewer says yes, an empty array is monotonic.

60
00:02:47,390 --> 00:02:47,990
All right.

61
00:02:47,990 --> 00:02:52,790
Now another question could be is an array with only one integer monotonic.

62
00:02:52,790 --> 00:02:57,110
And again let's say the interviewer replies yes, this array is monotonic.

63
00:02:57,110 --> 00:02:57,680
All right.

64
00:02:57,680 --> 00:03:00,500
So again, as of now we have understood the question.

65
00:03:00,500 --> 00:03:04,310
We have understood what is monotonic increasing and monotone decreasing.

66
00:03:04,310 --> 00:03:10,790
And we have done clarifying any kind of question or doubt you have all right now at the At.

67
00:03:10,790 --> 00:03:13,940
Then as a next step, let's go ahead and write test cases.

68
00:03:13,940 --> 00:03:18,110
And remember, you can always take the help of the interviewer whenever you write test cases.

69
00:03:18,110 --> 00:03:19,730
But over here it's pretty straightforward.

70
00:03:19,730 --> 00:03:21,980
So let's go ahead and write a few test cases.

71
00:03:22,010 --> 00:03:29,060
Now if the array which is given to us is one, two, three, then over here we can see that the elements

72
00:03:29,060 --> 00:03:30,320
are non-decreasing.

73
00:03:30,320 --> 00:03:30,620
Right.

74
00:03:30,620 --> 00:03:32,360
So I'm just writing end over here.

75
00:03:32,360 --> 00:03:33,800
It's non-decreasing.

76
00:03:33,800 --> 00:03:39,410
So yes we can say that this array over here is monotonic or it is monotonic increasing.

77
00:03:39,410 --> 00:03:39,740
Right.

78
00:03:39,740 --> 00:03:43,550
So if the value is monotonic increasing it should be just non-decreasing.

79
00:03:43,550 --> 00:03:46,040
Now what if we are given the array 321.

80
00:03:46,040 --> 00:03:52,370
In this case we can see that the array over here is non-increasing right as we go from left to right

81
00:03:52,370 --> 00:03:53,630
it's not increasing.

82
00:03:53,630 --> 00:03:56,240
So therefore it is monotonic decreasing.

83
00:03:56,240 --> 00:03:56,570
Right.

84
00:03:56,570 --> 00:03:59,810
So yes over here also we would be returning true.

85
00:03:59,810 --> 00:04:02,960
Now if the array which is given to us is one two, two.

86
00:04:02,990 --> 00:04:04,670
In this case also it will be true.

87
00:04:04,670 --> 00:04:04,820
Right?

88
00:04:04,820 --> 00:04:09,140
It will be monotonic because in this case it's monotonic increasing right.

89
00:04:09,140 --> 00:04:12,890
Again it's monotonic increasing because it is non-decreasing.

90
00:04:13,130 --> 00:04:15,260
When you go from 2 to 2 again it's not decreasing.

91
00:04:15,260 --> 00:04:16,490
So it's non-decreasing.

92
00:04:16,490 --> 00:04:18,920
That's why it's monotonic increasing.

93
00:04:18,920 --> 00:04:20,210
And we return true.

94
00:04:20,210 --> 00:04:21,830
Now let's take one more example.

95
00:04:21,830 --> 00:04:23,450
What if we have 333.

96
00:04:23,450 --> 00:04:25,070
In this case also it will be true.

97
00:04:25,070 --> 00:04:25,460
Right.

98
00:04:25,460 --> 00:04:29,120
So we have got a fair understanding and about the edge cases.

99
00:04:29,120 --> 00:04:30,830
We had asked clarifying questions.

100
00:04:30,830 --> 00:04:31,190
Right.

101
00:04:31,190 --> 00:04:36,680
So if you are given let's say an array with only one element, then we will return true.

102
00:04:36,680 --> 00:04:40,550
And if you are given an empty array in that case also we will return true.

103
00:04:40,550 --> 00:04:42,410
Now all of these cases were true.

104
00:04:42,410 --> 00:04:45,050
What would an example be of a false case?

105
00:04:45,050 --> 00:04:50,960
For example, if you have two, two, three and one, you can see as we go from left to right till here

106
00:04:50,960 --> 00:04:53,180
it was monotonic right.

107
00:04:53,180 --> 00:04:56,120
It was monotonic because it was non decreasing.

108
00:04:56,120 --> 00:04:59,030
But then when we came from 3 to 1 it decreased right.

109
00:04:59,030 --> 00:05:00,350
So when we take this array.

110
00:05:00,580 --> 00:05:00,940
The.

111
00:05:00,970 --> 00:05:02,200
It is not monotonic, right?

112
00:05:02,200 --> 00:05:05,350
It's neither non-increasing nor non-decreasing.

113
00:05:05,350 --> 00:05:06,460
So we return false.

114
00:05:06,460 --> 00:05:08,590
So we have got a good understanding of the question.

115
00:05:08,590 --> 00:05:13,900
Now in the next video, let's go ahead and think through how we are going to solve this.
