Find Original Array From Doubled Array - Complete Solution Guide
Find Original Array From Doubled Array is LeetCode problem 2007, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. Given an array changed , return original if changed is a doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order . Example 1: Input: changed = [1,3,4,2,6,8] Output: [1,3,4] Explanation: One possible original array could be [1,3,4]: - Twice the value
Detailed Explanation
The problem asks us to determine if a given array `changed` is a doubled array, meaning it was created by taking an original array `original`, appending each element's double to it, and then shuffling the result. If `changed` is a doubled array, we should return the `original` array. If not, we should return an empty array. The order of elements in `original` doesn't matter. For example, if `changed = [1, 3, 4, 2, 6, 8]`, a possible `original` is `[1, 3, 4]` because 2*1=2, 2*3=6, and 2*4=8, and all these elements are present in `changed`.
Solution Approach
The provided solution first checks if the length of `changed` is even. If not, it returns an empty array. Then, it uses a hash table (Counter in Python, HashMap in Java/C++, custom HashTable in C) to count the occurrences of each number in `changed`. Next, it sorts the `changed` array. The solution then iterates through the sorted `changed` array. For each number, it checks if its count is greater than 0. If it is, the solution decrements the count of that number and checks if its double is also present with a count greater than 0. If the double exists, its count is also decremented, and the original number is added to the `original` array. If the double doesn't exist, it implies `changed` is not a doubled array, so an empty array is returned.
Step-by-Step Algorithm
- Step 1: Check if the length of `changed` is even. If it's odd, return an empty array.
- Step 2: Create a hash table (or counter) to store the frequency of each number in `changed`.
- Step 3: Sort the `changed` array.
- Step 4: Iterate through the sorted `changed` array.
- Step 5: For each number, check if its count in the hash table is greater than 0.
- Step 6: If the count is greater than 0, decrement the count. Calculate the double of the current number.
- Step 7: Check if the double exists in the hash table and has a count greater than 0.
- Step 8: If the double exists, decrement its count and add the original number to the `original` array.
- Step 9: If the double doesn't exist, return an empty array.
- Step 10: If the loop completes without returning an empty array, return the `original` array.
Key Insights
- Insight 1: The length of the `changed` array must be even for it to be a doubled array because each element in `original` contributes two elements to `changed` (itself and its double).
- Insight 2: Sorting the `changed` array simplifies the search for doubled elements. By iterating through the sorted array, we can efficiently check if the doubled value exists and remove both from consideration using a counter.
- Insight 3: Using a hash table (or counter) to keep track of the frequency of each number allows for efficient checking and removal of elements.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Greedy, Sorting.
Companies
Asked at: Accenture, Verily.