要添加到数组 A 中的 N-1 个元素以使数组 B 的元素出现在 A 中的最小可能整数
给定两个长度为 N 的数组A[]和长度为N -1的B[] ,任务是找到最小的正整数X添加到A[]的每个元素中,除了一个元素,使得数组的所有元素B[]存在于数组A[]中。假设找到X总是可能的。
例子:
Input: A[] = {1, 4, 3, 8}, B[] = {15, 8, 11}
Output: 7
Explanation: Adding 7 to every elements of the array A except 3 will give all the elements of array B
Input: A[] = {4, 8}, B[] = {10}
Output: 2
Explanation: Adding 2 to 8 gives 10.
方法:这个想法是使用贪婪方法。
- 对数组A[]和B[]进行排序
- 通过观察可以看出, X的值可以是B[0] – A[0]或B[0] – A[1] 。
- 因此,不考虑A[0]或A[1] ,并将X添加到其余元素中。
- 通过遍历数组检查两个值是否有效,如果发现两个X值都有效,则选择最小值并打印。
以下是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the smallest positive integer
// X such that X is added to every element of A
// except one element to give array B
void findVal(int A[], int B[], int N)
{
// Stores the unique elements of array A
unordered_set s;
for (int i = 0; i < N; i++) {
// Insert A[i] into the set s
s.insert(A[i]);
}
// Sort array A[]
sort(A, A + N);
// Sort array B[]
sort(B, B + N - 1);
// Assume X value as B[0] - A[1]
int X = B[0] - A[1];
// Check if X value assumed is negative or 0
if (X <= 0)
// Update the X value to B[0] - A[0]
X = B[0] - A[0];
else {
for (int i = 0; i < N - 1; i++) {
// If assumed value is wrong
if (s.count(B[i] - X) == 0) {
// Update X value
X = B[0] - A[0];
break;
}
}
}
cout << X << endl;
}
// Driver Code
int main()
{
int A[] = { 1, 4, 3, 8 };
int B[] = { 15, 8, 11 };
int N = sizeof(A) / sizeof(A[0]);
findVal(A, B, N);
return 0;
}
Java
// Java implementation for the above approach
import java.util.*;
public class GFG
{
// Function to find the smallest positive integer
// X such that X is added to every element of A
// except one element to give array B
static void findVal(int []A, int []B, int N)
{
// Stores the unique elements of array A
HashSet s = new HashSet();
for (int i = 0; i < N; i++) {
// Insert A[i] into the set s
s.add(A[i]);
}
// Sort array A[]
Arrays.sort(A);
// Sort array B[]
Arrays.sort(B);
// Assume X value as B[0] - A[1]
int X = B[0] - A[1];
// Check if X value assumed is negative or 0
if (X <= 0)
// Update the X value to B[0] - A[0]
X = B[0] - A[0];
else {
for (int i = 0; i < N - 1; i++) {
// If assumed value is wrong
if (s.contains(B[i] - X) == false) {
// Update X value
X = B[0] - A[0];
break;
}
}
}
System.out.println(X);
}
// Driver Code
public static void main(String args[])
{
int []A = { 1, 4, 3, 8 };
int []B = { 15, 8, 11 };
int N = A.length;
findVal(A, B, N);
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# Python 3 program for the above approach
# Function to find the smallest positive integer
# X such that X is added to every element of A
# except one element to give array B
def findVal(A, B, N):
# Stores the unique elements of array A
s = set()
for i in range(N):
# Insert A[i] into the set s
s.add(A[i])
# Sort array A[]
A.sort()
# Sort array B[]
B.sort()
# Assume X value as B[0] - A[1]
X = B[0] - A[1]
# Check if X value assumed is negative or 0
if (X <= 0):
# Update the X value to B[0] - A[0]
X = B[0] - A[0]
else:
for i in range(N - 1):
# If assumed value is wrong
if (B[i] - X not in s):
# Update X value
X = B[0] - A[0]
break
print(X)
# Driver Code
if __name__ == '__main__':
A = [1, 4, 3, 8]
B = [15, 8, 11]
N = len(A)
findVal(A, B, N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the smallest positive integer
// X such that X is added to every element of A
// except one element to give array B
static void findVal(int []A, int []B, int N)
{
// Stores the unique elements of array A
HashSet s = new HashSet();
for (int i = 0; i < N; i++) {
// Insert A[i] into the set s
s.Add(A[i]);
}
// Sort array A[]
Array.Sort(A);
// Sort array B[]
Array.Sort(B);
// Assume X value as B[0] - A[1]
int X = B[0] - A[1];
// Check if X value assumed is negative or 0
if (X <= 0)
// Update the X value to B[0] - A[0]
X = B[0] - A[0];
else {
for (int i = 0; i < N - 1; i++) {
// If assumed value is wrong
if (s.Contains(B[i] - X) == false) {
// Update X value
X = B[0] - A[0];
break;
}
}
}
Console.Write(X);
}
// Driver Code
public static void Main()
{
int []A = { 1, 4, 3, 8 };
int []B = { 15, 8, 11 };
int N = A.Length;
findVal(A, B, N);
}
}
// This code is contributed by Samim Hossain Mondal
Javascript
输出
7
时间复杂度: O(N log N)
辅助空间: O(N)