📌  相关文章
📜  检查是否可以通过修改单个元素使两个数组相等

📅  最后修改于: 2021-04-22 01:40:49             🧑  作者: Mango

给定两个整数“ A”和“ B”以及整数“ k”的序列。任务是通过以下方式修改序列A中的任何一个元素来检查是否可以使两个序列相等:
我们可以将[-k,k]范围内的任何数字添加到A的任何元素。此操作只能执行一次。如果可以,请打印是,否则,请打印

例子:

方法:请注意,要使两个序列仅移动一遍就必须使两个序列中只有一个不匹配元素,并且它们之间的绝对差必须小于或等于’k’。

  • 对两个数组进行排序,然后查找不匹配的元素。
  • 如果存在多个不匹配元素,则打印“否”
  • 否则,找到元素之间的绝对差异。
  • 如果差异<= k,则打印“是”,否则打印“否”。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include
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


Java
// 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


Python 3
# 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")


C#
// 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


PHP


输出:
Yes