Check if the sequence of elements in given two Arrays is same or not

Given two arrays A and B each of size N, the task is to check if the sequencing of both the arrays is the same or not. If the sequencing of both the arrays is same, the print Yes otherwise print No.
Examples:
Input: A[] = { 10, 12, 9, 11 }, B[] = { 2, 7, -3, 5 };
Output: Yes
Explanation: In both the arrays 2nd element is greater than the first one.
The 3rd element is smaller than the 2nd and the last element is greater than the 3rd one.Input: A[] = { 1, 2, 3, 4 }, B[] = { 1, 3, 2, 4 };
Output: No
Â
Approach: Follow the below steps, to solve this problem:
- Create a vector of pairs, say arr[] and insert the elements of A and B in it.
- Each element in the vector arr, i.e. arr[i] is of type {A[i], B[i]}.
- Now, sort this vector arr, based on the first element.
- After sorting check if the second element of each pair in arr, should be a part of the sorted sequence.
 arr[i-1].second < arr[i].second, for each i.
- If it is, then print Yes, otherwise print No.
Below is the implementation of the above approach:
C++
// C++ code for the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to check if the sequencing// of both the arrays is the same or notbool sameOrder(vector<int>& A, vector<int>& B){Â Â Â Â int N = A.size();Â
    vector<pair<int, int> > 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 Codeint main(){Â Â Â Â vector<int> A = { 10, 12, 9, 11 };Â Â Â Â vector<int> B = { 2, 7, -3, 5 };Â
    if (sameOrder(A, B)) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;} |
Java
// Java code for the above approachimport java.util.*;Â
class GFG{Â Â static class pair implements Comparable<pair>Â Â {Â Â Â Â 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 notdef 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 Codeif __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 approachusing System;Â
public class GFG{Â Â class pair : IComparable<pair>Â Â {Â Â Â Â 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
<script>Â Â Â Â // JavaScript code for the above approachÂ
    // Function to check if the sequencing    // of both the arrays is the same or not    const sameOrder = (A, B) => {        let N = A.length;Â
        let arr = [];        for (let i = 0; i < N; ++i) {            arr.push([A[i], B[i]]);        }Â
        arr.sort((a, b) => a[0] - b[0]);Â
        // Check if the second element        // of each pair in arr        // is a part of the sorted sequence        for (let i = 1; i < N; ++i) {            if (arr[i - 1][1]                > arr[i][1]) {                return false;            }        }Â
        return true;    }Â
    // Driver Code    let A = [10, 12, 9, 11];    let B = [2, 7, -3, 5];Â
    if (sameOrder(A, B)) {        document.write("Yes");    }    else {        document.write("No");    }Â
// This code is contributed by rakeshsahniÂ
</script> |
Â
Â
Output
Yes
Â
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Â
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



