Check if two arrays can be made equal by reversing subarrays multiple times

22 Apr, 2021
Given two arrays A[] and B[], the task is to check if array B can be made equal to A by reversing the subarrays of B by any number of times.
Examples: 
Input:  A[] =  {1 2 3}, B[] =  {3 1 2}Output: YesExplanation: Reverse subarrays in array B as shown below:Reverse subarray [3, 1], B becomes [1, 3, 2]Reverse subarray [3, 2], B becomes [1, 2, 3] = AThere are multiple ways to convert B to A.Input:  A[] = {1 2 3}, B[] = {3 4 1 }Output: No 
Approach: Since we have to make array B equal to array A only by reversing any sub array any number of times, Therefore it is possible only when both the arrays are anagrams. To check if both arrays are anagrams, we will sort both the arrays and check if at any index elements are not same then we will return false, otherwise we will return true at the end.
Below implementation of the above approach:
C++
C
Java
Python3
C#
Javascript
// C++ implementation to check if// two arrays can be made equal #include <bits/stdc++.h>using namespace std; // Function to check if array B// can be made equal to array Abool 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 Codeint 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 implementation to check if // two arrays can be made equal#include<stdio.h>#include<math.h> 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] <a[i])            {                tmp = a[i];                a[i] = a[j];                a[j] = tmp;            }        }    }    return 0;} // Function to check if array B// can be made equal to array Aint canMadeEqual(int A[], int B[], int n){    int i;         // Sort both arrays    sort(A, n);    sort(B, n);         // Check both arrays equal or not    for(i = 0; i < n; i++)    {        if (A[i] != B[i])        {            return(0);        }    }    return (1);} // Driver Codeint main(){    int A[] = { 1, 2, 3 };    int n;    int B[] = { 1, 3, 2 };         n = sizeof(A) / sizeof(A[0]);    if(canMadeEqual(A, B, n))    {        printf("Yes");    }    else    {        printf("No");    }    return 0;} // This code is contributed by adityakumar27200
// Java implementation to check if// two arrays can be made equalimport java.util.*; class GFG{     // Function to check if array B// can be made equal to array Apublic 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 codepublic 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 implementation to check if# two arrays can be made equal # Function to check if array B# can be made equal to array Adef 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 Codeif __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# implementation to check if// two arrays can be made equalusing System; class GFG{ // Function to check if array B// can be made equal to array Astatic 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 codepublic 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
<script>  // Javascript implementation to check if// two arrays can be made equal // Function to check if array B// can be made equal to array Afunction canMadeEqual(A, B, n){    // sort both the arrays    A.sort();    B.sort();     // Check if both the arrays    // are equal or not    for (var i = 0; i < n; i++)        if (A[i] != B[i])            return false;    return true;} // Driver Codevar A = [ 1, 2, 3 ];var B = [ 1, 3, 2 ];var n = A.length;if (canMadeEqual(A, B, n))    document.write( "Yes");else    document.write("No"); // This code is contributed by rutvik_56.</script>
Yes
Time complexity: O(N*Log N) Auxiliary Space: O(1)
ukasp
divyeshrabadiya07
adityakumar27200
rutvik_56
anagram
Reverse
subarray
Arrays
Sorting
Arrays
Sorting
Reverse
anagram
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Bubble Sort Algorithm
Insertion Sort
Selection Sort Algorithm
std::sort() in C++ STL
Time Complexities of all Sorting Algorithms