Sub Sort

You are given an integer array ‘ARR’. You have to find the length of the shortest contiguous subarray such that, if you sort this subarray in ascending order, then the whole array will be sorted in ascending order.

An array 'C' is a subarray of array 'D' if it can be obtained by deletion of several elements(possibly zero) from the beginning and the end from array 'D'.

Example:

Let’s say we have an array 'ARR' {10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60}. Then we have to find the subarray {30 , 25 , 40 , 32 , 31 , 35} and print its length = 5 as our output. Because, when we sort this subarray the whole array will be sorted.
Input format:
The very first line of input contains an integer ‘T’ denoting the number of test cases. 

The first line of every test case contains one integer ‘N’ denoting the number of elements present in the array.

The second line of every test case contains ‘N’ space-separated integers denoting the elements present in the array.
Output format:
For each test case, print the shortest length of the unsorted subarray. Output for each test case is printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just return the length of the shortest subarray.
Constraints:
1 <= T <= 10
1 <= N <= 5 * 10 ^ 4
-10^5 <= ARR[i] <= 10^5

Where  ‘T’ represents the number of test cases, ‘N’ represents the number of elements present in the array, and ‘ARR[i]’ represents the array element. 

Time Limit: 1sec
AnswerBot
1y

The question asks to find the length of the shortest contiguous subarray that needs to be sorted in order to sort the whole array.

  • Iterate through the array and find the first element that is out of or...read more

CodingNinjas
author
2y
Brute Force

In this approach, we consider every possible subarray that can be formed from the given array ‘ARR’. For every subarray ARR[i … j] considered, we need to check whether this is the smallest ...read more

CodingNinjas
author
2y
Selection Sort

This approach is based on the idea of selection sort. But instead of swapping as we do in the case of selection sort, we will determine the leftmost boundary of the unsorted array and th...read more

CodingNinjas
author
2y
Sorting

This approach is based on the idea of sorting. Here we will copy the given array in another array and then sort it and then compare this array with the given array. We will determine the leftmo...read more

CodingNinjas
author
2y
Sub Sort

This approach uses two passes of the array. In the first pass, Iterate from the end, and check whether the array is in descending order, i.e., ARR[i] > ARR[i-1] > ARR[i-2] > … . If not we will...read more

Add answer anonymously...
ZeMoSo Technologies Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter