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

📅  最后修改于: 2021-09-06 05:29:53             🧑  作者: Mango

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

方法:该问题可以使用贪心方法解决。因为我们不能交换任何两个偶数或奇数。因此,数组arr1[]arr2[] 中偶数和奇数的相对位置必须完全相同,才能使两个数组与给定的运算相等。以下是步骤:

  1. 创建两个数组(比如even[]odd[] )将来自arr1[] 的所有偶数和奇数分别插入到even[]odd[] 中
  2. 现在检查arr2[]中的偶数和奇数是否与 even []odd[]中的顺序相同。
  3. 如果上述步骤没有给出来自arr2[] 的任何数字,这些数字分别不在even[]odd[]数组中的数字顺序中,则打印“是”,否则打印“否”

下面是上述方法的实现:

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


Javascript


输出:

No

时间复杂度: O(N) ,其中 N 是数组中的元素数。
辅助空间: O(N) ,其中 N 是数组中元素的数量。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live