Sorting array with reverse around middle
Last Updated :
09 Apr, 2023
Consider the given array arr[], we need to find if we can sort array with the given operation. The operation is
- We have to select a subarray from the given array such that the middle element(or elements (in case of even
number of elements)) of subarray is also the middle element(or elements (in case of even number of elements)) of
the given array.
- Then we have to reverse the selected subarray and place this reversed subarray in the array.
We can do the above operation as many times as we want. The task is to find if we can sort array with the given operation.
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:
C++
#include <bits/stdc++.h>
using namespace std;
bool ifPossible( int arr[], int n)
{
int cp[n];
copy(arr, arr + n, cp);
sort(cp, cp + n);
for ( int i = 0; i < n; i++) {
if (!(arr[i] == cp[i]) && !(arr[n - 1 - i] == cp[i]))
return false ;
}
return true ;
}
int main()
{
int arr[] = { 1, 7, 6, 4, 5, 3, 2, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
if (ifPossible(arr, n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean ifPossible( int arr[], int n)
{
int copy[] = Arrays.copyOf(arr, arr.length);
Arrays.sort(copy);
for ( int i = 0 ; i < n; i++) {
if (!(arr[i] == copy[i]) && !(arr[n - 1 - i] == copy[i]))
return false ;
}
return true ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 };
int n = arr.length;
if (ifPossible(arr, n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python 3
def ifPossible(arr, n):
cp = [ 0 ] * n
cp = arr
cp.sort()
for i in range ( 0 , n) :
if ( not (arr[i] = = cp[i]) and not
(arr[n - 1 - i] = = cp[i])):
return False
return True
arr = [ 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 ]
n = len (arr)
if (ifPossible(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool ifPossible( int []arr, int n)
{
int []cp = new int [n];
Array.Copy(arr, cp, n);
Array.Sort(cp);
for ( int i = 0; i < n; i++) {
if (!(arr[i] == cp[i]) &&
!(arr[n - 1 - i] == cp[i]))
return false ;
}
return true ;
}
public static void Main()
{
int []arr = new int []{ 1, 7, 6, 4,
5, 3, 2, 8 };
int n = arr.Length;
if (ifPossible(arr, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function ifPossible(& $arr , $n )
{
$cp = array ();
$cp = $arr ;
sort( $cp );
for ( $i = 0; $i < $n ; $i ++)
{
if (!( $arr [ $i ] == $cp [ $i ]) &&
!( $arr [ $n - 1 - $i ] == $cp [ $i ]))
return false;
}
return true;
}
$arr = array (1, 7, 6, 4, 5, 3, 2, 8);
$n = sizeof( $arr );
if (ifPossible( $arr , $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function ifPossible(arr, n)
{
let copy = arr;
copy.sort();
for (let i = 0; i < n; i++) {
if (!(arr[i] == copy[i]) && !(arr[n - 1 - i] == copy[i]))
return false ;
}
return true ;
}
let arr = [ 1, 7, 6, 4, 5, 3, 2, 8 ];
let n = arr.length;
if (ifPossible(arr, n))
document.write( "Yes" );
else
document.write( "No" );;
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...