📌  相关文章
📜  通过多次反转子数组来检查两个数组是否相等

📅  最后修改于: 2021-05-17 22:33:30             🧑  作者: Mango

给定两个数组A []B [],任务是通过将B的子数组反转任意次来检查数组B是否可以等于A。

例子:

方法:由于只需要通过反转任意次数组数次就可以使数组B与数组A相等,因此只有当两个数组都为字谜时才有可能。为了检查两个数组是否都是字谜,我们将对两个数组进行排序,并检查在任何索引元素上是否不相同,然后我们将返回false,否则最后将返回true。

下面实现以上方法:

C++
// C++ implementation to check if
// two arrays can be made equal
 
#include 
using namespace std;
 
// Function to check if array B
// can be made equal to array A
bool canMadeEqual(int A[],
                  int B[], int n)
{
    // sort both the arrays
    sort(A, A + n);
    sort(B, B + n);
 
    // Check if both the arrays
    // are equal or not
    for (int i = 0; i < n; i++)
        if (A[i] != B[i])
            return false;
    return true;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3 };
    int B[] = { 1, 3, 2 };
    int n = sizeof(A) / sizeof(A[0]);
    if (canMadeEqual(A, B, n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


C
// C implementation to check if 
// two arrays can be made equal
#include
#include
 
int sort(int a[],int n)
{
    int i, j, tmp;
    for(i = 0; i < n; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            if(a[j] 


Java
// Java implementation to check if
// two arrays can be made equal
import java.util.*;
 
class GFG{
     
// Function to check if array B
// can be made equal to array A
public static boolean canMadeEqual(int[] A,
                                   int[] B,
                                   int n)
{
     
    // Sort both the arrays
    Arrays.sort(A);
    Arrays.sort(B);
 
    // Check if both the arrays
    // are equal or not
    for(int i = 0; i < n; i++)
    {
        if (A[i] != B[i])
        {
            return false;
        }
    }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int A[] = { 1, 2, 3 };
    int B[] = { 1, 3, 2 };
    int n = A.length;
     
    if (canMadeEqual(A, B, n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation to check if
# two arrays can be made equal
 
# Function to check if array B
# can be made equal to array A
def canMadeEqual(A, B, n):
   
  # Sort both the arrays
  A.sort()
  B.sort()
 
  # Check if both the arrays
  # are equal or not
  for i in range(n):
    if (A[i] != B[i]):
      return False
     
  return True
 
# Driver Code
if __name__ == "__main__":
   
  A = [ 1, 2, 3 ]
  B = [ 1, 3, 2 ]
  n = len(A)
 
  if (canMadeEqual(A, B, n)):
    print( "Yes")
  else:
    print("No")
 
# This code is contributed by chitranayal


C#
// C# implementation to check if
// two arrays can be made equal
using System;
 
class GFG{
 
// Function to check if array B
// can be made equal to array A
static bool canMadeEqual(int[] A,
                         int[] B,
                         int n)
{
     
    // Sort both the arrays
    Array.Sort(A);
    Array.Sort(B);
 
    // Check if both the arrays
    // are equal or not
    for(int i = 0; i < n; i++)
    {
        if (A[i] != B[i])
        {
            return false;
        }
    }
    return true;
}
 
// Driver code
public static void Main()
{
    int[] A = { 1, 2, 3 };
    int[] B = { 1, 3, 2 };
    int n = A.Length;
     
    if (canMadeEqual(A, B, n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by adityakumar27200


Javascript


输出
Yes

时间复杂度: O(N * Log N)
辅助空间: O(1)