fb pixel

Algorithms and Data Structures

  • Profound Academy

    • Status
      • 1
        Implementation
      • 2
        Bitwise operations
      • 3
        Prefix Sums
      • 4
        Sliding window / Two pointers
      • 5
        Modular Arithmetic
      • 6
        Number Theory
      • 7
        Binary Search
      • 8
        Basic Sorting
      • 9
        Greedy Algorithms

  • Sort the array

    Given an array of n numbers, you are asked if you can sort it by removing at most a single element from it. Note the array is considered to be sorted if it’s ordered in both ascending and descending directions.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ ) the number of elements in the array. The next line contains n integers separated by a space .

    Output

    The program should print Yes in case it’s possible to make the list sorted by removing at most a single element and No otherwise.

    Examples

    Input
    Output
    3 1 2 3
    Yes
    4 5 4 8 2
    Yes
    7 1 4 5 0 6 7 2
    No
    6 1 10 2 3 4 5
    Yes
    6 1 10 -1 20 30 40
    Yes
    6 1 10 -1 20 30 20
    No
     
    To check your solution you need to sign in
    Sign in to continue