通过将某个值 X 添加到数组 A 形成的数组 B 中查找缺失值
给定两个数组arr1[]和arr2[] ,大小分别为N和N – 1 。 arr2[]中的每个值都是通过向任何arr1[i]添加一个隐藏值X来获得N-1次,这意味着对于任何i , arr1[i]+X不包含在arr2[]中,这是缺失的arr2[]中的值。任务是找到隐藏值X和未从arr1[]中挑选的缺失值。
例子:
Input: arr1[] = {3, 6, 5, 10}, arr2[] = {17, 10, 13}
Output: Hidden = 7, Missing = 5
Explanation: The elements of the second array are obtained in the following way:
First element: (10 + 7) = 17, 10 is the element at index 4 of arr1[]
Second element: (3 + 7) = 10, 3 is the element at index 0 of arr1[]
Third element: (6 + 7) = 13, 6 is the element at index 1 of arr1[]
So, 7 is the hidden value and 5 is the missing value.
Input: arr1[] = {4, 1, 7}, arr2[] = {4, 10}
Output: Hidden = 3, Missing = 4
方法:这个问题可以通过使用来解决
- 以非降序对给定的数组进行排序。
- 创建一个无序映射来存储差异。
- 遍历数组直到N - 1并通过存储两个数组元素的差异来检查值。
- 设a = arr2[i] – arr1[i]和b = arr2[i] – arr1[i+1] 。
- 如果a!=b然后检查是否
- a > 0 ,然后将其存储在地图中。
- b > 0 ,然后将其存储在地图中。
- 否则检查 a > 0,然后将其存储在地图中。
- 遍历地图并找到隐藏的值并打印它。
- 通过添加隐藏值遍历arr1[]并检查它是否存在于arr2[]中,否则打印缺失值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the hidden
// and missing values
void findHiddenMissing(int arr1[], int arr2[], int N)
{
// Sorting both the arrays
sort(arr1, arr1 + N);
sort(arr2, arr2 + N - 1);
// Create a map
unordered_map mp;
// Traversing the arrays and
// checking for the values
for (int i = 0; i < N - 1; i++) {
// Variable to store the difference
// between the elements of arr2
// and arr1 in same indices
int a = arr2[i] - arr1[i];
// Variable to store the difference
// between the elements of arr2
// and arr1 next index
int b = arr2[i] - arr1[i + 1];
// If a is not equals to b
if (a != b) {
// If a is greater than 0
if (a > 0)
mp[a] += 1;
// If b is greater than 0
if (b > 0)
mp[b] += 1;
}
// If a is equal to b
else {
// If a is greater than 0
if (a > 0)
mp[a] += 1;
}
}
// To store the hidden value
int hidden;
// Iterate over the map and searching
// for the hidden value
for (auto it : mp) {
if (it.second == N - 1) {
hidden = it.first;
cout << "Hidden: " << it.first;
break;
}
}
cout << endl;
// Find the missing value
for (int i = 0; i < N; i++) {
if (arr1[i] + hidden != arr2[i]) {
cout << "Missing: " << arr1[i];
break;
}
}
}
// Driver Code
int main()
{
int arr1[] = { 3, 6, 5, 10 };
int arr2[] = { 17, 10, 13 };
int N = sizeof(arr1) / sizeof(arr1[0]);
findHiddenMissing(arr1, arr2, N);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
import java.util.HashMap;
class GFG {
// Function to find the hidden
// and missing values
public static void findHiddenMissing(int arr1[], int arr2[], int N)
{
// Sorting both the arrays
Arrays.sort(arr1);
Arrays.sort(arr2);
// Create a map
HashMap mp = new HashMap();
// Traversing the arrays and
// checking for the values
for (int i = 0; i < N - 1; i++)
{
// Variable to store the difference
// between the elements of arr2
// and arr1 in same indices
int a = arr2[i] - arr1[i];
// Variable to store the difference
// between the elements of arr2
// and arr1 next index
int b = arr2[i] - arr1[i + 1];
// If a is not equals to b
if (a != b) {
// If a is greater than 0
if (a > 0) {
if (mp.containsKey(a))
mp.put(a, mp.get(a) + 1);
else
mp.put(a, 1);
}
// If b is greater than 0
if (b > 0)
if (mp.containsKey(b))
mp.put(b, mp.get(b) + 1);
else
mp.put(b, 1);
}
// If a is equal to b
else {
// If a is greater than 0
if (a > 0)
if (mp.containsKey(a))
mp.put(a, mp.get(a) + 1);
else
mp.put(a, 1);
}
}
// To store the hidden value
int hidden = 0;
// Iterate over the map and searching
// for the hidden value
for (int it : mp.keySet()) {
if (mp.get(it) == N - 1) {
hidden = it;
System.out.println("Hidden: " + it);
break;
}
}
// Find the missing value
for (int i = 0; i < N; i++) {
if (arr1[i] + hidden != arr2[i]) {
System.out.println("Missing: " + arr1[i]);
break;
}
}
}
// Driver Code
public static void main(String args[]) {
int arr1[] = { 3, 6, 5, 10 };
int arr2[] = { 17, 10, 13 };
int N = arr1.length;
findHiddenMissing(arr1, arr2, N);
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python program for the above approach
# Function to find the hidden
# and missing values
def findHiddenMissing(arr1, arr2, N):
# Sorting both the arrays
arr1.sort()
arr2.sort()
# Create a map
mp = {}
# Traversing the arrays and
# checking for the values
for i in range(0, N - 1):
# Variable to store the difference
# between the elements of arr2
# and arr1 in same indices
a = arr2[i] - arr1[i]
# Variable to store the difference
# between the elements of arr2
# and arr1 next index
b = arr2[i] - arr1[i + 1]
# If a is not equals to b
if (a != b):
# If a is greater than 0
if (a > 0):
if not a in mp:
mp[a] = 1
else:
mp[a] += 1
# If b is greater than 0
if (b > 0):
if not b in mp:
mp[b] = 1
else:
mp[b] += 1
# If a is equal to b
else:
# If a is greater than 0
if (a > 0):
if not a in mp:
mp[a] = 1
else:
mp[a] += 1
# To store the hidden value
hidden = 0
# Iterate over the map and searching
# for the hidden value
for it in mp:
if (mp[it] == N - 1):
hidden = it
print("Hidden:", end=" ")
print(it)
break
# Find the missing value
for i in range(0, N):
if (arr1[i] + hidden != arr2[i]):
print("Missing:", end=" ")
print(arr1[i])
break
# Driver Code
if __name__ == "__main__":
arr1 = [3, 6, 5, 10]
arr2 = [17, 10, 13]
N = len(arr1)
findHiddenMissing(arr1, arr2, N)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
// Function to find the hidden
// and missing values
public static void findHiddenMissing(int []arr1, int []arr2, int N)
{
// Sorting both the arrays
Array.Sort(arr1);
Array.Sort(arr2);
// Create a map
Dictionary mp = new Dictionary();
// Traversing the arrays and
// checking for the values
for (int i = 0; i < N - 1; i++)
{
// Variable to store the difference
// between the elements of arr2
// and arr1 in same indices
int a = arr2[i] - arr1[i];
// Variable to store the difference
// between the elements of arr2
// and arr1 next index
int b = arr2[i] - arr1[i + 1];
// If a is not equals to b
if (a != b) {
// If a is greater than 0
if (a > 0) {
if (mp.ContainsKey(a))
mp[a] = mp[a] + 1;
else
mp.Add(a, 1);
}
// If b is greater than 0
if (b > 0)
if (mp.ContainsKey(b))
mp[b] = mp[b] + 1;
else
mp.Add(b, 1);
}
// If a is equal to b
else {
// If a is greater than 0
if (a > 0)
if (mp.ContainsKey(a))
mp[a] = mp[a] + 1;
else
mp.Add(a, 1);
}
}
// To store the hidden value
int hidden = 0;
// Iterate over the map and searching
// for the hidden value
foreach(int it in mp.Keys) {
if (mp[it] == N - 1) {
hidden = it;
Console.WriteLine("Hidden: " + it);
break;
}
}
// Find the missing value
for (int i = 0; i < N; i++) {
if (arr1[i] + hidden != arr2[i]) {
Console.WriteLine("Missing: " + arr1[i]);
break;
}
}
}
// Driver Code
public static void Main(string []args) {
int []arr1 = { 3, 6, 5, 10 };
int []arr2 = { 17, 10, 13 };
int N = arr1.Length;
findHiddenMissing(arr1, arr2, N);
}
}
// This code is contributed by rutvik_56.
Javascript
Hidden: 7
Missing: 5
时间复杂度: O(N)
辅助空间: O(N)