Check whether it is possible to make both arrays equal by modifying a single element

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include<bits/stdc++.h>
using namespace std;
  
// Function to check if both 
// sequences can be made equal 
static bool check(int n, int k, 
                    int *a, int *b) 
{
    // Sorting both the arrays 
    sort(a,a+n);
    sort(b,b+n);
  
    // Flag to tell if there are 
    // more than one mismatch 
    bool fl = false;
  
    // To stores the index 
    // of mismatched element 
    int ind = -1;
    for (int i = 0; i < n; i++) 
    {
        if (a[i] != b[i])
        {
  
            // If there is more than one 
            // mismatch then return False 
            if (fl == true
            {
                return false;
            }
            fl = true;
            ind = i;
        }
    }
          
    // If there is no mismatch or the 
    // difference between the 
    // mismatching elements is <= k 
    // then return true 
    if (ind == -1 | abs(a[ind] - b[ind]) <= k)
    {
        return true;
    }
    return false;
  
}
  
// Driver code
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;
}
  
// This code is contributed by mits

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.util.Arrays;
class GFG 
{
  
    // Function to check if both 
    // sequences can be made equal 
    static boolean check(int n, int k, 
                        int[] a, int[] b) 
    {
  
        // Sorting both the arrays 
        Arrays.sort(a);
        Arrays.sort(b);
  
        // Flag to tell if there are 
        // more than one mismatch 
        boolean fl = false;
  
        // To stores the index 
        // of mismatched element 
        int ind = -1;
        for (int i = 0; i < n; i++) 
        {
            if (a[i] != b[i])
            {
  
                // If there is more than one 
                // mismatch then return False 
                if (fl == true
                {
                    return false;
                }
                fl = true;
                ind = i;
            }
        }
          
        // If there is no mismatch or the 
        // difference between the 
        // mismatching elements is <= k 
        // then return true 
        if (ind == -1 | Math.abs(a[ind] - b[ind]) <= k)
        {
            return true;
        }
        return false;
  
    }
  
    // Driver code
    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");
        }
    }
  
// This code is contributed by 29AjayKumar

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the above approach
  
# Function to check if both 
# sequences can be made equal
def check(n, k, a, b):
  
    # Sorting both the arrays
    a.sort()
    b.sort()
  
    # Flag to tell if there are
    # more than one mismatch
    fl = False
  
    # To stores the index 
    # of mismatched element
    ind = -1
    for i in range(n):
        if(a[i] != b[i]):
  
            # If there is more than one
            # mismatch then return False
            if(fl == True):
                return False
            fl = True
            ind = i
  
    # If there is no mismatch or the 
    # difference between the 
    # mismatching elements is <= k
    # then return true
    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")

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG 
{
  
    // Function to check if both 
    // sequences can be made equal 
    static bool check(int n, int k, 
                        int[] a, int[] b) 
    {
  
        // Sorting both the arrays 
        Array.Sort(a);
        Array.Sort(b);
  
        // Flag to tell if there are 
        // more than one mismatch 
        bool fl = false;
  
        // To stores the index 
        // of mismatched element 
        int ind = -1;
        for (int i = 0; i < n; i++) 
        {
            if (a[i] != b[i])
            {
  
                // If there is more than one 
                // mismatch then return False 
                if (fl == true
                {
                    return false;
                }
                fl = true;
                ind = i;
            }
        }
          
        // If there is no mismatch or the 
        // difference between the 
        // mismatching elements is <= k 
        // then return true 
        if (ind == -1 | Math.Abs(a[ind] - b[ind]) <= k)
        {
            return true;
        }
        return false;
    }
  
    // Driver code
    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");
        }
    }
}
  
// This code is contributed by Rajput-Ji

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP implementation of the 
// above approach
  
// Function to check if both 
// sequences can be made equal
function check($n, $k, &$a, &$b)
{
  
    // Sorting both the arrays
    sort($a);
    sort($b);
  
    // Flag to tell if there are
    // more than one mismatch
    $fl = False;
  
    // To stores the index 
    // of mismatched element
    $ind = -1;
    for ($i = 0; $i < $n; $i++)
    {
        if($a[$i] != $b[$i])
        {
  
            // If there is more than one
            // mismatch then return False
            if($fl == True)
                return False;
            $fl = True;
            $ind = $i;
        }
    }
  
    // If there is no mismatch or the 
    // difference between the 
    // mismatching elements is <= k
    // then return true
    if($ind == -1 || abs($a[$ind] - 
                         $b[$ind]) <= $k)
        return True;
    return False;
      
}
  
// Driver Code
$n = 2;
$k = 4;
$a = array(1, 5);
$b = array(1, 1);
if(check($n, $k, $a, $b))
    echo "Yes";
else
    echo "No";
      
// This code is contributed by ita_c
?>

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.