Shortest Un-ordered Subarray
An array is given of n length, and problem is that we have to find the length of shortest unordered {neither increasing nor decreasing} sub array in given array.
Examples:
Input : n = 5
7 9 10 8 11
Output : 3
Explanation : 9 10 8 unordered sub array.
Input : n = 5
1 2 3 4 5
Output : 0
Explanation : Array is in increasing order.
The idea is based on the fact that size of shortest subarray would be either 0 or 3. We have to check array element is either increasing or decreasing, if all array elements are in increasing or decreasing, then length of shortest sub array is 0, And if either the array element is not follow the increasing or decreasing then it shortest length is 3.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool increasing( int a[], int n)
{
for ( int i = 0; i < n - 1; i++)
if (a[i] >= a[i + 1])
return false ;
return true ;
}
bool decreasing( int a[], int n)
{
for ( int i = 0; i < n - 1; i++)
if (a[i] < a[i + 1])
return false ;
return true ;
}
int shortestUnsorted( int a[], int n)
{
if (increasing(a, n) == true ||
decreasing(a, n) == true )
return 0;
else
return 3;
}
int main()
{
int ar[] = { 7, 9, 10, 8, 11 };
int n = sizeof (ar) / sizeof (ar[0]);
cout << shortestUnsorted(ar, n);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG {
public static boolean increasing( int a[], int n)
{
for ( int i = 0 ; i < n - 1 ; i++)
if (a[i] >= a[i + 1 ])
return false ;
return true ;
}
public static boolean decreasing( int arr[], int n)
{
for ( int i = 0 ; i < n - 1 ; i++)
if (arr[i] < arr[i + 1 ])
return false ;
return true ;
}
public static int shortestUnsorted( int a[], int n)
{
if (increasing(a, n) == true ||
decreasing(a, n) == true )
return 0 ;
else
return 3 ;
}
public static void main (String[] args) {
int ar[] = new int []{ 7 , 9 , 10 , 8 , 11 };
int n = ar.length;
System.out.println(shortestUnsorted(ar,n));
}
}
|
Python3
def increasing(a, n):
for i in range ( 0 , n - 1 ):
if (a[i] > = a[i + 1 ]):
return False
return True
def decreasing(a, n):
for i in range ( 0 , n - 1 ):
if (a[i] < a[i + 1 ]):
return False
return True
def shortestUnsorted(a, n):
if (increasing(a, n) = = True or
decreasing(a, n) = = True ):
return 0
else :
return 3
ar = [ 7 , 9 , 10 , 8 , 11 ]
n = len (ar)
print (shortestUnsorted(ar, n))
|
C#
using System;
class GFG {
public static bool increasing( int [] a, int n)
{
for ( int i = 0; i < n - 1; i++)
if (a[i] >= a[i + 1])
return false ;
return true ;
}
public static bool decreasing( int [] arr, int n)
{
for ( int i = 0; i < n - 1; i++)
if (arr[i] < arr[i + 1])
return false ;
return true ;
}
public static int shortestUnsorted( int [] a, int n)
{
if (increasing(a, n) == true ||
decreasing(a, n) == true )
return 0;
else
return 3;
}
public static void Main()
{
int [] ar = new int [] { 7, 9, 10, 8, 11 };
int n = ar.Length;
Console.WriteLine(shortestUnsorted(ar, n));
}
}
|
PHP
<?php
function increasing( $a , $n )
{
for ( $i = 0; $i < $n - 1; $i ++)
if ( $a [ $i ] >= $a [ $i + 1])
return false;
return true;
}
function decreasing( $a , $n )
{
for ( $i = 0; $i < $n - 1; $i ++)
if ( $a [ $i ] < $a [ $i + 1])
return false;
return true;
}
function shortestUnsorted( $a , $n )
{
if (increasing( $a , $n ) == true ||
decreasing( $a , $n ) == true)
return 0;
else
return 3;
}
$ar = array ( 7, 9, 10, 8, 11 );
$n = sizeof( $ar );
echo shortestUnsorted( $ar , $n );
?>
|
Javascript
<script>
function increasing(a, n)
{
for (let i = 0; i < n - 1; i++)
if (a[i] >= a[i + 1])
return false ;
return true ;
}
function decreasing(arr, n)
{
for (let i = 0; i < n - 1; i++)
if (arr[i] < arr[i + 1])
return false ;
return true ;
}
function shortestUnsorted(a, n)
{
if (increasing(a, n) == true ||
decreasing(a, n) == true )
return 0;
else
return 3;
}
let ar = [7, 9, 10, 8, 11];
let n = ar.length;
document.write(shortestUnsorted(ar,n));
</script>
|
Time complexity: O(n) where n is the length of the array.
Auxiliary Space: O(1)
Last Updated :
04 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...