![]() |
VOOZH | about |
Consider the given array arr[], we need to find if we can sort array with the given operation. The operation is
Examples:
Input : arr[] = {1, 6, 3, 4, 5, 2, 7}
Output : Yes
We can choose sub-array[3, 4, 5] on
reversing this we get [1, 6, 5, 4, 3, 2, 7]
again on selecting [6, 5, 4, 3, 2] and
reversing this one we get [1, 2, 3, 4, 5, 6, 7]
which is sorted at last thus it is possible
to sort on multiple reverse operation.
Input : arr[] = {1, 6, 3, 4, 5, 7, 2}
Output : No
One solution is we can rotate each element around the center, which gives two possibilities in the array i.e. the value at index 'i' or the value at index "length - 1 - i".
If array has n elements then 2^n combinations possible thus running time would be O(2^n).
Another solution can be make copy of the array and sort the copied array. Then compare each element of the sorted array with equivalent element of original array and its mirror image when pivot around center. Sorting the array takes O(n*logn) and 2n comparisons be required thus running time would be O(n*logn).
Implementation:
Yes
Time Complexity: O(n log n), where n is the size of the input array. This is because of the sorting operation performed on the copied array.
Auxiliary Space: O(n), where n is the size of the input array. This is because of the copy of the original array created.