Approach: Use two boolean flags increasing and decreasing initially set to true, loop though array start from i = 1 and update those flags by comparing array[i] and array[i-1], return the or operation of the two flags
using System;
public class Program {
public static bool IsMonotonic(int[] array) {
var isNonDecreasing = true;
var isNonIncreasing = true;
for (int i = 1; i < array.Length; i++)
{
if(array[i] < array[i-1])
{
isNonDecreasing = false;
}
if(array[i] > array[i-1])
{
isNonIncreasing = false;
}
}
return isNonIncreasing||isNonDecreasing;
}
}