Check if the relative ordering of elements in given two Arrays is same or not
Last Updated :
22 Jul, 2024
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, the relative ordering of elements are same:
- A[0] < A[1] and B[0] < B[1].
- A[1] > A[2] and B[1] > B[2].
- A[2] < A[3] and B[2] < B[3].
Input: A[] = { 1, 2, 3, 4 }, B[] = { 1, 3, 2, 4 };
Output: No
Explanation: In both the arrays, the relative ordering of elements are not same as A[1] < A[2] but B[1] > B[2].
Approach: To solve the problem, follow the below idea:
The idea is simple. We traverse both array simultaneously and check for the relative ordering of the current element with the previous element. If the relative ordering of A[i] and A[i - 1] is not the same as B[i] and B[i - 1], return false. If all the elements have same relative ordering, return true.
Below is the implementation of the above approach:
C++
// C++ code to check if the relative ordering of elements in
// given two Arrays is same or not
#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();
if (B.size() != N) {
return false;
}
// 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 (A[i] > A[i - 1] && B[i] <= B[i - 1]) {
return false;
}
if (A[i] < A[i - 1] && B[i] >= B[i - 1]) {
return false;
}
if (A[i] == A[i - 1] && B[i] != B[i - 1]) {
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 to check if the relative ordering of elements in
// given two Arrays is same or not
import java.util.ArrayList;
import java.util.List;
public class SameOrderCheck {
// Function to check if the sequencing
// of both the arrays is the same or not
static boolean sameOrder(List<Integer> A, List<Integer> B) {
int N = A.size();
if (B.size() != N) {
return false;
}
// 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 (A.get(i) > A.get(i - 1) && B.get(i) <= B.get(i - 1)) {
return false;
}
if (A.get(i) < A.get(i - 1) && B.get(i) >= B.get(i - 1)) {
return false;
}
if (A.get(i).equals(A.get(i - 1)) && !B.get(i).equals(B.get(i - 1))) {
return false;
}
}
return true;
}
// Driver Code
public static void main(String[] args) {
List<Integer> A = new ArrayList<>();
A.add(10);
A.add(12);
A.add(9);
A.add(11);
List<Integer> B = new ArrayList<>();
B.add(2);
B.add(7);
B.add(-3);
B.add(5);
if (sameOrder(A, B)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
Python
# Python code to check if the relative ordering of
# elements in given two Arrays is same or not
# Function to check if the sequencing
# of both the arrays is the same or not
def sameOrder(A, B):
N = len(A)
if len(B) != N:
return False
# Check if the second element
# of each pair in arr
# is a part of the sorted sequence
for i in range(1, N):
if A[i] > A[i - 1] and B[i] <= B[i - 1]:
return False
if A[i] < A[i - 1] and B[i] >= B[i - 1]:
return False
if A[i] == A[i - 1] and B[i] != B[i - 1]:
return False
return True
# Driver Code
A = [10, 12, 9, 11]
B = [2, 7, -3, 5]
if sameOrder(A, B):
print("Yes")
else:
print("No")
C#
// C# code to check if the relative ordering of elements in
// given two Arrays is same or not
using System;
public class GFG {
// 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;
if (B.Length != N)
return false;
// 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 ((A[i] > A[i - 1] && B[i] <= B[i - 1])
|| (A[i] < A[i - 1] && B[i] >= B[i - 1])
|| (A[i] == A[i - 1] && B[i] != B[i - 1])) {
return false;
}
}
return true;
}
// Driver Code
public static void Main()
{
int[] A = { 10, 12, 9, 11 };
int[] B = { 2, 7, -3, 5 };
if (SameOrder(A, B)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript code to check if the relative ordering of elements in
// given two Arrays is same or not
// Function to check if the sequencing
// of both the arrays is the same or not
function sameOrder(A, B)
{
const N = A.length;
if (B.length !== N) {
return false;
}
// 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 (A[i] > A[i - 1] && B[i] <= B[i - 1]) {
return false;
}
if (A[i] < A[i - 1] && B[i] >= B[i - 1]) {
return false;
}
if (A[i] === A[i - 1] && B[i] !== B[i - 1]) {
return false;
}
}
return true;
}
// Driver Code
const A = [ 10, 12, 9, 11 ];
const B = [ 2, 7, -3, 5 ];
if (sameOrder(A, B)) {
console.log("Yes");
}
else {
console.log("No");
}
PHP
<?php
// PHP code to check if the relative ordering of elements in
// given two Arrays is same or not
// Function to check if the sequencing
// of both the arrays is the same or not
function sameOrder($A, $B) {
$N = count($A);
if (count($B) !== $N) {
return false;
}
// Check if the second element
// of each pair in arr
// is a part of the sorted sequence
for ($i = 1; $i < $N; ++$i) {
if ($A[$i] > $A[$i - 1] && $B[$i] <= $B[$i - 1]) {
return false;
}
if ($A[$i] < $A[$i - 1] && $B[$i] >= $B[$i - 1]) {
return false;
}
if ($A[$i] === $A[$i - 1] && $B[$i] !== $B[$i - 1]) {
return false;
}
}
return true;
}
// Driver Code
$A = [10, 12, 9, 11];
$B = [2, 7, -3, 5];
if (sameOrder($A, $B)) {
echo "Yes";
} else {
echo "No";
}
?>
Time Complexity: O(N), where N is the size of input array arr[].
Auxiliary Space: O(1)
Similar Reads
Check if set of first X elements of one Array is same as set of first Y elements of other Given two arrays arr1[] and arr2[] of size N each and an array Q[][2] consisting of M queries of the form [x, y], the task for each query is to check if the set of values contained from first x elements of arr1[] is equal to set of values from in first y elements of arr2[]. Note: Two sets are equal
15+ min read
Check if the given array is same as its inverse permutation Given an array arr[] consisting of integers in the range [1, N], the task is to determine whether the Inverse Permutation of the given array is same as the given array. An inverse permutation is a permutation obtained by inserting the position of all elements at the position equal to the respective
8 min read
Rearrange two given arrays such that sum of same indexed elements lies within given range Given two arrays arr1[] and arr2[] consisting of N positive integers and an even integer K, the task is to check if the sum of the same indexed elements in the two arrays lie in the range [K/2, K] after rearranging the given arrays or not. If it is possible to obtain such an arrangement, then print
6 min read
Find relative complement of two sorted arrays Given two sorted arrays arr1 and arr2 of size m and n respectively. We need to find relative complement of two array i.e, arr1 - arr2 which means that we need to find all those elements which are present in arr1 but not in arr2. Examples: Input : arr1[] = {3, 6, 10, 12, 15} arr2[] = {1, 3, 5, 10, 16
10 min read
Check if given array is almost sorted (elements are at-most one position away) Given an array with n distinct elements. An array is said to be almost sorted (non-decreasing) if any of its elements can occur at a maximum of 1 distance away from their original places in the sorted array. We need to find whether the given array is almost sorted or not.Examples: Input : arr[] = {1
11 min read
Check if given Preorder, Inorder and Postorder traversals are of same tree Given Preorder, Inorder, and Postorder traversals of some tree. Write a program to check if they all are of the same tree. Examples: Input: Inorder -> 4 2 5 1 3 Preorder -> 1 2 4 5 3 Postorder -> 4 5 2 3 1Output: YesExplanation: All of the above three traversals are of the same tree 1 / \ 2
15+ min read