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++
#include <bits/stdc++.h>
using namespace std;
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());
for ( int i = 1; i < N; ++i) {
if (arr[i - 1].second
> arr[i].second) {
return false ;
}
}
return true ;
}
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
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;
}
}
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);
for ( int i = 1 ; i < N; ++i) {
if (arr[i - 1 ].second
> arr[i].second) {
return false ;
}
}
return true ;
}
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" );
}
}
}
|
Python3
def sameOrder(A, B):
N = len (A)
arr = []
for i in range (N):
arr.append([A[i], B[i]])
arr.sort()
for i in range ( 1 , N):
if (arr[i - 1 ][ 1 ]
> arr[i][ 1 ]):
return False
return True
if __name__ = = "__main__" :
A = [ 10 , 12 , 9 , 11 ]
B = [ 2 , 7 , - 3 , 5 ]
if (sameOrder(A, B)):
print ( "Yes" )
else :
print ( "No" )
|
C#
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;
}
}
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);
for ( int i = 1; i < N; ++i) {
if (arr[i - 1].second
> arr[i].second) {
return false ;
}
}
return true ;
}
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" );
}
}
}
|
Javascript
<script>
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]);
for (let i = 1; i < N; ++i) {
if (arr[i - 1][1]
> arr[i][1]) {
return false ;
}
}
return true ;
}
let A = [10, 12, 9, 11];
let B = [2, 7, -3, 5];
if (sameOrder(A, B)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...