📌  相关文章
📜  使用相邻的元素交换将一个数组转换为另一个数组

📅  最后修改于: 2021-04-21 22:02:57             🧑  作者: Mango

给定N个整数的两个数组arr1 []arr2 [] 。我们可以从数组arr1 []中选择任意两个相邻元素,如果它们具有相反的奇偶性,则将它们交换,任务是检查是否可以通过对arr1 [进行给定的操作来将数组arr1 []转换为数组arr2 []] 。如果可以将数组arr1 []转换为arr2 [] ,则打印“是” ,否则打印“否”
例子:

方法:可以使用贪婪方法解决问题。由于我们不能交换任何两个偶数或奇数。因此,偶数和奇数在数组arr1 []arr2 []中的相对位置必须完全相同,以使两个数组与给定操作相等。步骤如下:

  1. 创建两个数组(例如even []奇数[] ),将arr1 []中的所有偶数和奇数分别插入偶数[]奇数[]
  2. 现在检查arr2 []中的偶数和奇数是否与even []奇数[]中的顺序相同。
  3. 如果以上步骤没有从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是数组中元素的数量。