- Given an array arr[] of 2*N integers such that it consists of all elements along with the twice values of another array, say A[], the task is to find the array A[].
Examples:
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
Approach: The given problem can be solved by counting the frequency of array elements in the HashMap array elements and observation can be made that, the smallest element in the array will always be a part of the original array, therefore it can be included in the result list res[]. The element with a double value of the smallest element will be the duplicate element that is not part of the original array so its frequency can be reduced from the map. Below steps can be followed to solve the problem:
- Sort the given array arr[] in ascending order
- Iterate through the array elements and store the numbers and their frequencies in a hashmap
- Create a result list res[] to store the elements present in the original list
- Add the first element in the result list and reduce the frequency of the element which has a double value of the first element.
- Traverse the array and check for the frequency of every element in the map:
- If the frequency is greater than 0, then add the element in the result list and decrement the frequency.
- Otherwise, skip the element and move ahead because it is a double value and not a part of the original array.
- After completing the above steps, print the elements in the list res[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > findOriginal(vector< int >& arr)
{
map< int , int > numFreq;
for ( int i = 0; i < arr.size(); i++) {
numFreq[arr[i]]++;
}
sort(arr.begin(), arr.end());
vector< int > res;
for ( int i = 0; i < arr.size(); i++) {
int freq = numFreq[arr[i]];
if (freq > 0) {
res.push_back(arr[i]);
numFreq[arr[i]]--;
int twice = 2 * arr[i];
numFreq[twice]--;
}
}
return res;
}
int main()
{
vector< int > arr = { 4, 1, 2, 2, 2, 4, 8, 4 };
vector< int > res = findOriginal(arr);
for ( int i = 0; i < res.size(); i++) {
cout << res[i] << " " ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static List<Integer>
findOriginal( int [] arr)
{
Map<Integer, Integer> numFreq
= new HashMap<>();
for ( int i = 0 ; i < arr.length; i++) {
numFreq.put(
arr[i],
numFreq.getOrDefault(arr[i], 0 )
+ 1 );
}
Arrays.sort(arr);
List<Integer> res = new ArrayList<>();
for ( int i = 0 ; i < arr.length; i++) {
int freq = numFreq.get(arr[i]);
if (freq > 0 ) {
res.add(arr[i]);
numFreq.put(arr[i], freq - 1 );
int twice = 2 * arr[i];
numFreq.put(
twice,
numFreq.get(twice) - 1 );
}
}
return res;
}
public static void main(String[] args)
{
List<Integer> res = findOriginal(
new int [] { 4 , 1 , 2 , 2 , 2 , 4 , 8 , 4 });
for ( int i = 0 ; i < res.size(); i++) {
System.out.print(
res.get(i) + " " );
}
}
}
|
Python3
def findOriginal(arr):
numFreq = {}
for i in range ( 0 , len (arr)):
if (arr[i] in numFreq):
numFreq[arr[i]] + = 1
else :
numFreq[arr[i]] = 1
arr.sort()
res = []
for i in range ( 0 , len (arr)):
freq = numFreq[arr[i]]
if (freq > 0 ):
res.append(arr[i])
numFreq[arr[i]] - = 1
twice = 2 * arr[i]
numFreq[twice] - = 1
return res
arr = [ 4 , 1 , 2 , 2 , 2 , 4 , 8 , 4 ]
res = findOriginal(arr)
for i in range ( 0 , len (res)):
print (res[i], end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static List< int > findOriginal( int [] arr)
{
Dictionary< int , int > numFreq = new Dictionary< int , int >();
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);
}
}
Array.Sort(arr);
List< int > res = new List< int >();
for ( int i = 0; i < arr.Length; i++) {
int freq = numFreq[arr[i]];
if (freq > 0) {
res.Add(arr[i]);
numFreq[arr[i]] = freq - 1;
int twice = 2 * arr[i];
numFreq[twice] = numFreq[twice] - 1;
}
}
return res;
}
public static void Main()
{
List< int > res = findOriginal( new int [] { 4, 1, 2, 2, 2, 4, 8, 4 });
for ( int i = 0; i < res.Count; i++) {
Console.Write(res[i] + " " );
}
}
}
|
Javascript
<script>
function findOriginal(arr)
{
let numFreq = new Map();
for (let i = 0; i < arr.length; i++) {
if (numFreq.has(arr[i])) {
numFreq.set(arr[i], numFreq.get(arr[i]) + 1);
} else {
numFreq.set(arr[i], 1);
}
}
arr.sort((a, b) => a - b);
let res = [];
for (let i = 0; i < arr.length; i++) {
let freq = numFreq.get(arr[i]);
if (freq > 0) {
res.push(arr[i]);
numFreq.set(arr[i], numFreq.get(arr[i]) - 1);
let twice = 2 * arr[i];
numFreq.set(twice, numFreq.get(twice) - 1);
}
}
return res;
}
let arr = [4, 1, 2, 2, 2, 4, 8, 4];
let res = findOriginal(arr);
for (let i = 0; i < res.length; i++) {
document.write(res[i] + " " );
}
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)