Check if every pair in Array B follows the same relation as their corresponding values in A
Given two Arrays A[] and B[] each of size N, the task is to check if the given arrays are valid or not, based on the following conditions:
- Every element in A at index i, will be mapped with the element in B at the same index only, i.e. (A[i] can be mapped with B[i] only)
- For any pair in A (A[i], A[j]), if A[i] > A[j], then its corresponding value in B should also be greater, i.e. B[i] > B[j] should be true.
- For any pair in A (A[i], A[j]), if A[i] = A[j], then its corresponding value in B should also be equal, i.e. B[i] = B[j] should be true.
Examples:
Input: N = 3, A[ ] = {10, 1, 17}, B[ ] = {10, 5, 15}
Output: true
Explanation: Consider all pairs in array A:
=> (10 and 1): Since 10>1, and their values in B (10 and 5 respectively) follow the same relation, therefore this is a valid pair.
=> (1 and 17): Since 1<17, and their values in B (5 and 15 respectively) follow the same relation, therefore this is a valid pair.
=> (10 and 17): Since 10<17, and their values in B (10 and 15 respectively) follow the same relation, therefore this is a valid pair.
As all the pairs are valid, therefore the given arrays are also valid. Hence the output is true.
Input: N = 5, A[ ] = {8, 5, 5, 10, 15}, B[ ] = {50, 10, 10, 15, 5 }
Output: false
Naive Approach: The most basic approach to solve is problem is to find each pair in array A, and check if the relation between that pair is satisfied for corresponding values in array B. If any such pair exists, where the values are not satisfied, then return false. Else return true.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach:
Intuition:
The idea for this approach is based on the observation that if the elements in an Array are sorted in ascending order,
- Then the first element will be always smaller than or equal to the second element
- Similarly, the first element will also be smaller than or equal to the last element
- Hence any element at index i will be smaller than or equal to element at index j, if (i < j)
Based on the above observation:
- If we try to sort the array A by remembering their corresponding values in array B, then instead of checking every pair in A, we can simply check for adjacent pairs in A to follow the conditions given in the problem.
- If all adjacent pairs in sorted A follows the conditions to be valid, then the given Arrays will be valid.
Illustration:
Suppose A[ ] = {10, 1, 17}, and B[ ] = {10, 5, 15}
If we sort A, by remembering their corresponding values in B, we get A[] = {1, 10, 17}, B[] = {5, 10, 15}
Now if we check adjacent pairs in A to follow the conditions given in problem, we get:
- Pair (1, 10): Since 1<10 and their values in B (5, 10) also follow same relation. Therefore this is a valid pair.
- Pair (10, 17): Since 10<17 and their values in B (10, 15) also follow same relation. Therefore this is a valid pair.
Since all the values in A has been verified, therefore the given arrays are also valid.
Algorithm: Follow the steps below to implement the above approach:
- Create a new vector of pairs to store corresponding values in {A[i], B[i]} format.
- Sort the vector, based on values of array A.
- For each adjacent pairs in the vector, check if:
- if A[i] < A[i+1] and B[i] > B[i+1], then this is not a valid pair.
- if A[i] == A[i+1] and B[i] != B[i+1], then this is not a valid pair.
- If none of the pairs in the above iteration satisfy the invalid pair conditions
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isValidArrays( int A[], int B[], int n)
{
vector<pair< int , int > > v1;
for ( int i = 0; i < n; i++) {
v1.push_back(make_pair(A[i], B[i]));
}
sort(v1.begin(), v1.end());
for ( int i = 0; i < v1.size() - 1; i++) {
if (v1[i].first == v1[i + 1].first) {
if (v1[i].second != v1[i + 1].second) {
return false ;
}
}
else {
if (v1[i].second >= v1[i + 1].second) {
return false ;
}
}
}
return true ;
}
int main()
{
int A[] = { 10, 1, 17 };
int B[] = { 10, 5, 15 };
int N = sizeof (A) / sizeof (A[0]);
cout << boolalpha << isValidArrays(A, B, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static boolean isValidArrays( int A[], int B[],
int n)
{
int v1[][] = new int [n][ 2 ];
for ( int i = 0 ; i < n; i++) {
v1[i][ 0 ] = A[i];
v1[i][ 1 ] = B[i];
}
Arrays.sort(v1,
(a, b) -> Integer.compare(a[ 0 ], b[ 0 ]));
for ( int i = 0 ; i < n - 1 ; i++) {
if (v1[i][ 0 ] == v1[i + 1 ][ 0 ]) {
if (v1[i][ 1 ] != v1[i + 1 ][ 1 ]) {
return false ;
}
}
else {
if (v1[i][ 1 ] >= v1[i + 1 ][ 1 ]) {
return false ;
}
}
}
return true ;
}
public static void main(String[] args)
{
int A[] = { 10 , 1 , 17 };
int B[] = { 10 , 5 , 15 };
int N = 3 ;
System.out.print(isValidArrays(A, B, N));
}
}
|
Python3
def isValidArrays(A, B, n):
v1 = []
for i in range (n):
v1.append([A[i], B[i]])
v1.sort()
for i in range ( len (v1) - 1 ):
if v1[i][ 0 ] = = v1[i + 1 ][ 0 ]:
if v1[i][ 1 ] ! = v1[i + 1 ][ 1 ]:
return False
else :
if v1[i][ 1 ] > = v1[i + 1 ][ 1 ]:
return False
return True
A = [ 10 , 1 , 17 ]
B = [ 10 , 5 , 15 ]
N = len (A)
print (isValidArrays(A, B, N))
|
C#
using System;
public class GFG
{
public static bool isValidArrays( int [] A, int [] B,
int n)
{
int [] v1 = new int [n];
int [] v2 = new int [n];
for ( int i = 0; i < n; i++) {
v1[i] = A[i];
v2[i] = B[i];
}
Array.Sort(v1, v2);
for ( int i = 0; i < n - 1; i++) {
if (v1[i] == v1[i + 1]) {
if (v2[i] != v2[i + 1]) {
return false ;
}
}
else {
if (v2[i] >= v2[i + 1]) {
return false ;
}
}
}
return true ;
}
public static void Main( string [] args)
{
int [] A = { 10, 1, 17 };
int [] B = { 10, 5, 15 };
int N = 3;
Console.WriteLine(isValidArrays(A, B, N));
}
}
|
Javascript
<script>
function isValidArrays(A, B, n)
{
let v1 = [];
for (let i = 0; i < n; i++) {
v1.push({ first: A[i], second: B[i] });
}
v1.sort( function (a, b) { return a.first - b.first })
for (let i = 0; i < v1.length - 1; i++)
{
if (v1[i].first == v1[i + 1].first)
{
if (v1[i].second != v1[i + 1].second) {
return false ;
}
}
else
{
if (v1[i].second >= v1[i + 1].second) {
return false ;
}
}
}
return true ;
}
let A = [10, 1, 17];
let B = [10, 5, 15];
let N = A.length;
document.write(isValidArrays(A, B, N));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N), for creating a vector of pairs.
Last Updated :
02 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...