从双倍数组中查找原始数组的元素
- 给定一个由2*N整数组成的数组arr[] ,它包含所有元素以及另一个数组的两倍值,比如A[] ,任务是找到数组A[] 。
例子:
Input: arr[] = {4, 1, 18, 2, 9, 8}
Output: 1 4 9
Explanation:
After taking double values of 1, 4, and 9 then adding them to the original array, All elements of the given array arr[] are obtained
Input: arr[] = {4, 1, 2, 2, 8, 2, 4, 4}
Output: 1 2 2 4
方法:给定的问题可以通过计算HashMap数组元素中数组元素的频率来解决,可以观察到,数组中最小的元素总是原始数组的一部分,因此可以包含在结果列表res[] 。具有最小元素双精度值的元素将是不属于原始数组的重复元素,因此可以从映射中减少其频率。可以按照以下步骤解决问题:
- 按升序对给定数组arr[]进行排序
- 遍历数组元素并将数字及其频率存储在哈希图中
- 创建一个结果列表res[]来存储原始列表中存在的元素
- 添加结果列表中的第一个元素并减少具有第一个元素的 double 值的元素的频率。
- 遍历数组并检查地图中每个元素的频率:
- 如果频率大于 0,则在结果列表中添加元素并减少频率。
- 否则,跳过该元素并继续前进,因为它是一个双精度值,而不是原始数组的一部分。
- 完成上述步骤后,打印列表res[]中的元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the original array
// from the doubled array
vector findOriginal(vector& arr)
{
// Stores the numbers and
// their frequency
map numFreq;
// Add number with their frequencies
// in the hashmap
for (int i = 0; i < arr.size(); i++) {
numFreq[arr[i]]++;
}
// Sort the array
sort(arr.begin(), arr.end());
// Initialize an arraylist
vector res;
for (int i = 0; i < arr.size(); i++) {
// Get the frequency of the number
int freq = numFreq[arr[i]];
if (freq > 0) {
// Element is of original array
res.push_back(arr[i]);
// Decrement the frequency of
// the number
numFreq[arr[i]]--;
int twice = 2 * arr[i];
// Decrement the frequency of
// the number having double value
numFreq[twice]--;
}
}
// Return the resultant string
return res;
}
// Driver Code
int main()
{
vector arr = { 4, 1, 2, 2, 2, 4, 8, 4 };
vector res = findOriginal(arr);
// Print the result list
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the original array
// from the doubled array
public static List
findOriginal(int[] arr)
{
// Stores the numbers and
// their frequency
Map numFreq
= new HashMap<>();
// Add number with their frequencies
// in the hashmap
for (int i = 0; i < arr.length; i++) {
numFreq.put(
arr[i],
numFreq.getOrDefault(arr[i], 0)
+ 1);
}
// Sort the array
Arrays.sort(arr);
// Initialize an arraylist
List res = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
// Get the frequency of the number
int freq = numFreq.get(arr[i]);
if (freq > 0) {
// Element is of original array
res.add(arr[i]);
// Decrement the frequency of
// the number
numFreq.put(arr[i], freq - 1);
int twice = 2 * arr[i];
// Decrement the frequency of
// the number having double value
numFreq.put(
twice,
numFreq.get(twice) - 1);
}
}
// Return the resultant string
return res;
}
// Driver Code
public static void main(String[] args)
{
List res = findOriginal(
new int[] { 4, 1, 2, 2, 2, 4, 8, 4 });
// Print the result list
for (int i = 0; i < res.size(); i++) {
System.out.print(
res.get(i) + " ");
}
}
}
Python3
# Python program for the above approach
# Function to find the original array
# from the doubled array
def findOriginal(arr):
# Stores the numbers and
# their frequency
numFreq = {}
# Add number with their frequencies
# in the hashmap
for i in range(0, len(arr)):
if (arr[i] in numFreq):
numFreq[arr[i]] += 1
else:
numFreq[arr[i]] = 1
# Sort the array
arr.sort()
# Initialize an arraylist
res = []
for i in range(0, len(arr)):
# Get the frequency of the number
freq = numFreq[arr[i]]
if (freq > 0):
# Element is of original array
res.append(arr[i])
# Decrement the frequency of
# the number
numFreq[arr[i]] -= 1
twice = 2 * arr[i]
# Decrement the frequency of
# the number having double value
numFreq[twice] -= 1
# Return the resultant string
return res
# Driver Code
arr = [4, 1, 2, 2, 2, 4, 8, 4]
res = findOriginal(arr)
# Print the result list
for i in range(0, len(res)):
print(res[i], end=" ")
# This code is contributed by _Saurabh_Jaiswal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find the original array
// from the doubled array
public static List findOriginal(int[] arr)
{
// Stores the numbers and
// their frequency
Dictionary numFreq = new Dictionary();
// Add number with their frequencies
// in the hashmap
for (int i = 0; i < arr.Length; i++) {
if(numFreq.ContainsKey(arr[i])){
numFreq[arr[i]] = numFreq[arr[i]] + 1;
}else{
numFreq.Add(arr[i], 1);
}
}
// Sort the array
Array.Sort(arr);
// Initialize an arraylist
List res = new List();
for (int i = 0; i < arr.Length; i++) {
// Get the frequency of the number
int freq = numFreq[arr[i]];
if (freq > 0) {
// Element is of original array
res.Add(arr[i]);
// Decrement the frequency of
// the number
numFreq[arr[i]] = freq - 1;
int twice = 2 * arr[i];
// Decrement the frequency of
// the number having double value
numFreq[twice] = numFreq[twice] - 1;
}
}
// Return the resultant string
return res;
}
// Driver Code
public static void Main()
{
List res = findOriginal(new int[] { 4, 1, 2, 2, 2, 4, 8, 4 });
// Print the result list
for (int i = 0; i < res.Count; i++) {
Console.Write(res[i] + " ");
}
}
}
// This code is contributed by gfgking.
Javascript
输出:
1 2 2 4
时间复杂度: O(N*log N)
辅助空间: O(N)