![]() |
VOOZH | about |
Given an array A positive integers, sort the array in ascending order such that element in given subarray (start and end indexes are input) in unsorted array stay unmoved and all other elements are sorted.
Examples :
Input : arr[] = {10, 4, 11, 7, 6, 20}
l = 1, u = 3
Output : arr[] = {6, 4, 11, 7, 10, 20}
We sort elements except arr[1..3] which
is {11, 7, 6}.
Input : arr[] = {5, 4, 3, 12, 14, 9};
l = 1, u = 2;
Output : arr[] = {5, 4, 3, 9, 12, 14 }
We sort elements except arr[1..2] which
is {4, 3}.
Approach : Copy all elements except the given limit of given array to another array. Then sort the other array using a sorting algorithm. Finally again copy the sorted array to original array. While copying, skip given subarray.
4 5 3 12 14 9
Time Complexity: O(n*log(n))
Auxiliary Space: O(n)