2 methods||sort+2 pointer vs 1 pass seen Array||8ms Beats 97.34%
Intuition\n Describe your first thoughts on how to solve this problem. \n2 methods.\n1. Sorting nums + 2 pointer\n2. 1 pass using seen array (a unsigned char ar
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n2 methods.\n1. Sorting nums + 2 pointer\n2. 1 pass using seen array (a unsigned char array is enough)\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe 1st method needs sorting  O(n log n) time which is a very standard method; then use 2-pointer to find the answer.\n\nThe 2nd method use a uint8_t array instead of the slow C++ unordered_map or unordered_set.\nThe bit manipulation trick is so: if negative x is seen set seen[-x]|=2, if positive x is seen set seen[x]|=1. seen[x]==3 $\\iff$ both x, -x are seen.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n\\log n)$$ vs $$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$O(1)$ vs $$O(n)$$\n# C++ sort+ 2 pointer||11 ms Beats 94.04%\n```\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        int n=nums.size(), l=0, r=n-1;\n        sort(nums.begin(), nums.end());\n        while(l<r && nums[l]!=-nums[r]){\n            if (-nums[l]>nums[r]) l++;\n            else r--;\n\n        }\n        return (l<r) ?nums[r]:-1;\n          \n    }\n};\n```\n# Code using seen array||8ms Beats 97.34%\n```\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        uint8_t seen[1001]={0};\n        int k=-1;\n        for(int x:nums){\n            if (x<0) {\n                x=-x;\n                seen[x]|=2;\n            }\n            else seen[x]|=1;\n            if (seen[x]==3 && x>k) k=x;\n        }\n        return k;\n    }\n};\n```
['Array', 'Two Pointers', 'Bit Manipulation', 'Sorting', 'C++']