检查数组是否可以通过交换相反奇偶校验的相邻元素来排序
给定一个大小为n的数组A ,任务是检查数组是否可以按升序排序,如果唯一允许的操作是交换相邻元素(如果它们具有相反的奇偶性)。该操作可以进行任意次数。
例子:
Input : n = 4, A = [1, 6, 51, 16]
Output: YES
Explanation: Since 51 is odd and 16 is even, we will just swap them. The array now becomes [1, 6, 16, 51], which is sorted in increasing order.
Input: n = 4, A = [5, 5, 5, 5]
Output: YES
Explanation: The array is already sorted.
方法:可以观察到,如果偶数元素的顺序或奇数元素的顺序是递减的,那么就不可能对数组进行排序。
我们可以假设我们将使用冒泡排序对数组进行排序(就像在冒泡排序中一样,交换相邻元素直到我们得到排序数组)。在冒泡排序中,如果元素 A[i]>A[j](其中,i 请按照以下步骤解决问题 - 以下是上述方法的实施 - 时间复杂度: O(n),其中 n 是数组的大小C++
// C++ Code for the Check if array can be
// sorted by swapping adjacent elements
// of opposite parity
#include
Java
// Java Code for the Check if array can be
// sorted by swapping adjacent elements
// of opposite parity
import java.io.*;
class GFG {
// function to check if the array can be
// sorted in increasing order by
// swappingadjacent elements of same parity
static Boolean canBeSorted(int n, int A[])
{
// declaring & initializing odd and
// even variables, that stores previous
// odd and even elements respectively
int odd = -1, even = -1;
// declaring and initializing flag
// variable to store the answer
int flag = 1;
// iterating through the array
for (int i = 0; i < n; i++) {
// if the element is odd
if (A[i] % 2 == 1) {
if (A[i] < odd) {
flag = 0;
break;
}
// if it is less than previous
// odd element, then array can
// not be sorted
else {
// else we update the last
// odd element
odd = A[i];
}
}
// if the element is even
else {
if (A[i] < even) {
flag = 0;
break;
}
// if it is less than previous
// even element, then array can
// not be sorted
even = A[i];
// else we update
// the last even element
}
}
// all even elements are sorted and all
// odd elements are sorted, hence we
// return true as it is possible to
// sort the array
if (flag == 1) {
return true;
}
// not possible to sort the array
return false;
}
// Driver Code
public static void main (String[] args) {
int n = 4;
int A[] = { 1, 6, 51, 16 };
Boolean answer = canBeSorted(n, A);
if (answer == true) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python Code for the Check if array can be
# sorted by swapping adjacent elements
# of opposite parity
# function to check if the array can be
# sorted in increasing order by
# swappingadjacent elements of same parity
def canBeSorted(n, A):
# declaring & initializing odd and
# even variables, that stores previous
# odd and even elements respectively
odd = -1
even = -1
# declaring and initializing flag
# variable to store the answer
flag = 1
# iterating through the array
for i in range(0, n):
# if the element is odd
if (A[i] % 2 == 1):
if (A[i] < odd):
flag = 0
break
# if it is less than previous
# odd element, then array can
# not be sorted
else:
# else we update the last
# odd element
odd = A[i]
# if the element is even
else:
if (A[i] < even):
flag = 0
break
# if it is less than previous
# even element, then array can
# not be sorted
even = A[i]
# else we update
# the last even element
# all even elements are sorted and all
# odd elements are sorted, hence we
# return true as it is possible to
# sort the array
if (flag):
return True
# not possible to sort the array
return False
# Driver Code
n = 4
A = [1, 6, 51, 16]
answer = canBeSorted(n, A)
if (answer == 1):
print("YES")
else:
print("NO")
# This code is contributed by Palak Gupta
C#
// C# Code for the Check if array can be
// sorted by swapping adjacent elements
// of opposite parity
using System;
class GFG {
// function to check if the array can be
// sorted in increasing order by
// swappingadjacent elements of same parity
static bool canBeSorted(int n, int []A)
{
// declaring & initializing odd and
// even variables, that stores previous
// odd and even elements respectively
int odd = -1, even = -1;
// declaring and initializing flag
// variable to store the answer
int flag = 1;
// iterating through the array
for (int i = 0; i < n; i++) {
// if the element is odd
if (A[i] % 2 == 1) {
if (A[i] < odd) {
flag = 0;
break;
}
// if it is less than previous
// odd element, then array can
// not be sorted
else {
// else we update the last
// odd element
odd = A[i];
}
}
// if the element is even
else {
if (A[i] < even) {
flag = 0;
break;
}
// if it is less than previous
// even element, then array can
// not be sorted
even = A[i];
// else we update
// the last even element
}
}
// all even elements are sorted and all
// odd elements are sorted, hence we
// return true as it is possible to
// sort the array
if (flag == 1) {
return true;
}
// not possible to sort the array
return false;
}
// Driver Code
public static void Main () {
int n = 4;
int []A = { 1, 6, 51, 16 };
bool answer = canBeSorted(n, A);
if (answer == true) {
Console.WriteLine("YES");
}
else {
Console.WriteLine("NO");
}
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
YES
辅助空间: O(1),因为没有使用额外的空间