Leetcode Problem 2007. Find Original Array From Doubled Array

2007. Find Original Array From Doubled Array

Leetcode Solutions

Sort + HashMap Approach

  1. Check if the length of changed is odd. If it is, return an empty array.
  2. Sort the changed array in non-decreasing order.
  3. Create a HashMap freq to store the frequency of each element in changed.
  4. Iterate over the sorted changed array and for each element num: a. If freq[num] is zero, continue to the next element. b. Decrease freq[num] by one. c. Check if freq[2 * num] is greater than zero. If not, return an empty array. d. Decrease freq[2 * num] by one and add num to the original array.
  5. Return the original array.
UML Thumbnail

Counting Sort Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...