📜  在中间反转的排序数组

📅  最后修改于: 2021-10-26 06:56:21             🧑  作者: Mango

考虑给定的数组 arr[],我们需要找到是否可以使用给定的操作对数组进行排序。操作是
1. 我们必须从给定的数组中选择一个子数组,使得中间元素(或元素(在偶数的情况下)
元素数))的子数组也是中间元素(或元素(在偶数元素的情况下))
给定的数组。
2. 然后我们要反转选中的子阵列,并将这个反转的子阵列放入阵列中。
我们可以根据需要多次执行上述操作。任务是找到我们是否可以使用给定的操作对数组进行排序。
例子:

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

一种解决方案是我们可以围绕中心旋转每个元素,这给出了数组中的两种可能性,即索引“i”处的值或索引“length – 1 – i”处的值。
如果数组有 n 个元素,则可能有 2^n 个组合,因此运行时间将为 O(2^n)。
另一种解决方案是复制数组并对复制的数组进行排序。然后将排序后的数组的每个元素与原始数组的等效元素及其绕中心旋转时的镜像进行比较。对数组进行排序需要 O(n*logn) 并且需要 2n 次比较,因此运行时间将为 O(n*logn)。

C++
// CPP program to find possibility to sort
// by multiple subarray reverse operarion
#include 
using namespace std;
 
bool ifPossible(int arr[], int n)
{
    int cp[n];
 
    // making the copy of the original array
    copy(arr, arr + n, cp);
 
    // sorting the copied array
    sort(cp, cp + n);
 
    for (int i = 0; i < n; i++) {
 
        // checking mirror image of elements of sorted
        // copy array and equivalent element of original
        // array
        if (!(arr[i] == cp[i]) && !(arr[n - 1 - i] == cp[i]))
            return false;
    }
 
    return true;
}
 
// driver code
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
// Java program to find possibility to sort
// by multiple subarray reverse operation
import java.util.*;
class GFG {
 
    static boolean ifPossible(int arr[], int n)
    {
 
        // making the copy of the original array
        int copy[] = Arrays.copyOf(arr, arr.length);
 
        // sorting the copied array
        Arrays.sort(copy);
 
        for (int i = 0; i < n; i++) {
 
            // checking mirror image of elements of
            // sorted copy array and equivalent element
            // of original array
            if (!(arr[i] == copy[i]) && !(arr[n - 1 - i] == copy[i]))
                return false;
        }
 
        return true;
    }
 
    // driver code
    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
# Python 3 program to find
# possibility to sort by
# multiple subarray reverse
# operarion
 
def ifPossible(arr, n):
 
    cp = [0] * n
 
    # making the copy of
    # the original array
    cp = arr
 
    # sorting the copied array
    cp.sort()
 
    for i in range(0 , n) :
  
        # checking mirror image of
        # elements of sorted copy
        # array and equivalent element
        # of original array
        if (not(arr[i] == cp[i]) and not
               (arr[n - 1 - i] == cp[i])):
            return False
 
    return True
 
# Driver code
arr = [1, 7, 6, 4, 5, 3, 2, 8]
n = len(arr)
if (ifPossible(arr, n)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Smitha


C#
// C# Program to answer queries on sum
// of sum of odd number digits of all
// the factors of a number
using System;
 
class GFG {
 
    static bool ifPossible(int []arr, int n)
    {
        int []cp = new int[n];
     
        // making the copy of the original
        // array
        Array.Copy(arr, cp, n);
     
        // sorting the copied array
        Array.Sort(cp);
     
        for (int i = 0; i < n; i++) {
     
            // checking mirror image of
            // elements of sorted copy
            // array and equivalent element
            // of original array
            if (!(arr[i] == cp[i]) &&
                 !(arr[n - 1 - i] == cp[i]))
                return false;
        }
     
        return true;
    }
     
    // Driver code
    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");
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出:
Yes

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程