Beats 100% of users with C++|| Using Map and Two Pointer || Faster Solution || Easy to Understand ||
Abhiraj Pratap Singh\n\n---\n\n# if you like the approach please upvote it\n\n---\n\n# Intuition\n- The problem appears to involve finding the maximum value k s
# Abhiraj Pratap Singh\n\n---\n\n# if you like the approach please upvote it\n\n---\n\n# Intuition\n- The problem appears to involve finding the maximum value k such that both k and -k exist in the given vector.\n\n---\n\n![1713037055032.png](https://assets.leetcode.com/users/images/22962e38-74c4-48c5-9c08-c40e4d8b9bef_1714669404.123081.png)\n\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Use an ordered map to count the occurrences of each number in the vector.\n2. Iterate through the map:\n    - For each element it, check if its negative counterpart -it exists in the map and it is positive.\n    - If both it and -it exist, update the large variable to it.\n3. Return the maximum value found, which represents the maximum k such that both k and -k exist.\n4. If no such k is found, return -1.\n\n---\n\n# Complexity\n- **Time complexity :**\n    - Constructing the map takes O(nlogn) time, where n is the number of elements in the vector.\n    - Iterating through the map takes O(n) time.\n    - Overall, the time complexity is O(nlogn).\n\n---\n\n- **Space complexity :**\n    - We use O(n) extra space for the map.\n    - Overall, the space complexity is O(n).\n\n---\n\n\n# Code\n```\nclass Solution {\npublic:\n    // Using ordered Map\n    int findMaxK(vector<int>& nums) \n    {\n        map<int,int>mp;\n        int large=0;\n        for(int i=0;i<nums.size();i++)\n            mp[nums[i]]++;\n        for(auto it : mp)\n        {\n            if(it.first>0)\n            {\n                if(mp[(it.first)*-1])\n                    large=it.first;\n            }\n        }\n        if(large!=0)\n            return large;\n        return -1;\n    }\n};\n```\n#code\n```\nclass Solution {\npublic:\n    //Using Sorting then Two Pointer Appraoch\n    int findMaxK(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        int i=0;\n        int j=nums.size()-1;\n        while(i<j){\n            \n            if(-nums[i]==nums[j]) return nums[j];\n            else if(-nums[i]>nums[j]) i++;\n            else j--;\n        }\n        return -1;\n    }\n};\n```
['Hash Table', 'Two Pointers', 'Ordered Map', 'Sorting', 'C++']