Convert one array to another using adjacent swaps of elements
Last Updated :
23 Aug, 2021
Given two arrays arr1[] and arr2[] of N integers. We can choose any two adjacent elements from array arr1[] and swap them if they are of opposite parity, the task is to check if it is possible to convert array arr1[] to array arr2[] by performing the given operation on arr1[]. Print “Yes” if it is possible to convert array arr1[] to arr2[] Else print “No”.
Examples:
Input: arr1[] = {5, 7, 8, 2, 10, 13}, arr2[] = {8, 5, 2, 7, 13, 10}
Output: Yes
Explanation:
At first, swap 10 and 13 so arr1[] = [5, 7, 8, 2, 13, 10].
Now, swap 7 and 8 so arr1[] = [5, 8, 7, 2, 13, 10].
Now, swap 5 and 8 so arr1[] = [8, 5, 7, 2, 13, 10].
Now, swap 7 and 2 so arr1[] = [8, 5, 2, 7, 13, 10] = arr2[].
In each operation, we swap adjacent elements with different parity.
Input: arr1[] = {0, 1, 13, 3, 4, 14, 6}, arr2[] = {0, 1, 14, 3, 4, 13, 6}
Output: No
Explanation:
It is not possible to swap 13, 14 because they are not adjacent.
Approach: The problem can be solved using Greedy Approach. Since we cannot swap any two even or odd numbers. So the relative position of both even and odd numbers in the arrays arr1[] and arr2[] must be exactly the same to make both the arrays equal with the given operation. Below are the steps:
- Create two arrays(say even[] and odd[]) insert all the even and odd numbers from arr1[] in even[] and odd[] respectively.
- Now check whether the even and odd numbers in arr2[] are in the same order as in even[] and odd[].
- If the above steps doesn’t gives any number from arr2[] which are not in the order of numbers in even[] and odd[] arrays respectively then, print “Yes” else print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void convert( int a[], int b[], int n)
{
vector< int > even, odd;
for ( int x = 0; x < n; x++) {
if (a[x] % 2 == 0)
even.push_back(a[x]);
else
odd.push_back(a[x]);
}
int ei = 0, oi = 0;
bool poss = true ;
for ( int x = 0; x < n; x++) {
if (b[x] % 2 == 0) {
if (ei < even.size()
&& b[x] == even[ei]) {
ei++;
}
else {
poss = false ;
break ;
}
}
else {
if (oi < odd.size()
&& b[x] == odd[oi]) {
oi++;
}
else {
poss = false ;
break ;
}
}
}
if (poss)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
int arr1[] = { 0, 1, 13, 3, 4, 14, 6 };
int arr2[] = { 0, 1, 14, 3, 4, 13, 6 };
int N = sizeof (arr1) / sizeof (arr1[0]);
convert(arr1, arr2, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void convert( int a[], int b[], int n)
{
Vector<Integer> even = new Vector<Integer>(),
odd = new Vector<Integer>();
for ( int x = 0 ; x < n; x++)
{
if (a[x] % 2 == 0 )
even.add(a[x]);
else
odd.add(a[x]);
}
int ei = 0 , oi = 0 ;
boolean poss = true ;
for ( int x = 0 ; x < n; x++)
{
if (b[x] % 2 == 0 )
{
if (ei < even.size() &&
b[x] == even.get(ei))
{
ei++;
}
else
{
poss = false ;
break ;
}
}
else
{
if (oi < odd.size() &&
b[x] == odd.get(oi))
{
oi++;
}
else
{
poss = false ;
break ;
}
}
}
if (poss)
System.out.print( "Yes" + "\n" );
else
System.out.print( "No" + "\n" );
}
public static void main(String[] args)
{
int arr1[] = { 0 , 1 , 13 , 3 , 4 , 14 , 6 };
int arr2[] = { 0 , 1 , 14 , 3 , 4 , 13 , 6 };
int N = arr1.length;
convert(arr1, arr2, N);
}
}
|
Python3
def convert(a, b, n):
even = []
odd = []
for x in range (n):
if (a[x] % 2 = = 0 ):
even.append(a[x])
else :
odd.append(a[x])
ei, oi = 0 , 0
poss = True
for x in range (n):
if (b[x] % 2 = = 0 ):
if (ei < len (even) and
b[x] = = even[ei]):
ei + = 1
else :
poss = False
break
else :
if (oi < len (odd) and
b[x] = = odd[oi]):
oi + = 1
else :
poss = False
break
if (poss):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
arr1 = [ 0 , 1 , 13 , 3 , 4 , 14 , 6 ]
arr2 = [ 0 , 1 , 14 , 3 , 4 , 13 , 6 ]
N = len (arr1)
convert(arr1, arr2, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void convert( int []a, int []b, int n)
{
List< int > even = new List< int >(),
odd = new List< int >();
for ( int x = 0; x < n; x++)
{
if (a[x] % 2 == 0)
even.Add(a[x]);
else
odd.Add(a[x]);
}
int ei = 0, oi = 0;
bool poss = true ;
for ( int x = 0; x < n; x++)
{
if (b[x] % 2 == 0)
{
if (ei < even.Count &&
b[x] == even[ei])
{
ei++;
}
else
{
poss = false ;
break ;
}
}
else
{
if (oi < odd.Count &&
b[x] == odd[oi])
{
oi++;
}
else
{
poss = false ;
break ;
}
}
}
if (poss)
Console.Write( "Yes" + "\n" );
else
Console.Write( "No" + "\n" );
}
public static void Main(String[] args)
{
int []arr1 = { 0, 1, 13, 3, 4, 14, 6 };
int []arr2 = { 0, 1, 14, 3, 4, 13, 6 };
int N = arr1.Length;
convert(arr1, arr2, N);
}
}
|
Javascript
<script>
function convert(a, b, n)
{
var even = [], odd = [];
for ( var x = 0; x < n; x++) {
if (a[x] % 2 == 0)
even.push(a[x]);
else
odd.push(a[x]);
}
var ei = 0, oi = 0;
var poss = true ;
for ( var x = 0; x < n; x++) {
if (b[x] % 2 == 0) {
if (ei < even.length
&& b[x] == even[ei]) {
ei++;
}
else {
poss = false ;
break ;
}
}
else {
if (oi < odd.length
&& b[x] == odd[oi]) {
oi++;
}
else {
poss = false ;
break ;
}
}
}
if (poss)
document.write( "Yes" );
else
document.write( "No" );
}
var arr1 = [0, 1, 13, 3, 4, 14, 6 ];
var arr2 = [0, 1, 14, 3, 4, 13, 6 ];
var N = arr1.length;
convert(arr1, arr2, N);
</script>
|
Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(N), where N is the number of elements in the array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...