Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6420
@blackshadows 6380
@mb1973 5704
@Quandray 5245
@akhayrutdinov 5111
@saiujwal13083 5046
@sanjay05 3762
@kirtidee18 3673
@marius_valentin_dragoi 3523
@mantu_singh 3510
@sushant_a 3459
Complete Leaderboard
Shortest un-ordered subarray
Basic Accuracy: 38.53% Submissions: 174 Points: 1

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 <= 105
1 <= a[i] <= 105

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Shortest un-ordered subarray

Output Window