## Find Original Array from a Double Array in JavaIt is a problem frequently asked in interviews of top IT companies like 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 StatementGiven 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 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 We can also shuffle the original array as ## Example 2:Let's consider another array 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. ## Note: If an array contains a single element, changed is not a doubled array.## Solution to the Problem- First, sort the given array in ascending order.
- Create a queue data structure. Initially queue is empty.
- Create another array for storing the resultant array.
- Repeat the following step until the queue becomes empty.
- Insert twice of the i
^{th}element in the front of the queue. - Pick the next element of the given array and compare it with the i
^{th}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.
- Insert twice of the i
- Print the resultant array.
Let's implement the above steps in a Java program. ## Java Program to Find the Original Array from a Doubled ArrayThe problem can be solved using Queue and HashMap data structure. ## Using HashMapThe above problem can be solved by using the HashMap.
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 AnalysisTime complexity of the above solution is |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]