P21700 [GeeksforGeeks] Shortest un ordered subarray3634
普及-
题目描述

Given an array of n distinct elements. Find the length of shortest unordered (neither increasing nor decreasing) sub array in given array.

Example 1:
Input:
n = 5
a[] = {7, 9, 10, 8, 11}
Output:
3
Explanation:
Shortest unsorted subarray is 9, 10, 8
which is of 3 elements.

Example 2:
Input:
n = 4
a[] = {1, 2, 3, 5}
Output:
0

Your Task:
You don't need to read input or print anything. Your task is to complete the function shortestUnorderedSubarray() which takes the array a[] and its size n as inputs and returns the length of shortest unordered subarray.

Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)

Constraints:
1 <= n <= 10^{5}
1 <= a[i] <= 10^{5}

输入格式

见题面

输出格式

见题面

样例输入
n = 5 a[] = {7, 9, 10, 8, 11}
样例输出
3