C++ Clear Solution with Queue | Beats 85% in Runtime and 79% in Memory
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n1. Sort nums[] and test
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort ```nums[]``` and test every possible K by ```nums[i] - nums[0]```\n2. Conduct early termination if ```ele``` is greater than the smallest numbers by ```twoK```\n3. Check all element has been paired by ```deq.size() == 0```\n# Complexity\n- Time complexity: $$O(N^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\nprivate:\n    bool recover(vector<int>& nums, int twoK, vector<int>& answer){\n\n        //invalid k\n        if(twoK % 2 == 1 || twoK == 0) return false;\n\n        deque<int> deq;\n        for(int ele : nums){\n            if(deq.size() > 0 && ele - twoK > deq.front()){\n                return false;\n            }else if(deq.size() > 0 && ele - twoK == deq.front()){\n                answer.push_back(deq.front() + (twoK/2));\n                deq.pop_front();\n            }else{\n                deq.push_back(ele);\n            }\n        }\n        return deq.size() == 0;\n    }\npublic:\n    vector<int> recoverArray(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        int n = nums.size();\n        for(int i = 0; i < n; ++i){\n            vector<int> answer;\n            if(recover(nums, nums[i]-nums[0], answer)){\n                return answer;\n            }\n        }\n        // should never be here\n        return vector<int>();\n    }\n};\n```\n![Upvote.jpeg](https://assets.leetcode.com/users/images/bd99339f-753c-4dac-ad68-72a53fef61c2_1716713364.632431.jpeg)\n
['Array', 'Queue', 'Sorting', 'C++']