# longest increasing sequence possible by the boundary elements of an array | function to find longest strictly increasing sequence using boundary elements ; maintains rightmost element in the sequence ; pointer to start of array ; pointer to end of array ; stores the required sequence ; traverse the array ; if arr [ i ] > arr [ j ] ; if arr [ j ] is greater than rightmost element of the sequence ; push arr [ j ] into the sequence ; update rightmost element ; push arr [ i ] into the sequence ; update rightmost element ; if arr [ i ] < arr [ j ] ; if arr [ i ] > rightmost element ; push arr [ i ] into the sequence ; update rightmost element ; if arr [ j ] > rightmost element ; push arr [ j ] into the sequence ; update rightmost element ; if arr [ i ] is equal to arr [ j ] ; if i and j are at the same element ; if arr [ i ] > rightmostelement ; push arr [ j ] into the sequence ; update rightmost element ; traverse array from left to right ; stores the increasing sequence from the left end ; traverse array from left to right ; push arr [ k ] to max_left vector ; traverse the array from right to left ; stores the increasing sequence from the right end ; traverse array from right to left ; push arr [ k ] to max_right vector ; if size of max_left is greater than max_right ; push max_left elements to the original sequence ; otherwise ; push max_right elements to the original sequence ; print the sequence ; 
def findMaxLengthSequence(N, arr):
    rightmost_element = -1
    i = 0
    j = N - 1
    sequence = []
    while (i <= j):
        if (arr[i] > arr[j]):
            if (arr[j] > rightmost_element):
                sequence .append(arr[j])
                rightmost_element = arr[j]
                j -= 1
            elif (arr[i] > rightmost_element):
                sequence .append(arr[i])
                rightmost_element = arr[i]
                i += 1
            else:
                break
        elif (arr[i] < arr[j]):
            if (arr[i] > rightmost_element):
                sequence .append(arr[i])
                rightmost_element = arr[i]
                i += 1
            elif (arr[j] > rightmost_element):
                sequence .append(arr[j])
                rightmost_element = arr[j]
                j -= 1
            else:
                break
        elif (arr[i] == arr[j]):
            if (i == j):
                if (arr[i] > rightmost_element):
                    sequence .append(arr[i])
                    rightmost_element = arr[i]
                    i += 1
                break
            else:
                sequence .append(arr[i])
                k = i + 1
                max_left = []
                while (k < j and arr[k] > arr[k - 1]):
                    max_left .append(arr[k])
                    k += 1
                l = j - 1
                max_right = []
                while (l > i and arr[l] > arr[l + 1]):
                    max_right .append(arr[l])
                    l -= 1
                if (len(max_left) > len(max_right)):
                    for element in max_left:
                        sequence .append(element)
                else:
                    for element in max_right:
                        sequence .append(element)
                break
    for element in sequence:
        print(element, end=" ")


if __name__ == '__main__':
    N = 4
    arr = [1, 3, 2, 1]
    findMaxLengthSequence(N, arr)

# Expected output:
1 2 3