Sub Sort Problem Statement

You are given an integer array ARR. Determine the length of the shortest contiguous subarray which, when sorted in ascending order, results in the entire array being sorted in ascending order.

Definition:

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

Example:

Input:
ARR = [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60]
Output:
6
Explanation:

The subarray [30, 25, 40, 32, 31, 35] needs to be sorted. When sorted, the entire array will be in ascending order. The length of this subarray is 6.

Input:

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 in the array.
The second line of every test case contains ‘N’ space-separated integers denoting the elements present in the array.

Output:

For each test case, print the shortest length of the unsorted subarray. Output each length on a separate line.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 5 * 10^4
  • -10^5 ≤ ARR[i] ≤ 10^5
  • Time Limit: 1 sec

Note:

You do not need to print anything; it has already been taken care of. Just return the length of the shortest subarray.

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

Help your peers!
Select
Add answer anonymously...

ZeMoSo Technologies Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked 8mo agoQ. Write a program to check whether the parentheses are properly balanced, meaning ...read more
An Associate Software Engineer was asked 8mo agoQ. Given a Map of fruits with their price as a key-value pair respectively, find th...read more
An Associate Software Engineer was asked 8mo agoQ. Given a list of integers, find the first non-repeating integer using the Stream ...read more

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked 9mo agoQ1. Write a program to check whether the parentheses are properly balanced, meaning ...read more
An Associate Software Engineer was asked 9mo agoQ2. Given a Map of fruits with their price as a key-value pair respectively, find th...read more
An Associate Software Engineer was asked 9mo agoQ3. Given a list of integers, find the first non-repeating integer using the Stream ...read more
ZeMoSo Technologies Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits