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
      • 10
        Basic Dynamic Programming
      • 11
        Recursion
      • 12
        Linked LIst
      • 13
        Queue & Stack
      • 14
        Binary tree + BST
      • 15
        Divide & Conquer + Advanced Sorting
      • 16
        Heap
      • 17
        Hashing
      • 18
        Graph Representation

  • 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
    Hint
    You can define a function that would check for the existence of a solution for ONLY the non-decreasing part. Then use the same function on the initial array and the reversed array.
     

    Constraints

    Time limit: 1 seconds

    Memory limit: 512 MB

    Output limit: 1 MB

    To check your solution you need to sign in
    Sign in to continue