📌  相关文章
📜  检查给定两个数组中的元素序列是否相同

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

检查给定两个数组中的元素序列是否相同

给定两个大小为N的数组AB ,任务是检查两个数组的顺序是否相同。如果两个数组的顺序相同,则打印Yes否则打印No

例子:

方法:按照以下步骤,解决这个问题:

  • 创建一个对向量,比如arr[]并在其中插入 A 和 B 的元素。
  • 向量 arr 中的每个元素,即 arr[i] 的类型为 {A[i], B[i]}。
  • 现在,根据第一个元素对该向量 arr 进行排序。
  • 排序后检查 arr 中每对的第二个元素是否应该是排序序列的一部分。
  • 如果是,则打印Yes ,否则打印No

下面是上述方法的实现:

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to check if the sequencing
// of both the arrays is the same or not
bool sameOrder(vector& A, vector& B)
{
    int N = A.size();
 
    vector > arr(N);
    for (int i = 0; i < N; ++i) {
        arr[i] = { A[i], B[i] };
    }
 
    sort(arr.begin(), arr.end());
 
    // Check if the second element
    // of each pair in arr
    // is a part of the sorted sequence
    for (int i = 1; i < N; ++i) {
        if (arr[i - 1].second
            > arr[i].second) {
            return false;
        }
    }
 
    return true;
}
 
// Driver Code
int main()
{
    vector A = { 10, 12, 9, 11 };
    vector B = { 2, 7, -3, 5 };
 
    if (sameOrder(A, B)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG{
  static class pair implements Comparable
  {
    int first,second;
    pair(int s, int e)
    {
      first = s;
      second = e;
    }
 
    public int compareTo(pair p)
    {
      return this.first - p.first;
    }
  }
 
  // Function to check if the sequencing
  // of both the arrays is the same or not
  static boolean sameOrder(int []A, int []B)
  {
    int N = A.length;
 
    pair[] arr = new pair[N];
    for (int i = 0; i < N; ++i) {
      arr[i] = new pair( A[i], B[i] );
    }
 
    Arrays.sort(arr);
 
    // Check if the second element
    // of each pair in arr
    // is a part of the sorted sequence
    for (int i = 1; i < N; ++i) {
      if (arr[i - 1].second
          > arr[i].second) {
        return false;
      }
    }
 
    return true;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int []A = { 10, 12, 9, 11 };
    int []B = { 2, 7, -3, 5 };
 
    if (sameOrder(A, B)) {
      System.out.print("Yes");
    }
    else {
      System.out.print("No");
    }
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python 3 code for the above approach
 
# Function to check if the sequencing
# of both the arrays is the same or not
def sameOrder(A, B):
 
    N = len(A)
 
    arr = []
    for i in range(N):
        arr.append([A[i], B[i]])
 
    arr.sort()
 
    # Check if the second element
    # of each pair in arr
    # is a part of the sorted sequence
    for i in range(1, N):
        if (arr[i - 1][1]
                > arr[i][1]):
            return False
 
    return True
 
# Driver Code
if __name__ == "__main__":
 
    A = [10, 12, 9, 11]
    B = [2, 7, -3, 5]
 
    if (sameOrder(A, B)):
        print("Yes")
 
    else:
        print("No")
 
        # This code is contributed by ukasp.


C#
// C# code for the above approach
using System;
 
public class GFG{
  class pair : IComparable
  {
    public int first, second;
    public pair(int first, int second)
    {
      this.first = first;
      this.second = second;
    }
    public int CompareTo(pair p)
    {
      return this.first-p.first;
    }
  }
 
  // Function to check if the sequencing
  // of both the arrays is the same or not
  static bool sameOrder(int []A, int []B)
  {
    int N = A.Length;
 
    pair[] arr = new pair[N];
    for (int i = 0; i < N; ++i) {
      arr[i] = new pair( A[i], B[i] );
    }
 
    Array.Sort(arr);
 
    // Check if the second element
    // of each pair in arr
    // is a part of the sorted sequence
    for (int i = 1; i < N; ++i) {
      if (arr[i - 1].second
          > arr[i].second) {
        return false;
      }
    }
 
    return true;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int []A = { 10, 12, 9, 11 };
    int []B = { 2, 7, -3, 5 };
 
    if (sameOrder(A, B)) {
      Console.Write("Yes");
    }
    else {
      Console.Write("No");
    }
  }
}
 
// This code contributed by shikhasingrajput


Javascript



输出
Yes

时间复杂度: O(N * logN)
辅助空间: 在)