📌  相关文章
📜  检查数组是否可以通过交换相反奇偶校验的相邻元素来排序

📅  最后修改于: 2022-05-13 01:56:10.426000             🧑  作者: Mango

检查数组是否可以通过交换相反奇偶校验的相邻元素来排序

给定一个大小为n的数组A ,任务是检查数组是否可以按升序排序,如果唯一允许的操作是交换相邻元素(如果它们具有相反的奇偶性)。该操作可以进行任意次数。

例子

方法:可以观察到,如果偶数元素的顺序或奇数元素的顺序是递减的,那么就不可能对数组进行排序。

我们可以假设我们将使用冒泡排序对数组进行排序(就像在冒泡排序中一样,交换相邻元素直到我们得到排序数组)。在冒泡排序中,如果元素 A[i]>A[j](其中,i

请按照以下步骤解决问题 -

  • 创建 2 个名为“odd”和“even”的变量,分别存储之前的奇数和偶数元素。
  • 遍历数组。
  • 在每次迭代中,检查元素是偶数还是奇数,并分别与之前的偶数或奇数元素进行比较。
  • 如果当前偶/奇元素小于前一个偶/奇元素,则返回false。
  • 如果迭代结束,则返回 true,因为可以对数组进行排序,

以下是上述方法的实施 -

C++
// C++ Code for the Check if array can be
// sorted by swapping adjacent elements
// of opposite parity
#include 
using namespace std;
 
// function to check if the array can be
// sorted in increasing order by
// swappingadjacent elements of same parity
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) {
        return true;
    }
 
    // not possible to sort the array
    return false;
}
 
// Driver Code
int main()
{
    int n = 4;
    int A[] = { 1, 6, 51, 16 };
    bool answer = canBeSorted(n, A);
 
    if (answer == 1) {
        cout << "YES";
    }
    else {
        cout << "NO";
    }
}


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(n),其中 n 是数组的大小
辅助空间: O(1),因为没有使用额外的空间