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 not bool 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 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<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 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<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> |
Yes
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!