An array is monotonic if it is either monotone increasing or monotone decreasing.
An array A is monotone increasing if for all i <= j, A[i] <= A[j].  An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
Return true if and only if the given array A is monotonic.
 



Example 1:
Input: [1,2,2,3]
Output: true


Example 2:
Input: [6,5,4,4]
Output: true


Example 3:
Input: [1,3,2]
Output: false


Example 4:
Input: [1,2,4,5]
Output: true


Example 5:
Input: [1,1,1]
Output: true

 
Note:

1 <= A.length <= 50000
-100000 <= A[i] <= 100000

Difficulty: introductory

Starter code:

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        

Solutions:
class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if not A: return True
        increasing = True
        index = 0
        while index<len(A)-1 and A[index] == A[index+1]:
            index+=1
        if index == len(A)-1:
            return True
        if A[index] > A[index+1]:
            increasing = False
        for i in range(len(A)-1):
            if increasing:
                if A[i] > A[i+1]:
                    return False
            else:
                if A[i] < A[i+1]:
                    return False
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        n = len(A)
        if self.checkMonInc(A, n) or self.checkMonDec(A, n):
            return True
        return False
        
    def checkMonInc(self, A, n):
        for i in range(1, n):
            if A[i] >= A[i-1]:
                continue
            else:
                return False
        return True

    def checkMonDec(self, A, n):
        for i in range(1, n):
            if A[i] <= A[i-1]:
                continue
            else:
                return False
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        is_asc = True
        is_desc = True
        
        for i in range(len(A) - 1):
            if A[i] > A[i+1]:
                is_asc = False
            if A[i] < A[i+1]:
                is_desc = False
                
        return is_asc or is_desc

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) == 1:
            return True
        
        direction = 0
        
        for i in range(len(A)-1) : 
            j = i+1
            tmp = A[i] - A[j]
            if tmp == 0:
                continue
            elif tmp > 0:
                if direction == 0 or direction == -1:
                #if direction < 1:
                    direction = -1
                else:
                    return False
            else:
                if direction == 0 or direction == 1:
                #if direction > -1 :
                    direction = 1
                else:
                    return False
            
            #if A[i] == A[j]:
            #    continue
            #elif A[i] > A[j]:
            #    if direction < 0:
            #        continue
            #    elif direction == 0:
            #        direction = -1
            #    else:
            #        return False
            #else:# A[i] < A[j]
            #    if direction > 0:
            #        continue
            #    elif direction == 0:
            #        direction = 1
            #    else:
            #        return False
                
        return True
                    
                    
            
        


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increasing = all(A[i] <= A[i+1] for i in range(len(A)-1))
        decreasing = all(A[i] >= A[i+1] for i in range(len(A)-1))
        return increasing or decreasing

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increasing = decreasing = True

        for i, value in enumerate(A):
            if i:
                if value > A[i - 1]:
                    increasing = False
                if value < A[i - 1]:
                    decreasing = False
            if not increasing and not decreasing:
                return False
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        monotonic = 2
        lenA = len(A)
        for i in range(lenA-1):
            if A[i] > A[i+1]:
                monotonic -= 1
                break
        for i in range(lenA-1):
            if A[i] < A[i+1]:
                monotonic -= 1
                break
        return monotonic

class Solution(object):
    def isMonotonic(self, A):
        increasing = decreasing = True

        for i in range(len(A) - 1):
            if A[i] > A[i+1]:
                increasing = False
            if A[i] < A[i+1]:
                decreasing = False

        return increasing or decreasing
                


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        prev = 0
        cur = 1
        while cur < len(A):
            if A[prev] <= A[cur]:
                prev += 1
                cur += 1
            else:
                break
        if cur >= len(A):
            return True
        prev = 0
        cur = 1
        while cur < len(A):
            if A[prev] >= A[cur]:
                prev += 1
                cur += 1
            else:
                break
        if cur >= len(A):
            return True
        return False


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        # two pointers to traverse array checking increasing and decreasing at the same time.
        i, j = 0, 0
        # two variables to keep track of values increasing and decreasing.
        tempi, tempj = -100000, 100000
        # flag to check if monotonic.
        flagi, flagj = True, True
        # go through array, check if monotonic.
        while i < len(A) and j < len(A):
            if tempi <= A[i]:
                tempi = A[i]
            else:
                flagi = False
            if tempj >= A[j]:
                tempj = A[j]
            else:
                flagj = False 
            i += 1
            j += 1
        # return True by default.
        return flagi or flagj

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if A == sorted(A) or A == sorted(A, reverse=True) :
            return 1
        return 0

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        flow = None
        for i in range(len(A)-1):
            if A[i] > A[i+1]:
                if not flow:
                    flow = 'd'
                
                elif flow != 'd':
                    return False
                continue
            if  A[i] < A[i+1]:
                if not flow:
                    flow = 'i'
                elif flow != 'i':
                    return False
                continue
        return True
                        


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if sorted(A)==A or sorted(A,reverse=True)==A:
            return True
        else:
            return False


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        n = len(A)
        return all([A[i] >= A[i - 1] for i in range(1, n)]) or all([A[i] <= A[i - 1] for i in range(1, n)])

class Solution:
    def isMonotonic(self, a: List[int]) -> bool:
        n = len(a)
        std = 0
        for i in range(0, n-1):

            sgn = a[i] - a[i+1]

            if sgn == 0:
                continue
                
            sgn = abs(sgn)/sgn
            if std == 0:
                std = sgn
            if sgn != std and sgn != 0:
                return False

        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        res=''
        for x in range(1, len(A)):
            if A[x]>A[x-1]:
                res='pos'
            elif A[x]==A[x-1]:
                continue
            else:
                res='neg'
        if (res!='pos' and res!='neg'):
            return True
        for x in range(1, len(A)):
            if res=='neg':
                if A[x] > A[x-1]:
                    return False
                else:
                    continue
            else:
                if A[x] < A[x-1]:
                    return False
                else:
                    continue
        return True
                
                


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) == 1:
            return True
        i = 0
        while (i < len(A) - 1 and A[i] == A[i + 1]):
            i += 1
        if (i >= len(A) - 1):
            return True
        if A[i] > A[i + 1]:
            while i < len(A) - 1:
                if A[i] < A[i + 1]:
                    return False
                i += 1
            return True
        else:
            while i < len(A) - 1:
                if A[i] > A[i + 1]:
                    return False
                i += 1
            return True
#         either we are at a spot where A[i] != A[i + 1]


class Solution:
    def isMonotonic(self, arr: List[int]) -> bool:
        length = len(arr)
        dec = inc = 1
        for i in range(length-1):
            if arr[i] < arr[i+1]:
                dec += 1
            elif arr[i] > arr[i+1]:
                inc += 1
            else:
                dec += 1
                inc += 1
        if dec == length:
            return True
        elif inc == length:
            return True
        else:
            return False

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        diff_lst = [A[i + 1] - A[i] for i in range(len(A) - 1)]
        positive = all(i >= 0 for i in diff_lst)
        negative = all(i <= 0 for i in diff_lst)
        return positive or negative

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        dec = False
        
        for i in range(len(A) - 1):
            if A[i] < A[i+1]:
                dec = True
        for i in range(len(A) - 1):
            if A[i] > A[i+1] and dec:
                return False
        return True

class Solution:
    def isMonotonic(self, A):
        is_monotonic_asc = True;
        is_monotonic_desc = True;
        
        for i in range(1, len(A)):
            if A[i] < A[i - 1]:
                is_monotonic_asc = False;
            if A[i] > A[i - 1]:
                is_monotonic_desc = False;
            
        return is_monotonic_asc or is_monotonic_desc;

class Solution:
    def isMonotonic(self, a: List[int]) -> bool:
        isIncreasing = isDecreasing = True
        for i in range(len(a) - 1):
            if(a[i] < a[i+1]):
                isIncreasing = False
            if(a[i] > a[i+1]):
                isDecreasing = False
        return isIncreasing or isDecreasing


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        inc, dec = True, True
        
        for i in range(len(A) - 1):
            if A[i+1] > A[i]:
                dec = False
            if A[i+1] < A[i]:
                inc = False
        return inc or dec


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        is_monotonic_asc = True
        is_monotonic_desc = True
        
        for i in range(1, len(A)):
            if A[i] < A[i - 1]:
                is_monotonic_asc = False
            if A[i] > A[i - 1]:
                is_monotonic_desc = False
            
        return is_monotonic_asc or is_monotonic_desc

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        

#         inc = all([A[i]<=A[i+1] for i in range(0,len(A)-1)])
#         dec = all([A[i]>=A[i+1] for i in range(0,len(A)-1)])
        
#         return inc or dec

#         inc = True
#         dec = True
        
#         for i in range(0,len(A)-1):
#             if A[i]>A[i+1]:
#                 inc=False
#             if A[i]<A[i+1]:
#                 dec=False
                
#             if not inc and not dec:
#                 return False
        
#         return inc or dec
    
#         if A[-1] > A[0]:            
#             return all(A[i] <= A[i + 1] for i in range(0, len(A) - 1))

#         else:
#             return all(A[i] >= A[i + 1] for i in range(0, len(A) - 1))

        if A[-1] > A[0]:            
            inc=True
            for i in range(0,len(A)-1):
                if A[i]>A[i+1]:
                    return False
            else:
                return True

        else:
            dec=True
            for i in range(0,len(A)-1):
                if A[i]<A[i+1]:
                    return False
            else:
                return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        isAscending = True
        isDescending = True
        
        for i in range(len(A) - 1):
            if A[i] > A[i+1]:
                isAscending = False
            if A[i] < A[i+1]:
                isDescending = False
        return isAscending or isDescending

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        asc = True
        dsc = True
        for i in range(1,len(A)):
            if A[i-1]<A[i]:
                dsc=False
            elif A[i-1]>A[i]:
                asc=False
        return asc or dsc


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        is_increasing = is_decreasing = True
        for i in range(1, len(A)):
            if A[i] < A[i - 1]:
                is_decreasing = False
            if A[i] > A[i - 1]:
                is_increasing = False
        return is_decreasing or is_increasing

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increasing = decreasing = True

        for i in range(len(A) - 1):
            if A[i] > A[i+1]:
                increasing = False
            if A[i] < A[i+1]:
                decreasing = False

        return increasing or decreasing

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        is_asc = True
        is_desc = True
        
        for i in range(1, len(A)):
            if A[i] > A[i-1]:
                is_desc = False
            if A[i] < A[i-1]:
                is_asc = False
        
        return is_asc or is_desc

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        # Valid monotic number of the following characteristic
        # Increasing 
        # Decreasing
        # None increasing or decreasing

        # Linear time complexity is the best thing to do.

        isIncreasing = True
        isDecreasing = True

        # check if the number is Monotonic
        index = 1

        while index < len(A):
            
            previous = index - 1
            current = index

            # In any given if the current number is bigger then number is increasing
            # So assign isIncreasing to false
            if A[previous] < A[current]:
                isDecreasing = False
                
            # if the current is smaller the number is decreasing
            # so assign isIncreasing to false
            if A[previous] > A[current]:
                isIncreasing = False
            index += 1
        
        return isIncreasing or isDecreasing

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        len_ = len(A)
        
        if (len_ == 0 or len_ == 1 or len_ == 2):
            return True
        
        f, l = A[0], A[-1]
        prev = f
        
        for n in A[1:]:
            if (f < l):
                if (n < prev):
                    return False
            if (f > l):
                if (n > prev):
                    return False
            if (f == l):
                if (n != prev):
                    return False
            prev = n
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        dec = False
        inc = False

        for i in range(len(A) - 1):
            j = i+1
            if A[i] is not A[j]: 
                if A[i] < A[j]:
                    inc = True
                elif A[i] > A[j]:
                    dec = True
            if(inc and dec):
                return False
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        if len(A) == 0: return False
        if len(A) == 1: return True
        
        incr = 0
        
        for i in range(1, len(A)):
            if A[i] - A[i-1] > 0 and incr == 0:
                incr = 1
            elif A[i] - A[i-1] > 0 and incr == -1:
                return False
            elif A[i] - A[i-1] < 0 and incr == 0:
                incr = -1
            elif A[i] - A[i-1] < 0 and incr == 1:
                return False
                
        return True
            


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) < 3:
            return True
        i = 0
        while i < len(A) - 1 and A[i] == A[i+1]:
            i += 1
        if i == len(A) - 1:
            return True
        asc = (A[i] < A[i+1])
        for j in range(i, len(A) - 1):
            if A[j] == A[j+1]:
                continue
            tmp = (A[j] < A[j+1])
            if tmp != asc:
                return False
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        icounter = 0
        dcounter = 0
        for i in range(len(A)-1):
            if A[i] > A[i+1]:
                dcounter += 1
            if A[i] < A[i+1]:
                icounter += 1
            if A[i] == A[i+1]:
                icounter += 1
                dcounter += 1
        if icounter == len(A) - 1 or dcounter == len(A) - 1:
            return True
        return False


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        def isIncreasing(self, A: List[int]) -> bool:
            mono = True
            for i in range(len(A)):
                if i == (len(A) - 1):
                    break
                if A[i] > A[i+1]:
                    mono = False
            return mono
        def isDecreasing(self, A: List[int]) -> bool:
            m = True
            for i in range(len(A)):
                if i == (len(A) - 1):
                    break
                if A[i] < A[i+1]:
                    m = False
            return m
        if isDecreasing(self,A) or isIncreasing(self,A):
            return True
        return False
            


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) <= 1:
            return True
        
        increasing = True
        decreasing = True
        
        for i in range(len(A)-1):
            if A[i] > A[i+1]:
                increasing = False
        
        if not increasing:
            for i in range(len(A)-1):
                if A[i] < A[i+1]:
                    decreasing = False
        
        return True if increasing or decreasing else False

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if not A or len(A) < 2:
            return True
        
        self.direction = None
        
        return self.isMonotonicHelper(A, 0, len(A)-1)
    
    def isMonotonicHelper(self, A, start, end):
        if start < end:
            mid = (start + end) // 2
            if self.isMonotonicHelper(A, start, mid) and self.isMonotonicHelper(A, mid+1, end):
                if self.direction is None:
                    if A[mid] < A[mid+1]:
                        self.direction = 1
                    elif A[mid] > A[mid+1]:
                        self.direction = 0
                    return True
                elif self.direction == 1:
                    return A[mid] <= A[mid+1]
                else:
                    return A[mid] >= A[mid+1]
            else:
                return False
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if min(A) == A[0]:
            start = 0
            increment = 1;
            end = len(A)
        elif max(A) == A[0]:
            start = len(A) - 1
            increment = -1
            end = 0
        else:
            return False
        num = A[start]
        print(start, num)
        for i in range(start, end, increment):
            print(A[i], num)
            if A[i] < num:
                return False
            num = A[i]
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increasing = decreasing = True
        
        for i in range(len(A) - 1):
            if A[i] > A[i+1]:
                increasing = False
            if A[i] < A[i+1]:
                decreasing = False
        if increasing or decreasing:
            return increasing or decreasing

class Solution:
    
    def check(self, B):
        self.B = B
        result = []
        for i in range(len(self.B) - 1):
            if self.B[i] <= self.B[i+1]:
                result.append(True)
            else:
                result.append(False)
        
        final = all(result)
        return final
    
    
    def isMonotonic(self, A: List[int]) -> bool:
        
        a = []
        
        a.append(self.check(A))
        a.append(self.check(A[::-1]))
        
        b = any(a)

        return b        


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) <= 1:
            return True
        t = 0
        for i in range(len(A) - 1):
            if A[i] < A[i+1] and t == -1:
                return False
            elif A[i] > A[i+1] and t ==1:
                return False
            elif A[i] < A[i+1] and t == 0:
                t = 1
            elif A[i] > A[i+1] and t == 0:
                t = -1
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        climb = None
        bef = None
        for num in A:
            if bef is None:
                pass
            elif climb is None and num > bef:
                climb = True
            elif climb is None and num < bef:
                climb = False
            elif climb is None and num == bef:
                pass
            elif climb is True and num >= bef:
                pass
            elif climb is False and num <= bef:
                pass
            else:
                return False
            bef = num
        
        return True    

class Solution:
    def isMonotonic(self, arr: List[int]) -> bool:
        # ok 
        f=True
        n = len(arr)
        for i in range(n-1):
            if arr[i]>arr[i+1]:
                f=False
                break
        if f:return True
        r=True
        for i in range(n-1):
            if arr[i]<arr[i+1]:
                r=False
                break
        return r


def isMono(A, forward=True):
    curValue = A[0]
    
    for i in range(1, len(A)):
        if forward and A[i] < curValue:
            return False
        if not forward and A[i] > curValue:
            return False
        
        curValue = A[i]
    
    return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        return isMono(A) or isMono(A, False)

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        is_decreasing = is_increasing = True
        for i in range(len(A)-1):
            if A[i] < A[i + 1]: is_increasing = False
            if A[i] > A[i + 1]: is_decreasing = False
        return is_decreasing or is_increasing


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        isIncreasing, isDecreasing = True, True
        
        for i in range(len(A) - 1):
            if A[i] > A[i+1]:
                isIncreasing = False
            elif A[i] < A[i+1]:
                isDecreasing = False
            
        return isIncreasing or isDecreasing

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if sorted(A) == A:
            return True
        elif sorted(A, reverse = True) == A:
            return True
        else:
            return False
                


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        decreasing = True
        increasing = True
        for i in range(0, len(A)-1):
            if A[i] > A[i+1]: decreasing = False
            if A[i] < A[i+1]: increasing = False
        
        return decreasing or increasing


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        res1 = sorted(A, reverse=True)
        res2 = sorted(A)
        if res1 == A or res2 == A:
            return True
        return False

class Solution(object):
    def isMonotonic(self, A):
        return (all(A[i] <= A[i+1] for i in range(len(A) - 1)) or
                all(A[i] >= A[i+1] for i in range(len(A) - 1)))

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        # initial value
        mono = 0
        
        for i in range(len(A) - 1):
            if A[i + 1] - A[i] != 0:
                if mono == 0:
                    mono = A[i + 1] - A[i]
                else:
                    if mono * (A[i + 1] - A[i]) < 0:
                        return False

            
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increase, decrease = True, True
        for i in range(1, len(A)):
            if A[i] > A[i - 1]:
                decrease = False
            if A[i] < A[i - 1]:
                increase = False
        return increase or decrease

class Solution(object):
    def isMonotonic(self, A):
        monotonicIncr = True
        monotonicDecr = True

        for i in range(len(A) - 1):
            j = i + 1
            if A[i] > A[j]:
                monotonicIncr = False
            if A[i] < A[j]:
                monotonicDecr = False

        return monotonicIncr or monotonicDecr

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increasing = True
        for x, y in zip(A, A[1:]):
            if x > y:
                increasing = False
                break
        decreasing = True
        for x, y in zip(A, A[1:]):
            if x < y:
                decreasing = False
                break
        return increasing or decreasing


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
       
        if A[-1] - A[0] > 0:
            signal = 'incr'
        elif A[-1] - A[0] == 0:
            signal = 'same'
        elif A[-1] - A[0] < 0:
            signal = 'decr'
        
        if signal == 'same':
            if A == [A[0]] * len(A):
                return True
            else:
                return False
        
        elif signal == 'incr':
            for i in range(1, len(A)):
                if A[i] < A[i- 1]:
                    return False
            return True
       
        elif signal == 'decr':
            for i in range(1, len(A)):
                if A[i] > A[i-1]:
                    return False
            return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        count=0
        count2=0
        for x in range(len(A)-1):
            if(A[x]<=A[x+1]):
                count+=1
            if(A[x]>=A[x+1]):
                count2+=1
                print(count2, A[x])
        
        if(count== len(A)-1 or count2==len(A)-1):
            return True
        else:
            return False

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        inc=False
        i=0
        while i<len(A)-1 and A[i]==A[i+1]:
            i+=1
        if i==len(A)-1:
            return True
        if A[i]<A[i+1]:
            inc=True
        
        for i in range(1,len(A)-1):
            if inc==True:
                if A[i]>A[i+1]:
                    return False
            if inc==False:
                if A[i+1]>A[i]:
                    return False
        return True
        


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        B=A.copy()
        A.sort()
        if A==B or A[::-1]==B:
            return True
        return False


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        up = False
        if A[0] < A[len(A)-1]:
            up = True
        elif A[0] > A[len(A)-1]:
            up = False
    
        for i in range(len(A)-1):
            if up:
                print((A[i], A[i+1]))
                if A[i] > A[i+1]:
                    return False
            else:
                if A[i] < A[i+1]:
                    return False
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increase, decrease = True, True
        
        for i in range(1, len(A)):
            if A[i-1] > A[i]:
                increase = False
                
            if A[i-1] < A[i]:
                decrease = False
                
        return increase or decrease
    


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        up = down = True
        
        for i in range(1, len(A)):
            if A[i - 1] > A[i]:
                up = False

            if A[i - 1] < A[i]:
                down = False
        
        return up or down

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        new_array = sorted(A)
        new_array_rev = sorted(A, reverse=True)
        
        if A == new_array or A == new_array_rev:
            return True
        else:
            return False


class Solution:
    def isMonotonic(self, a: List[int]) -> bool:
#         n = len(a)
        
#         if n == 1:
#             return True
        
#         asc = True if a[n-1] - a[0] >= 0 else False
        
#         for i in range(1, n):
#             if asc:
#                 if a[i] - a[i-1] < 0:
#                     return False
#             else:
#                 if a[i] - a[i-1] > 0:
#                     return False
                
        
#         return True

        increasing = decreasing = True

        for i in range(len(a) - 1):
            if a[i] > a[i+1]:
                increasing = False
            if a[i] < a[i+1]:
                decreasing = False

        return increasing or decreasing
    
'''
[1,2,2,3]
[6,5,4,4]
[1,3,2]
[1,2,4,5]
[1,1,1]
[1,2,3,4,0]
'''

class Solution:
    
    def isMonotonic(self, A):
        return (all(A[i] <= A[i+1] for i in range(len(A) - 1)) or
                all(A[i] >= A[i+1] for i in range(len(A) - 1)))
    


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        is_monotonic_incr = True
        is_monotonic_desc = True
        array1=A
        for num in range(1,len(array1)):
            # check if second element is smallar that first
            if (array1[num] < array1[num-1]):
                print('in first if',array1[num] ,array1[num-1])
                is_monotonic_incr = False
            # check if second element is greater that first
            if (array1[num] > array1[num-1]):
                print('in second if',array1[num] ,array1[num-1])
                is_monotonic_desc = False
        
        return is_monotonic_incr or is_monotonic_desc

class Solution(object):
    def isMonotonic(self, A):
        increasing = decreasing = True

        for i in range(len(A) - 1):
            if A[i] > A[i+1]:
                increasing = False
            if A[i] < A[i+1]:
                decreasing = False

        return increasing or decreasing

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        return((A==sorted(A))or(A==sorted(A,reverse=True)))

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if not A: return True
        isIncrementing, prev = None, A[0]
        for i in range(1,len(A)):
            print(prev,A[i],isIncrementing)
            if prev < A[i] and isIncrementing == False: return False
            if prev > A[i] and isIncrementing == True:  return False
            if prev < A[i]: isIncrementing = True
            if prev > A[i]: isIncrementing = False
            prev = A[i]
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        sort = sorted(A)
        return A==sort or A[::-1]==sort

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        direction = 0
        for idx, ele in enumerate(A):
            if idx == 0:
                continue
            
            if ele > A[idx - 1] and direction == 0:
                direction = 1
            elif ele < A[idx - 1] and direction == 0:
                direction = -1
            elif ele > A[idx - 1] and direction == -1:
                return False
            elif ele < A[idx - 1] and direction == 1:
                return False
            
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) == 1:
            return True
        
        i = 0
        n = len(A)
        while i < n-1 and A[i] == A[i+1]:
            i += 1
        if i == n-1:
            return True
        if A[i] > A[i+1]:
            while i < n-1 and A[i] >= A[i+1]:
                i += 1
            if i != n-1:
                return False
            else:
                return True
        else:
            while i < n-1 and A[i] <= A[i+1]:
                i += 1
            if i != n-1:
                return False
            else:
                return True
        
        


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        i = 1
        pr = None
        cur = None
        if len(A) < 3:
            return True
        while i < len(A) - 1:
            if A[i] >  A[i-1] and A[i] > A[i +1]:
                return False
            elif  A[i] <  A[i-1] and A[i] < A[i +1]:
                return False
            elif (A[i] >=  A[i-1] and A[i] < A[i +1]) or (A[i] >  A[i-1] and A[i] <= A[i +1])  :
                cur = 'I'
            elif (A[i] <= A[i -1] and A[i] > A[i +1]) or (A[i] < A[i -1] and A[i] >= A[i +1]):
                cur = 'D'            
            if pr is None and cur is not None:
                pr = cur
            elif pr is not None and cur is not None and pr != cur:
                return False            
            i += 1
            
        return True
            
            


class Solution:
    def monotonicArrayIncreasing(self,a):
        i = 0
        while i+1 < len(a):
            if a[i] > a[i+1]:
                return False
            i += 1
        return True

    def monotonicArrayDecreasing(self,a):
        i = 0
        while i+1 < len(a):
            if a[i] < a[i+1]:
                return False
            i += 1
        return True
    def isMonotonic(self, A: List[int]) -> bool:
        x=self.monotonicArrayDecreasing(A)
        y=self.monotonicArrayIncreasing(A) 
        return x or y

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increasing = None
        for i in range(1, len(A)):
            if increasing is None:
                if A[i] != A[i - 1]:
                    increasing = A[i] > A[i - 1]
            elif increasing and A[i] < A[i - 1]:
                return False
            elif not increasing and A[i] > A[i - 1]:
                return False
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) < 2:
            return True
        
        increasing = None
        previous = A[0]
        for element in A[1:]:
            if increasing == True and element < previous:
                return False
            
            if increasing == False and element > previous:
                return False
            
            if increasing is None and element != previous:
                increasing = element > previous
            
            previous = element
            
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        ans=True
        n=len(A)
        if n<=2:
            return ans
        # ind=0
        # while True:
        #     if ind+1==n:
        #         break
        #     else:
        #         if A[ind]<A[ind+1]:
        #             comp=\"<\"
        #             break
        #         elif A[ind]>A[ind+1]:
        #             comp=\">\"
        #             break
        #         else:
        #             ind+=1
        # for j in range(ind,n-1):
        #     if  not eval(\"A[j]\"+comp+\"=A[j+1]\"):
        #                  return False
        # return ans
        left=True
        right=True
        for i in range(1,n):
            if A[i]>A[i-1]:
                left=False
                break
        for i in range(1,n):
            if A[i]<A[i-1]:
                right=False
                break
        return left or right
        
            
            


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        asc_arr = sorted(A)
        desc_arr = sorted(A, reverse=True)
        return (A == asc_arr or A == desc_arr)


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        if (len(A) < 3):
            return True
        gt = False
        lt = False
        
        for i in range(len(A) - 1):
            
            if A[i] > A[i + 1]:
                gt = True
            if A[i] < A[i + 1]:
                lt = True
            if (gt and lt):
                return False
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) <= 1:
            return True
        
        flag = -1
        
        if A[0] < A[1]:
            flag = 0
        elif A[0] == A[1]:
            flag = 1
        else:
            flag = 2
             
        for i in range(0, len(A) - 1):
            if flag == 0 and A[i] > A[i + 1]:
                return False
            elif flag == 2 and A[i] < A[i + 1]:
                return False
            elif flag == 1:
                if A[i] > A[i + 1]:
                    flag = 2
                elif A[i] < A[i + 1]:
                    flag = 0
                else:
                    flag = 1
                    
        return True            


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increasing = True
        decreasing = True
        for i in range(len(A)-1):
            if A[i] > A[i+1]:
                increasing = False
            if A[i] < A[i+1]:
                decreasing = False
            
            
        return increasing or decreasing

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        inc = True
        dec = True
        
        for i in range(1,len(A)):
            if A[i-1] > A[i]:
                inc = False
            if A[i-1] < A[i]:
                dec = False
        return inc or dec

        


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) <= 1:
            return True
        last_diff = 0
        for i in range(1, len(A)):
            cur_diff = A[i] - A[i - 1]
            if cur_diff < 0:
                if last_diff == 0:
                    last_diff = cur_diff
                elif last_diff > 0:
                    return False
            elif cur_diff > 0:
                if last_diff == 0:
                    last_diff = cur_diff
                elif last_diff < 0:
                    return False
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        flag = None
        
        for i in zip(A, A[1:]):
            temp = i[1] - i[0]
            if flag:
                if flag*temp < 0:
                    return False
            else:
                if temp > 0:
                    flag = 1
                elif temp < 0:
                    flag = -1
           
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        monotone = 0
        for i in range(len(A)-1):
            m = A[i+1]-A[i]
            if m*monotone<0:
                return False
            if monotone == 0 and m != 0:
                monotone = 1 if m>0 else -1
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        increase = []
        decrease = []
        i, d = [], []
        for idx, v in enumerate(A):
            while decrease and decrease[-1][0] >= v:
                dv, di = decrease.pop()
                if not d:
                    d.append((dv, di))
                elif d[-1][1] < di:
                    d.append((dv, di))
            decrease.append((v, idx))
                
            while increase and increase[-1][0] <= v:
                iv, ii = increase.pop()
                if not i:
                    i.append((iv, ii))
                elif i[-1][1] < ii:
                    i.append((iv, ii))
            increase.append((v, idx))
        if decrease and len(decrease) == 1:
            g = decrease.pop()
            if d and d[-1][0] >= g[0]:
                d.append(g)
            else:
                d.append(g)
        if increase and len(increase) == 1:
            g = increase.pop()
            if i and i[-1][0] <= g[0]:
                i.append(g)
            else:
                i.append(g)
        return len(i) == len(A) or len(d) == len(A)

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        i=0
        j=1
        if(A.index(min(A))==0 or A.index(min(A)) == (len(A)-1) or min(A)==A[-1]):
            if(A.index(min(A)) == 0):
                while j <= len(A)-1:
                    if(A[i] <= A[j]):
                        i = i+1
                        j = j+1
                    else:
                        return False
            elif(A.index(min(A)) == len(A)-1 or min(A) == A[-1]):
                while j <= len(A)-1:
                    if(A[i] >= A[j]):
                        i = i+1
                        j = j+1
                    else:
                        return False
        else:
            return False
        return True


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        return sorted(A) == A or sorted(A, reverse=True) == A

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A)<2:
            return True
        
        increasing = False
        decreasing = False
        i = 0
        
        while i < len(A)-1 and (not increasing and not decreasing):
            if A[i] == A[i+1]:
                i+=1
                continue
            
            if A[i] < A[i+1]:
                increasing = True
            
            else:
                decreasing= True
                
            i+=1

        
                
        if increasing:
            while i < len(A)-1:
                if A[i] > A[i+1]:
                    return False
                i+=1
                
            return True
        
        if decreasing:
            while i < len(A)-1:
                if A[i] < A[i+1]:
                    return False
                i+=1
                
            return True
        
        if not increasing and not decreasing:
            return True
                
        
        
                

        


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        inc = True
        dec = True
        for i in range(len(A)-1):
            if A[i+1] - A[i] > 0:
                inc = inc and True
                dec = False
            elif A[i+1] - A[i] < 0:
                dec = dec and True
                inc = False
                
        return inc or dec

from enum import Enum
class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) <= 2:
            return True
        
        class Comp(Enum):
            inc = 0
            eq = 1
            dec = 2
            
        def to_comp(left, right):
            if right > left:
                return Comp.inc
            elif right == left:
                return Comp.eq
            else:
                return Comp.dec
        
        comp = to_comp(A[0], A[1])
        
        for i in range(2, len(A)):
            new_comp = to_comp(A[i - 1], A[i])
            if new_comp == Comp.inc and comp == Comp.dec:
                return False
            elif new_comp == Comp.dec and comp == Comp.inc:
                return False
            if new_comp != Comp.eq:
                comp = new_comp
            
        return True

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) <= 2: return True
        
        d = A[1] - A[0]
        
        for i in range(1, len(A)-1):
            new_d = A[i+1] - A[i]
                
            if new_d * d < 0: return False
            if new_d != 0:
                d = new_d
        return True
            


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        length = len(A)
        monotone_inc = True
        monotone_dec = True
        for i in range(length-1):
            if A[i+1] >= A[i]:
                continue
            else:
                monotone_inc = False
        for i in range(length-1):
            if A[i+1] <= A[i]:
                continue
            else:
                monotone_dec = False
        return monotone_inc or monotone_dec
        


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        inc, dec = True, True
        for i in range(1, len(A)):
            a, b = A[i], A[i-1]
            inc = inc & (a >= b)
            dec = dec & (a <= b)
            if(not (inc or dec)): return False
        return inc or dec

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if (len(A) == 1):
            return 1
        
        dir = 0
        for (i,j) in zip(A, A[1:] + [A[len(A) - 1]]):
            if (i != j):   
                cdir = (i - j) / abs(i - j)           
                if (dir == 0):
                    dir = cdir
                elif (dir != cdir):
                    return 0

        return 1
                           

    
    


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        if len(A) <= 1:
            return True
        
        inc, dec = True, True
        
        for i in range(len(A)-1):
            if A[i+1] > A[i]:
                dec = False
            if A[i+1] < A[i]:
                inc = False
                
        return dec or inc

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        up = down = True

        for i in range(len(A)-1):
            if A[i] > A[i+1]:
                up = False
            if A[i] < A[i + 1]:
                down = False
                
        return up or down  

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        import numpy as np
        if len(A)==1:
            return(True)
        else:
            diff = np.diff(A)
            return(min(diff)>=0 or max(diff)<=0)


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        
        a1, a2 = sorted(A), sorted(A, reverse = True)
        if A == a1 or A == a2:
            return True
        return False
        '''
        nD, nI = 1, 1
        for i in range(1, len(A)):
            if A[i] - A[i-1] < 0:
                nD = 0
            elif A[i] - A[i-1] > 0 :
                nI = 0
        return nI or nD
        '''

class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        return A == sorted(A) or A == sorted(A)[::-1]


class Solution:
    def isMonotonic(self, A: List[int]) -> bool:
        if len(A) < 2:
            return True
        asc = None
        for i in range(1, len(A)):
            if A[i] == A[i-1]:
                continue
            if asc is None:
                asc = A[i-1] < A[i]
            else:
                if asc != (A[i-1] < A[i]):
                    return False
        return True