Check if two arrays can be made equal by reversing any subarray once
Last Updated :
17 Aug, 2021
Given two arrays A[] and B[] of equal size N, the task is to check whether A[] can be made equal to B[] by reversing any sub-array of A only once.
Examples:
Input: A[] = {1, 3, 2, 4}
B[] = {1, 2, 3, 4}
Output: Yes
Explanation:
The sub-array {3, 2} can be reversed to {2, 3}, which makes A equal to B.
Input: A[] = {1, 4, 2, 3}
B[] = {1, 2, 3, 4}
Output: No
Explanation:
There is no sub-array of A which, when reversed, makes A equal to B.
Naive Approach: Check for all sub-arrays of A[] and compare the two arrays after reversing the sub-array.
Time complexity: O(N2).
Efficient Approach:
- First, find the starting and the ending index of the sub-array not equal in A and B.
- Then, by reversing the required sub-array, we can check whether A can be made equal to B or not.
- The starting index is the first index in the arrays for which A[i] != B[i] and the ending index is the last index in arrays for which A[i] != B[i].
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void checkArray( int A[], int B[], int N)
{
int start = 0;
int end = N - 1;
for ( int i = 0; i < N; i++) {
if (A[i] != B[i]) {
start = i;
break ;
}
}
for ( int i = N - 1; i >= 0; i--) {
if (A[i] != B[i]) {
end = i;
break ;
}
}
reverse(A + start, A + end + 1);
for ( int i = 0; i < N; i++) {
if (A[i] != B[i]) {
cout << "No" << endl;
return ;
}
}
cout << "Yes" << endl;
}
int main()
{
int A[] = { 1, 3, 2, 4 };
int B[] = { 1, 2, 3, 4 };
int N = sizeof (A) / sizeof (A[0]);
checkArray(A, B, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void checkArray( int A[], int B[], int N)
{
int start = 0 ;
int end = N - 1 ;
for ( int i = 0 ; i < N; i++)
{
if (A[i] != B[i])
{
start = i;
break ;
}
}
for ( int i = N - 1 ; i >= 0 ; i--)
{
if (A[i] != B[i])
{
end = i;
break ;
}
}
Collections.reverse(Arrays.asList(A));
for ( int i = 0 ; i < N; i++)
{
if (A[i] != B[i])
{
System.out.println( "No" );
return ;
}
}
System.out.println( "Yes" );
}
public static void main(String[] args)
{
int A[] = { 1 , 3 , 2 , 4 };
int B[] = { 1 , 2 , 3 , 4 };
int N = A.length;
checkArray(A, B, N);
}
}
|
Python3
def checkArray(A, B, N):
start = 0
end = N - 1
for i in range (N):
if (A[i] ! = B[i]):
start = i
break
for i in range (N - 1 , - 1 , - 1 ):
if (A[i] ! = B[i]):
end = i
break
A[start:end + 1 ] = reversed (A[start:end + 1 ])
for i in range (N):
if (A[i] ! = B[i]):
print ( "No" )
return
print ( "Yes" )
if __name__ = = '__main__' :
A = [ 1 , 3 , 2 , 4 ]
B = [ 1 , 2 , 3 , 4 ]
N = len (A)
checkArray(A, B, N)
|
C#
using System;
class GFG{
static void checkArray( int []A, int []B, int N)
{
int start = 0;
int end = N - 1;
for ( int i = 0; i < N; i++)
{
if (A[i] != B[i])
{
start = i;
break ;
}
}
for ( int i = N - 1; i >= 0; i--)
{
if (A[i] != B[i])
{
end = i;
break ;
}
}
Array.Reverse(A, start, end);
for ( int i = 0; i < N; i++)
{
if (A[i] != B[i])
{
Console.Write( "No" );
return ;
}
}
Console.Write( "Yes" );
}
public static void Main( string [] args)
{
int []A = { 1, 3, 2, 4 };
int []B = { 1, 2, 3, 4 };
int N = A.Length;
checkArray(A, B, N);
}
}
|
Javascript
<script>
function checkArray(A, B, N)
{
var start = 0;
var end = N - 1;
for ( var i = 0; i < N; i++) {
if (A[i] != B[i]) {
start = i;
break ;
}
}
for ( var i = N - 1; i >= 0; i--) {
if (A[i] != B[i]) {
end = i;
break ;
}
}
var l = start;
var r = end;
var d = parseInt((r-l+2)/2);
for ( var i=0;i<d;i++)
{
var t = A[l+i];
A[l+i] = A[r-i];
A[r-i] = t;
}
for ( var i = 0; i < N; i++) {
if (A[i] != B[i]) {
document.write( "No" );
return ;
}
}
document.write( "Yes" );
}
var A = [1, 3, 2, 4];
var B = [1, 2, 3, 4];
var N = A.length;
checkArray(A, B, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...