#K52212. Smallest Window Sorting
Smallest Window Sorting
Smallest Window Sorting
You are given an array of n integers. Your task is to find the smallest continuous subarray (or window) such that, if only that subarray is sorted in ascending order, the entire array becomes sorted in ascending order.
If the array is already sorted, output should indicate that no subarray needs to be sorted by returning the indices as \(-1\) and \(-1\).
Formally, let the array be \(A = [a_0, a_1, \ldots, a_{n-1}]\). Find indices \(i\) and \(j\) (with \(0 \le i \le j < n\)) such that sorting the subarray \(A[i\ldots j]\) makes the whole array sorted. If the array is already sorted, output \(i = -1\) and \(j = -1\).
Additionally, you are required to output the fully sorted array.
Note: If a part of the array is out of order, extend the window if necessary so that every element to the left of the window is less than or equal to the minimum element in the window, and every element to the right is greater than or equal to the maximum element in the window.
inputFormat
The input is provided via standard input (stdin) and has the following format:
- The first line contains a single integer \(n\), representing the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the elements of the array.
\(1 \le n \le 10^5\) and each element is an integer that fits in a 32-bit signed integer.
outputFormat
Print the result to standard output (stdout) in the following format:
- On the first line, print two space-separated integers representing the starting and ending indices of the smallest window that must be sorted. If the array is already sorted, print "-1 -1".
- On the second line, print the fully sorted array as a sequence of space-separated integers.
5
1 2 3 4 5
-1 -1
1 2 3 4 5
</p>