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

Last Updated : 28 Mar, 2022

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 ` `using` `namespace` `std;`   `// Function to check if the sequencing` `// of both the arrays is the same or not` `bool` `sameOrder(vector<``int``>& A, vector<``int``>& 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<``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 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

 ``

Output

`Yes`

Time Complexity: O(N * logN)
Auxiliary Space: O(N)