Given two sorted arrays A[] and B[] consisting of N distinct integers, the task is to rearrange the elements of array B[] such that, for every ith index, A[i] is not equal to B[i]. If multiple such arrangements exist, print any one of them. If no such arrangement exists, print -1.
Examples:
Input: A[] = {2, 4, 5, 8}, B[] = {2, 4, 5, 8}
Output: 4 2 8 5
Explanation:
Possible arrangements that satisfy the required conditions are {4, 2, 8, 5}, {8, 5, 4, 2} and {8, 5, 4, 2}.
Input: A[] = {1, 3, 4, 5}, B[] = {2, 4, 6, 7}
Output: 7 6 2 4
Naive approach: The simplest approach is to find all possible permutations of array B[] and print any permutation among them such that, for every ith index, A[i]) is not equal to B[i].
Time Complexity: O(N*N!)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use a Greedy Approach to find the required arrangement of array B[] by using the condition that both the arrays consist of N distinct elements in ascending order. Follow the steps below to solve the problem:
- Reverse the given array B[].
- If N is 1 and A[0] = B[0], then print -1.
- Otherwise, iterate over the arrays, and check if A[i] equals to B[i] or not.
- If A[i] equals to B[i], swap B[i] with B[i+1] and break the loop.
- After the above steps, print the array B[].
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void RearrangeB( int A[], vector< int > B, int n)
{
if (n == 1 && A[0] == B[0])
{
cout << "-1" << endl;
return ;
}
for ( int i = 0; i < n / 2; i++)
{
int t = B[i];
B[i] = B[n - i - 1];
B[n - i - 1] = t;
}
for ( int i = 0; i < n - 1; i++)
{
if (B[i] == A[i])
{
int t = B[i + 1];
B[i + 1] = B[i];
B[i] = t;
break ;
}
}
for ( int k : B)
cout << k << " " ;
}
int main()
{
int A[] = { 2, 4, 5, 8 };
vector< int > B = { 2, 4, 5, 8 };
int n = sizeof (A) / sizeof (A[0]);
RearrangeB(A, B, n);
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static void RearrangeB( int [] A, int [] B)
{
int n = A.length;
if (n == 1 && A[ 0 ] == B[ 0 ]) {
System.out.println( "-1" );
return ;
}
for ( int i = 0 ; i < n / 2 ; i++) {
int t = B[i];
B[i] = B[n - i - 1 ];
B[n - i - 1 ] = t;
}
for ( int i = 0 ; i < n - 1 ; i++) {
if (B[i] == A[i]) {
int t = B[i + 1 ];
B[i + 1 ] = B[i];
B[i] = t;
break ;
}
}
for ( int k : B)
System.out.print(k + " " );
}
public static void main(String[] args)
{
int [] A = { 2 , 4 , 5 , 8 };
int [] B = { 2 , 4 , 5 , 8 };
RearrangeB(A, B);
}
}
|
Python3
def RearrangeB(A, B):
n = len (A)
if (n = = 1 and A[ 0 ] = = B[ 0 ]):
print ( - 1 )
return
for i in range (n / / 2 ):
t = B[i]
B[i] = B[n - i - 1 ]
B[n - i - 1 ] = t
for i in range (n - 1 ):
if (B[i] = = A[i]):
B[i], B[i - 1 ] = B[i - 1 ], B[i]
break
for k in B:
print (k, end = " " )
A = [ 2 , 4 , 5 , 8 ]
B = [ 2 , 4 , 5 , 8 ]
RearrangeB(A, B)
|
C#
using System;
class GFG{
static void RearrangeB( int [] A,
int [] B)
{
int n = A.Length;
if (n == 1 && A[0] == B[0])
{
Console.WriteLine( "-1" );
return ;
}
for ( int i = 0; i < n / 2; i++)
{
int t = B[i];
B[i] = B[n - i - 1];
B[n - i - 1] = t;
}
for ( int i = 0; i < n - 1; i++)
{
if (B[i] == A[i])
{
int t = B[i + 1];
B[i + 1] = B[i];
B[i] = t;
break ;
}
}
foreach ( int k in B)
Console.Write(k + " " );
}
public static void Main(String[] args)
{
int [] A = {2, 4, 5, 8};
int [] B = {2, 4, 5, 8};
RearrangeB(A, B);
}
}
|
Javascript
<script>
function RearrangeB( A, B)
{
let n = A.length;
if (n == 1 && A[0] == B[0]) {
document.write( "-1" );
return ;
}
for (let i = 0; i < n / 2; i++) {
let t = B[i];
B[i] = B[n - i - 1];
B[n - i - 1] = t;
}
for (let i = 0; i < n - 1; i++) {
if (B[i] == A[i]) {
let t = B[i + 1];
B[i + 1] = B[i];
B[i] = t;
break ;
}
}
for (let k in B)
document.write(B[k] + " " );
}
let A = [ 2, 4, 5, 8 ];
let B = [ 2, 4, 5, 8 ];
RearrangeB(A, B);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)