Let's now get into the code for solving the monotonic array question.
Okay.
So first over here we're going to check whether the input array which is given to us does not have any
elements.
And remember in the previous video we had asked the interviewer that whether we should consider an empty
array to be monotonic.
And the reply that we got was that yes, an empty array should be considered monotonic.
So let's have a check for that over here.
So I will say if array dot length is equal to zero.
So if this is the case then we will just return true.
Now if this is not the case then we proceed with the remaining part of the solution.
So as we have discussed in the previous video, we are going to first compare the first element and
the last element in the array which is given to us.
So I will say const first is equal to array at index zero.
And similarly I will say const last is equal to array at index array dot length.
Minus one.
Okay, so we have got the first and last element in the array which is given to us.
Now what are the three possibilities that we have discussed in the previous video.
The first possibility is the first element.
And the last element may be equal.
Okay.
Now before we go ahead and fill the details for this, let me just over here construct the skeleton
okay.
So the first possibility is that the first element and the last element could be equal.
Now if this is not the case, the other possibility is that the first element could be less than the
last element.
Okay.
So that's again one more possibility.
And if these two are not true then the remaining possibility is that the first element is larger than
the last element okay.
So again we don't need to write that specifically because that's the only remaining case.
Right.
So this means that first is larger than last.
Now the way that we are going to approach this is that we are going to check whether the criteria needed
for each of these cases for us to conclude that the array which is given to us is monotonic.
So we are going to check whether that particular condition, which is the needed condition, has been
broken.
And if we can find that it has been broken or it has been violated, then we will just return false.
But even after going through whichever the case is, if you are not able to return false, then we would
come over here and we will just return true.
Okay, so this is the skeleton of the approach.
Now let's go ahead and fill in the details.
If the first element and the last element are equal, we would need every element in the array to be
equal for it to be monotonic.
So what we are going to do is we are going to iterate from index zero till the last index of the given
array, and we are going to check whether array at index I plus one is not equal to array at index I,
because if that is the case, then the required condition has been violated and we will just return
false.
So over here I will say for let I is equal to zero, I less than array dot length minus one.
And we have I plus plus.
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
will just return false.
Okay.
So again we are checking whether the required criteria has been violated.
And if we find that this is true we will return false.
Okay.
Now let's proceed.
Now if the case is that the first element is less than the last element, then again we will have the
same for loop.
So we will have for let I is equal to zero I less than array dot length minus one I plus plus okay.
And over here the needed criteria is that array at I plus one has to be greater than array at I okay.
Because the first element is less than the last element, or the last element is greater than the first
element.
So we are going to check whether that criteria has been broken.
So over here I will say if array at index I plus one.
Is less than array at index I.
So if this is true at any instance then we will just return false.
Okay.
And finally, we'll have to do something similar over here as well.
So over here the first element is greater than the last element.
So we would need an array at I plus one to be less than array at I.
So we will see or we will try to check whether that criteria has been broken.
And if we find that it's broken we will just return false.
So again I'll say for.
Let I is equal to zero I less than array dot length minus one.
I plus plus.
And over here I will say if array at index I plus one greater than array at index I.
Then we will just return false.
Okay, so that's it.
Now, if we were not able to return false, then over here we'll go ahead and return true.
Because the array which has been given to us is in fact monotonic.
So this is the solution to the monotonic array question.
Now let's go ahead and run this code and see if it's passing all the test cases.
And you can see that it's passing all the test cases.
Okay.
And again, do make use of the user logs.
It will help you debug your code in case you're stuck at some point.