📌  相关文章
📜  找到 K 使得数组 A 可以通过将 K 添加到选定范围 [L, R] 来转换为数组 B

📅  最后修改于: 2022-05-13 01:57:51.489000             🧑  作者: Mango

找到 K 使得数组 A 可以通过将 K 添加到选定范围 [L, R] 来转换为数组 B

给定两个由唯一元素组成的长度为N的数组a[]b[] ,任务是找到一个数K (K > 0),使得第一个数组可以通过将 K 添加到选定范围来转换为第二个数组[L, R] 在数组中。如果不存在这样的数字 K,则打印NA

例子:

方法

  • 创建一个临时数组c[] ,其中包含数组元素之间的差异,即
ci = bi - ai
  • 然后为数组 c[n] 的所有非零元素及其索引创建一个向量对。因此,向量对将是:
vector

where c[i] is a non zero value in c[]
and i is the index of c[i]
  • 如果指标值相差1且差值相同,则K=差值,[L,R]=指标范围。
  • 因此,数组 a[n] 可以通过将 K 添加到 [a[L], a[R]] 来转换为 b[n]。

例如

  • 给定
a[n] = [3, 7, 1, 4, 0, 2, 2]
b[n] = [3, 7, 3, 6, 2, 2, 2]
  • 因此,在创建临时数组 c[] 和向量对时:
c[n] = [0, 0, 2, 2, 2, 0, 0]
vector pair = {{2, 2}, {2, 3}, {2, 4}}
  • 由于向量对中的所有索引值 (2, 3, 4) 相差 1,因此它们是连续的。
  • 并且差异的值是相同的(2)。
  • 因此,我们可以简单地将不同的值添加到给定索引 [2, 4] 中的第一个数组 a[n] 中,以将其转换为第二个数组 b[n]。
  • 因此,所需的 K 值为 2

下面是上述方法的实现:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function to Check if it is possible to
// convert a given array to another array
// by adding elements to first array
bool checkconv(int a[], int b[], int n)
{
    int c[n], flag = 0;
 
    // Create a temporary array c[]
    // which contains the difference
    // of the array elements
    for (int i = 0; i < n; i++) {
 
        c[i] = b[i] - a[i];
    }
 
    // Create a vector pair for all non zero
    // elements of array c[n] with there index
    vector > idxs;
    for (int i = 0; i < n; i++) {
        if (c[i] != 0)
            idxs.push_back(make_pair(i, c[i]));
    }
 
    // Check If the index value differs by 1
    // and the difference value is same
    for (int i = 0; i < idxs.size() - 1; i++) {
        if (idxs[i + 1].first - idxs[i].first != 1
            || idxs[i + 1].second != idxs[i].second) {
            flag = 1;
            break;
        }
    }
 
    return !flag;
}
 
// Function to calculate the value of K
int diffofarrays(int a[], int b[], int n)
{
    int c[n], ans = 0;
    for (int i = 0; i < n; i++) {
        c[i] = b[i] - a[i];
    }
    for (int i = 0; i < n; i++) {
        if (c[i] != 0) {
            ans = c[i];
            break;
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int A[] = { 3, 7, 1, 4, 0, 2, 2 };
    int B[] = { 3, 7, 3, 6, 2, 2, 2 };
    int arr_size = sizeof(A) / sizeof(A[0]);
 
    if (checkconv(A, B, arr_size)) {
        cout << diffofarrays(A, B, arr_size) << endl;
    }
    else
        cout << "NA" << endl;
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
 
class GFG
{
    static class pair
    {
        int first, second;
        public pair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }
    }
     
// Function to Check if it is possible to
// convert a given array to another array
// by adding elements to first array
static boolean checkconv(int a[], int b[], int n)
{
    int []c = new int[n];
    int flag = 0;
 
    // Create a temporary array c[]
    // which contains the difference
    // of the array elements
    for (int i = 0; i < n; i++)
    {
 
        c[i] = b[i] - a[i];
    }
 
    // Create a vector pair for all non zero
    // elements of array c[n] with there index
    Vector idxs = new Vector();
    for (int i = 0; i < n; i++)
    {
        if (c[i] != 0)
            idxs.add(new pair(i, c[i]));
    }
 
    // Check If the index value differs by 1
    // and the difference value is same
    for (int i = 0; i < idxs.size() - 1; i++)
    {
        if (idxs.get(i + 1).first - idxs.get(i).first != 1
            || idxs.get(i + 1).second != idxs.get(i).second)
        {
            flag = 1;
            break;
        }
    }
 
    return flag == 1 ? false:true;
}
 
// Function to calculate the value of K
static int diffofarrays(int a[], int b[], int n)
{
    int []c = new int[n];
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        c[i] = b[i] - a[i];
    }
    for (int i = 0; i < n; i++)
    {
        if (c[i] != 0)
        {
            ans = c[i];
            break;
        }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int A[] = { 3, 7, 1, 4, 0, 2, 2 };
    int B[] = { 3, 7, 3, 6, 2, 2, 2 };
    int arr_size = A.length;
 
    if (checkconv(A, B, arr_size))
    {
        System.out.print(diffofarrays(A, B, arr_size) +"\n");
    }
    else
        System.out.print("NA" +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of above approach
 
# Function to Check if it is possible to
# convert a given array to another array
# by adding elements to first array
def checkconv(a, b, n) :
 
    c = [0]*n; flag = 0;
 
    # Create a temporary array c[]
    # which contains the difference
    # of the array elements
    for i in range(n) :
        c[i] = b[i] - a[i];
 
    # Create a vector pair for all non zero
    # elements of array c[n] with there index
    idxs = [];
    for i in range(n) :
        if (c[i] != 0) :
            idxs.append((i, c[i]));
 
    # Check If the index value differs by 1
    # and the difference value is same
    for i in range(len(idxs) - 1) :
        if (idxs[i + 1][0] - idxs[i][0] != 1
            or idxs[i + 1][1] != idxs[i][1]) :
            flag = 1;
            break;
 
    return not flag;
 
# Function to calculate the value of K
def diffofarrays(a, b, n) :
    c = [0] * n;
    ans = 0;
     
    for i in range(n) :
        c[i] = b[i] - a[i];
         
    for i in range(n) :
        if (c[i] != 0) :
            ans = c[i];
            break;
     
    return ans;
 
# Driver code
if __name__ == "__main__" :
 
    A = [ 3, 7, 1, 4, 0, 2, 2 ];
    B = [ 3, 7, 3, 6, 2, 2, 2 ];
    arr_size = len(A);
     
    if (checkconv(A, B, arr_size)) :
        print(diffofarrays(A, B, arr_size));
         
    else :
        print("NA");
 
# This code is contributed by AnkitRai01


C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
 
class GFG
{
    class pair
    {
        public int first, second;
        public pair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }
    }
     
// Function to Check if it is possible to
// convert a given array to another array
// by adding elements to first array
static bool checkconv(int []a, int []b, int n)
{
    int []c = new int[n];
    int flag = 0;
 
    // Create a temporary array c[]
    // which contains the difference
    // of the array elements
    for (int i = 0; i < n; i++)
    {
 
        c[i] = b[i] - a[i];
    }
 
    // Create a vector pair for all non zero
    // elements of array c[n] with there index
    List idxs = new List();
    for (int i = 0; i < n; i++)
    {
        if (c[i] != 0)
            idxs.Add(new pair(i, c[i]));
    }
 
    // Check If the index value differs by 1
    // and the difference value is same
    for (int i = 0; i < idxs.Count - 1; i++)
    {
        if (idxs[i + 1].first - idxs[i].first != 1
            || idxs[i + 1].second != idxs[i].second)
        {
            flag = 1;
            break;
        }
    }
 
    return flag == 1 ? false:true;
}
 
// Function to calculate the value of K
static int diffofarrays(int []a, int []b, int n)
{
    int []c = new int[n];
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        c[i] = b[i] - a[i];
    }
    for (int i = 0; i < n; i++)
    {
        if (c[i] != 0)
        {
            ans = c[i];
            break;
        }
    }
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int []A = { 3, 7, 1, 4, 0, 2, 2 };
    int []B = { 3, 7, 3, 6, 2, 2, 2 };
    int arr_size = A.Length;
 
    if (checkconv(A, B, arr_size))
    {
        Console.Write(diffofarrays(A, B, arr_size) +"\n");
    }
    else
        Console.Write("NA" +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2