Given two sequences of integers ‘A’ and ‘B’, and an integer ‘k’. The task is to check if we can make both sequences equal by modifying any one element from the sequence A in the following way:
We can add any number from the range [-k, k] to any element of A. This operation must only be performed once. Print Yes if it is possible or No otherwise.
Examples:
Input: K = 2, A[] = {1, 2, 3}, B[] = {3, 2, 1}
Output: Yes
0 can be added to any element and both the sequences will be equal.
Input: K = 4, A[] = {1, 5}, B[] = {1, 1}
Output: Yes
-4 can be added to 5 then the sequence A becomes {1, 1} which is equal to the sequence B.
Approach: Notice that to make both the sequence equal with just one move there has to be only one mismatching element in both the sequences and the absolute difference between them must be less than or equal to ‘k’.
- Sort both the arrays and look for the mismatching elements.
- If there are more than one mismatch elements then print ‘No’
- Else, find the absolute difference between the elements.
- If the difference <= k then print ‘Yes’ else print ‘No’.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
static bool check( int n, int k,
int *a, int *b)
{
sort(a,a+n);
sort(b,b+n);
bool fl = false ;
int ind = -1;
for ( int i = 0; i < n; i++)
{
if (a[i] != b[i])
{
if (fl == true )
{
return false ;
}
fl = true ;
ind = i;
}
}
if (ind == -1 | abs (a[ind] - b[ind]) <= k)
{
return true ;
}
return false ;
}
int main()
{
int n = 2, k = 4;
int a[] = {1, 5};
int b[] = {1, 1};
if (check(n, k, a, b))
{
printf ( "Yes" );
}
else
{
printf ( "No" );
}
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static boolean check( int n, int k,
int [] a, int [] b)
{
Arrays.sort(a);
Arrays.sort(b);
boolean fl = false ;
int ind = - 1 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] != b[i])
{
if (fl == true )
{
return false ;
}
fl = true ;
ind = i;
}
}
if (ind == - 1 | Math.abs(a[ind] - b[ind]) <= k)
{
return true ;
}
return false ;
}
public static void main(String[] args)
{
int n = 2 , k = 4 ;
int [] a = { 1 , 5 };
int b[] = { 1 , 1 };
if (check(n, k, a, b))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python 3
def check(n, k, a, b):
a.sort()
b.sort()
fl = False
ind = - 1
for i in range (n):
if (a[i] ! = b[i]):
if (fl = = True ):
return False
fl = True
ind = i
if (ind = = - 1 or abs (a[ind] - b[ind]) < = k):
return True
return False
n, k = 2 , 4
a = [ 1 , 5 ]
b = [ 1 , 1 ]
if (check(n, k, a, b)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool check( int n, int k,
int [] a, int [] b)
{
Array.Sort(a);
Array.Sort(b);
bool fl = false ;
int ind = -1;
for ( int i = 0; i < n; i++)
{
if (a[i] != b[i])
{
if (fl == true )
{
return false ;
}
fl = true ;
ind = i;
}
}
if (ind == -1 | Math.Abs(a[ind] - b[ind]) <= k)
{
return true ;
}
return false ;
}
public static void Main()
{
int n = 2, k = 4;
int [] a = {1, 5};
int [] b = {1, 1};
if (check(n, k, a, b))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function check(n, k, a, b)
{
a.sort();
b.sort();
let fl = false ;
let ind = -1;
for (let i = 0; i < n; i++)
{
if (a[i] != b[i])
{
if (fl == true )
{
return false ;
}
fl = true ;
ind = i;
}
}
if (ind == -1 | Math.abs(a[ind] - b[ind]) <= k)
{
return true ;
}
return false ;
}
let n = 2, k = 4;
let a = [1, 5];
let b = [1, 1];
if (check(n, k, a, b))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
PHP
<?php
function check( $n , $k , & $a , & $b )
{
sort( $a );
sort( $b );
$fl = False;
$ind = -1;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] != $b [ $i ])
{
if ( $fl == True)
return False;
$fl = True;
$ind = $i ;
}
}
if ( $ind == -1 || abs ( $a [ $ind ] -
$b [ $ind ]) <= $k )
return True;
return False;
}
$n = 2;
$k = 4;
$a = array (1, 5);
$b = array (1, 1);
if (check( $n , $k , $a , $b ))
echo "Yes" ;
else
echo "No" ;
?>
|
Complexity Analysis:
- Time Complexity: O(nlog(n))
- Auxiliary Space: O(1)
Approach: Hash Map
Steps:
- Initialize an empty hash map, freqMap.
- Iterate over each element in sequence A and update the frequencies of elements in freqMap.
- Iterate over each element in sequence B and decrement the frequencies of elements in freqMap.
- If all frequencies in freqMap are 0 or within the range [-k, k], print “Yes”.
- Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
bool makeSequencesEqual( int k, const vector< int >& A,
const vector< int >& B)
{
unordered_map< int , int > freqMap;
for ( int num : A) {
freqMap[num]++;
}
for ( int num : B) {
freqMap[num]--;
}
for ( const auto & pair : freqMap) {
int num = pair.first;
int freq = pair.second;
if (freq != 0 && abs (freq) > k) {
return false ;
}
}
return true ;
}
int main()
{
int k = 2;
vector< int > A = { 1, 2, 3 };
vector< int > B = { 3, 2, 1 };
if (makeSequencesEqual(k, A, B)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
public class GFG {
public static boolean makeSequencesEqual( int k, List<Integer> A, List<Integer> B) {
Map<Integer, Integer> freqMap = new HashMap<>();
for ( int num : A) {
freqMap.put(num, freqMap.getOrDefault(num, 0 ) + 1 );
}
for ( int num : B) {
freqMap.put(num, freqMap.getOrDefault(num, 0 ) - 1 );
}
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
int num = entry.getKey();
int freq = entry.getValue();
if (freq != 0 && Math.abs(freq) > k) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
int k = 2 ;
List<Integer> A = new ArrayList<>();
A.add( 1 );
A.add( 2 );
A.add( 3 );
List<Integer> B = new ArrayList<>();
B.add( 3 );
B.add( 2 );
B.add( 1 );
if (makeSequencesEqual(k, A, B)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python
def make_sequences_equal(k, A, B):
freq_map = {}
for num in A:
freq_map[num] = freq_map.get(num, 0 ) + 1
for num in B:
freq_map[num] = freq_map.get(num, 0 ) - 1
for num, freq in freq_map.items():
if freq ! = 0 and abs (freq) > k:
return False
return True
if __name__ = = "__main__" :
k = 2
A = [ 1 , 2 , 3 ]
B = [ 3 , 2 , 1 ]
if make_sequences_equal(k, A, B):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static bool MakeSequencesEqual( int k, List< int > A,
List< int > B)
{
Dictionary< int , int > freqMap
= new Dictionary< int , int >();
foreach ( int num in A)
{
if (freqMap.ContainsKey(num))
freqMap[num]++;
else
freqMap[num] = 1;
}
foreach ( int num in B)
{
if (freqMap.ContainsKey(num))
freqMap[num]--;
else
freqMap[num] = -1;
}
foreach (KeyValuePair< int , int > pair in freqMap)
{
int num = pair.Key;
int freq = pair.Value;
if (freq != 0 && Math.Abs(freq) > k) {
return false ;
}
}
return true ;
}
static void Main()
{
int k = 2;
List< int > A = new List< int >() { 1, 2, 3 };
List< int > B = new List< int >() { 3, 2, 1 };
if (MakeSequencesEqual(k, A, B)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
function makeSequencesEqual(k, A, B) {
const freqMap = new Map();
for (let num of A) {
freqMap.set(num, (freqMap.get(num) || 0) + 1);
}
for (let num of B) {
freqMap.set(num, (freqMap.get(num) || 0) - 1);
}
for (let [num, freq] of freqMap) {
if (freq !== 0 && Math.abs(freq) > k) {
return false ;
}
}
return true ;
}
const k = 2;
const A = [1, 2, 3];
const B = [3, 2, 1];
if (makeSequencesEqual(k, A, B)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(n), where n is the total number of elements in sequences A and B.
Auxiliary Space: O(m), where m is the number of unique elements in sequences A and B.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
04 Sep, 2023
Like Article
Save Article