给定N个整数的两个数组arr1 []和arr2 [] 。我们可以从数组arr1 []中选择任意两个相邻元素,如果它们具有相反的奇偶性,则将它们交换,任务是检查是否可以通过对arr1 [进行给定的操作来将数组arr1 []转换为数组arr2 [] 。 ] 。如果可以将数组arr1 []转换为arr2 [] ,则打印“是” ,否则打印“否” 。
例子:
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.
方法:可以使用贪婪方法解决问题。由于我们不能交换任何两个偶数或奇数。因此,偶数和奇数在数组arr1 []和arr2 []中的相对位置必须完全相同,以使两个数组与给定操作相等。步骤如下:
- 创建两个数组(例如even []和奇数[] ),将arr1 []中的所有偶数和奇数分别插入偶数[]和奇数[] 。
- 现在检查arr2 []中的偶数和奇数是否与even []和奇数[]中的顺序相同。
- 如果以上步骤没有从arr2 []中给出任何数字,它们分别不在even []和奇数[]数组中以数字的顺序排列,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function which checks if it is
// possible to convert arr1[] to
// arr2[] by given operations
void convert(int a[], int b[], int n)
{
// even[] will store the even
// elements of a[]
// odd[] will store the odd
// elements of a[]
vector even, odd;
// Traverse a[] and insert the even
// and odd element respectively
for (int x = 0; x < n; x++) {
if (a[x] % 2 == 0)
even.push_back(a[x]);
else
odd.push_back(a[x]);
}
// ei points to the next
// available even element
// oi points to the next
// avaialble odd element
int ei = 0, oi = 0;
// poss will store whether the
// given transformation
// of a[] to b[] is possible
bool poss = true;
// Traverse b[]
for (int x = 0; x < n; x++) {
if (b[x] % 2 == 0) {
// Check if both even
// elements are equal
if (ei < even.size()
&& b[x] == even[ei]) {
ei++;
}
else {
poss = false;
break;
}
}
else {
// Check if both odd
// elements are equal
if (oi < odd.size()
&& b[x] == odd[oi]) {
oi++;
}
else {
poss = false;
break;
}
}
}
// If poss is true, then we can
// transform a[] to b[]
if (poss)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
// Driver Code
int main()
{
// Given arrays
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]);
// Function Call
convert(arr1, arr2, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function which checks if it is
// possible to convert arr1[] to
// arr2[] by given operations
static void convert(int a[], int b[], int n)
{
// even[] will store the even
// elements of a[]
// odd[] will store the odd
// elements of a[]
Vector even = new Vector(),
odd = new Vector();
// Traverse a[] and insert the even
// and odd element respectively
for(int x = 0; x < n; x++)
{
if (a[x] % 2 == 0)
even.add(a[x]);
else
odd.add(a[x]);
}
// ei points to the next
// available even element
// oi points to the next
// avaialble odd element
int ei = 0, oi = 0;
// poss will store whether the
// given transformation
// of a[] to b[] is possible
boolean poss = true;
// Traverse b[]
for(int x = 0; x < n; x++)
{
if (b[x] % 2 == 0)
{
// Check if both even
// elements are equal
if (ei < even.size() &&
b[x] == even.get(ei))
{
ei++;
}
else
{
poss = false;
break;
}
}
else
{
// Check if both odd
// elements are equal
if (oi < odd.size() &&
b[x] == odd.get(oi))
{
oi++;
}
else
{
poss = false;
break;
}
}
}
// If poss is true, then we can
// transform a[] to b[]
if (poss)
System.out.print("Yes" + "\n");
else
System.out.print("No" + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given arrays
int arr1[] = { 0, 1, 13, 3, 4, 14, 6 };
int arr2[] = { 0, 1, 14, 3, 4, 13, 6 };
int N = arr1.length;
// Function Call
convert(arr1, arr2, N);
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program for the above approach
# Function which checks if it is
# possible to convert arr1[] to
# arr2[] by given operations
def convert(a, b, n):
# even[] will store the even
# elements of a[]
# odd[] will store the odd
# elements of a[]
even = []
odd = []
# Traverse a[] and insert the even
# and odd element respectively
for x in range(n):
if (a[x] % 2 == 0):
even.append(a[x])
else:
odd.append(a[x])
# ei points to the next
# available even element
# oi points to the next
# avaialble odd element
ei, oi = 0, 0
# poss will store whether the
# given transformation
# of a[] to b[] is possible
poss = True
# Traverse b[]
for x in range(n):
if (b[x] % 2 == 0):
# Check if both even
# elements are equal
if (ei < len(even) and
b[x] == even[ei]):
ei += 1
else:
poss = False
break
else:
# Check if both odd
# elements are equal
if (oi < len(odd) and
b[x] == odd[oi]):
oi += 1
else:
poss = False
break
# If poss is true, then we can
# transform a[] to b[]
if (poss):
print("Yes")
else:
print("No")
# Driver Code
if __name__ == "__main__":
# Given arrays
arr1 = [ 0, 1, 13, 3, 4, 14, 6 ]
arr2 = [ 0, 1, 14, 3, 4, 13, 6 ]
N = len(arr1)
# Function call
convert(arr1, arr2, N)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function which checks if it is
// possible to convert arr1[] to
// arr2[] by given operations
static void convert(int []a, int []b, int n)
{
// even[] will store the even
// elements of []a
// odd[] will store the odd
// elements of []a
List even = new List(),
odd = new List();
// Traverse []a and insert the even
// and odd element respectively
for(int x = 0; x < n; x++)
{
if (a[x] % 2 == 0)
even.Add(a[x]);
else
odd.Add(a[x]);
}
// ei points to the next
// available even element
// oi points to the next
// avaialble odd element
int ei = 0, oi = 0;
// poss will store whether the
// given transformation
// of []a to []b is possible
bool poss = true;
// Traverse []b
for(int x = 0; x < n; x++)
{
if (b[x] % 2 == 0)
{
// Check if both even
// elements are equal
if (ei < even.Count &&
b[x] == even[ei])
{
ei++;
}
else
{
poss = false;
break;
}
}
else
{
// Check if both odd
// elements are equal
if (oi < odd.Count &&
b[x] == odd[oi])
{
oi++;
}
else
{
poss = false;
break;
}
}
}
// If poss is true, then we can
// transform []a to []b
if (poss)
Console.Write("Yes" + "\n");
else
Console.Write("No" + "\n");
}
// Driver Code
public static void Main(String[] args)
{
// Given arrays
int []arr1 = { 0, 1, 13, 3, 4, 14, 6 };
int []arr2 = { 0, 1, 14, 3, 4, 13, 6 };
int N = arr1.Length;
// Function call
convert(arr1, arr2, N);
}
}
// This code is contributed by gauravrajput1
No
时间复杂度: O(N) ,其中N是数组中元素的数量。
辅助空间: O(N) ,其中N是数组中元素的数量。