Let's do this question.
An array is monotonic if it is either monotone increasing or monotone decreasing.
An array is monotone increasing if all its elements from left to right are non-decreasing, an array
is monotone decreasing if all its elements from left to right are non-increasing.
Given an integer array, return true if the given array is monotonic or false otherwise.
All right, so this is the question at hand.
Let's try to understand this over here.
This is a mathematical concept.
When we say that an array is monotonic.
Now an array is monotonic if it's either monotone increasing or monotone decreasing.
So let's understand these two.
So this is monotone increasing and this is monotone decreasing.
Now if an array is monotone increasing then it means that when we go from left to right the elements
are non decreasing.
For example let's take an example like one.
And then you have um two.
And then you have three right over here.
When you go from 1 to 2 that is you're going from left to right.
Right.
So the elements are non decreasing.
So we can say that this is monotone increasing.
Now why is it mentioned over here as non decreasing.
Now let's say we add an element three over here.
Now over here we can see that these two elements are equal right.
So it's not increasing.
But still because it is non-decreasing right.
From 3 to 3 it's not decreasing.
Right.
So it's non decreasing.
Therefore we can say that this array over here is still monotone increasing.
Now what about monotone decreasing.
Let's take an example.
Again we take a simple example.
Let's say we have 321.
Now in this case when we go from left to right right.
So the elements are non increasing.
So from 3 to 2 the the value is not increasing right.
And similarly from 2 to 1 the value is not increasing.
So the elements are non increasing.
Again we add an element over here.
Let's say we have one over here.
In this case when we go from 1 to 1 the value is not decreasing.
It's equal right.
But then it is not increasing.
So that's why we can say that this array over here is also monotone decreasing.
All right.
So we have understood the question.
We have understood what we mean with monotone increasing and monotone decreasing.
Now the question says that we are given an array.
And then we have to do some operations.
And we have to identify whether the given array is monotonic.
That means whether it is monotone increasing or monotone decreasing.
And if it is, we have to return true, else we have to return false.
So that's the question now in the interview when you get this question, always feel free to ask clarifying
questions so that you thoroughly understand the question and you and the interviewer are on the same
page.
Now, what are some possible questions possibly you could ask?
Is an empty array monotonic?
And let's say that the interviewer says yes, an empty array is monotonic.
All right.
Now another question could be is an array with only one integer monotonic.
And again let's say the interviewer replies yes, this array is monotonic.
All right.
So again, as of now we have understood the question.
We have understood what is monotonic increasing and monotone decreasing.
And we have done clarifying any kind of question or doubt you have all right now at the At.
Then as a next step, let's go ahead and write test cases.
And remember, you can always take the help of the interviewer whenever you write test cases.
But over here it's pretty straightforward.
So let's go ahead and write a few test cases.
Now if the array which is given to us is one, two, three, then over here we can see that the elements
are non-decreasing.
Right.
So I'm just writing end over here.
It's non-decreasing.
So yes we can say that this array over here is monotonic or it is monotonic increasing.
Right.
So if the value is monotonic increasing it should be just non-decreasing.
Now what if we are given the array 321.
In this case we can see that the array over here is non-increasing right as we go from left to right
it's not increasing.
So therefore it is monotonic decreasing.
Right.
So yes over here also we would be returning true.
Now if the array which is given to us is one two, two.
In this case also it will be true.
Right?
It will be monotonic because in this case it's monotonic increasing right.
Again it's monotonic increasing because it is non-decreasing.
When you go from 2 to 2 again it's not decreasing.
So it's non-decreasing.
That's why it's monotonic increasing.
And we return true.
Now let's take one more example.
What if we have 333.
In this case also it will be true.
Right.
So we have got a fair understanding and about the edge cases.
We had asked clarifying questions.
Right.
So if you are given let's say an array with only one element, then we will return true.
And if you are given an empty array in that case also we will return true.
Now all of these cases were true.
What would an example be of a false case?
For example, if you have two, two, three and one, you can see as we go from left to right till here
it was monotonic right.
It was monotonic because it was non decreasing.
But then when we came from 3 to 1 it decreased right.
So when we take this array.
The.
It is not monotonic, right?
It's neither non-increasing nor non-decreasing.
So we return false.
So we have got a good understanding of the question.
Now in the next video, let's go ahead and think through how we are going to solve this.