# Find Original Array from a Double Array in Java

It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, HCL, IBM, and Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to find original array from a doubled array in Java with different approaches and logic. Also, we will create Java programs for the same. In this problem, an integer array original is transformed into a doubled array changed by appending twice the value of every element in original and then randomly shuffling the resulting array.

## Problem Statement

Given an array changed, return original if changed is doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order.

Let's understand the problem through an example.

### Example 1:

Suppose, we have given a doubled array [1, 3, 4, 2, 6, 8]. Check if the array is an original array or not.

In order to check if the given array is doubled array or not, multiply each element of the array by 2. If the resultant elements exist in the array, means given array is a doubled array and the original array consist the elements that are multiplied by 2.

Twice of the first element: 1*2 = 2 (exist in the given array)

Twice of the second element: 3*2 = 6 (exist in the given array)

Twice of the third element: 4*2 = 8 (exist in the given array)

We observe that the twice of the first three elements of the given array already exist in the array, so it is a doubled array and the original elements of the array are [1, 3, 4].

We can also shuffle the original array as [4, 3, 1] or [3, 1, 4].

### Example 2:

Let's consider another array [4, 2, 0, 1].

Twice the first element 4*2 = 8 (not exist in the given array)

Twice the second element 2*2 = 4 (exist in the given array)

Twice the third element 0*2 = 0 (exist in the given array)

Twice the fourth element 1*2 = 2 (exist in the given array)

We observe that twice of the three elements exist in the given array. Twice of the first element (8) does not exist in the given array. Hence, the original array will be an empty array because changed is not a doubled array.

## Solution to the Problem

1. First, sort the given array in ascending order.
2. Create a queue data structure. Initially queue is empty.
3. Create another array for storing the resultant array.
4. Repeat the following step until the queue becomes empty.
1. Insert twice of the ith element in the front of the queue.
2. Pick the next element of the given array and compare it with the ith element of the queue.
• If the element already exists, remove that element form the queue.
• Else, insert the element in the resultant array and move to the next element.
5. Print the resultant array.

Let's implement the above steps in a Java program.

## Java Program to Find the Original Array from a Doubled Array

The problem can be solved using Queue and HashMap data structure.

### Using HashMap

The above problem can be solved by using the HashMap.

FindOriginalArray.java

Output:

```The original array is: 1 2 2 4
```

The same problem can also be solving by using the queue data structure. The logic is a bit different form the above. The following code snippet shows the logic for solving the problem using queue.

## Complexity Analysis

Time complexity of the above solution is O(nlogn) and the space complexity is O(n).

## Help Others, Please Share   