Beats 99.07% Time⌛ 98.70% Memory 💾🔥🔥 | Python, C++ 💻 | Clear Explanation📗
Beats 99.07% Time\u231B 98.70% Memory \uD83D\uDCBE\uD83D\uDD25\uD83D\uDD25 | Python, C++ \uD83D\uDCBB | Clear Explanation\uD83D\uDCD7\n## 1. Proof\n Describe yo
# Beats 99.07% Time\u231B 98.70% Memory \uD83D\uDCBE\uD83D\uDD25\uD83D\uDD25 | Python, C++ \uD83D\uDCBB | Clear Explanation\uD83D\uDCD7\n## 1. Proof\n<!-- Describe your first thoughts on how to solve this problem. -->\n### 1.1. Python3\n![image.png](https://assets.leetcode.com/users/images/11bf8ec1-ba8b-418f-9ae2-4a5d23b5702c_1714622105.9590352.png)\n### 1.2. C++\n![image.png](https://assets.leetcode.com/users/images/4501ad22-9d9b-42cd-916c-b402ca4554e6_1714623094.1484709.png)\n\n## 2. Approach\n**Sorting** & **Two pointers**\n\n## 3. Code (with explanation each line)\n```python3 []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int: # Two pointers\n        nums.sort() # Negative integer with max abs value (|-|) on the left, whereas the max positive integer (+) on the right\n        l, r = 0, len(nums) - 1 # left & right pointers -> We go from both ends to inside to get the max value\n        \n        while nums[l] < 0 and nums[r] > 0: # Ensure the left pointer always points to a negative integer (-) & right pointer always points to a positive integer (+)\n            neg, pos = abs(nums[l]), nums[r] # Get the absolute value of negative number (-), and positive number (+) -> for easier comparison\n            if neg == pos: # If abs of negative (|-|) matches positive (+) -> Found\n                return pos # Return the positive (+) number\n            elif neg < pos: # The positive number (+) is too large compared to the corresponding abs of negative number (|-|) which left pointer is pointing at\n                r -= 1 # Get a smaller positive number (+) by moving right pointer inside\n            else: # The abs of negative number (|-|) is too large compared to the corresponding positive number (+) which right pointer is pointing at\n                l += 1 # Get a smaller abs of negative number (|-|) by moving left pointer inside\n        \n        return -1 # Not found\n```\n```cpp []\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        int l = 0, r = nums.size() - 1;\n\n        while ((nums[l] < 0) && (nums[r] > 0)) {\n            int neg = abs(nums[l]), pos = nums[r];\n            if (neg == pos) {\n                return pos;\n            }\n            else if (neg < pos) {\n                r--;\n            }\n            else {\n                l++;\n            }\n        }\n        return -1;\n    }\n};\n```\n## 4. Complexity\n- Time complexity: $$O((N*logN) + N/2)$$ simplified to $$O(N*logN)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n### Upvote if you find this solution helpful, thank you \uD83E\uDD0D
['Two Pointers', 'Sorting', 'C++', 'Python3']