100% Faster || 3 Approaches || Easy to Understand || Short & Concise
Intuition\nWe need to find the largest positive integer k such that its negative counterpart -k exists in the given array. To achieve this, we can iterate throu
# Intuition\nWe need to find the largest positive integer k such that its negative counterpart -k exists in the given array. To achieve this, we can iterate through the array and keep track of the largest positive integer k encountered so far for which -k also exists in the array.\n\n# Approach: Brute Force\nLet\'s dive into the Naruto-inspired explanation for this approach:\n\nImagine you\'re on a mission to find the strongest ninja in the village, someone whose power level is unmatched, and who has a counterpart with an equal but opposite allegiance.\n\n1. **Setting Out on the Mission**:\n   - Equipped with your mission scroll (`vector<int>& nums`), you set out to explore the village and encounter various ninjas along the way.\n\n2. **Surveying the Village**:\n   - As you traverse the village, you encounter each ninja one by one (`for(int num : nums)`), carefully observing their power levels.\n\n3. **Identifying Potential Candidates**:\n   - As you meet each ninja, you evaluate whether they have the potential to be the strongest ninja (`if(num > maxi)`).\n   - However, being the strongest ninja isn\'t enough; they must also have a counterpart with an opposite allegiance somewhere in the village.\n\n4. **Consulting the Scroll**:\n   - To determine if a ninja\'s counterpart exists, you consult your mission scroll (`vector<int>& nums`) and search for their opposite counterpart.\n   - Using your keen ninja senses, you scan through the scroll, looking for the counterpart with an opposite power level (`-num`).\n\n5. **Claiming Victory**:\n   - After meticulously scanning through the entire village and evaluating each ninja\'s strength and allegiance, you finally identify the strongest ninja (`maxi`).\n   - With the mission accomplished, you proudly return to the village with news of your success.\n\nIn this approach, you dynamically search through the village as you encounter each ninja, using your mission scroll to determine if a counterpart exists. It\'s like being a skilled ninja scout, using your agility and perception to identify the strongest ninja amidst the village\'s inhabitants.\n\n# Complexity\n- Time complexity: O(n * n)\n\n- Space complexity: O(1)\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        int maxi = -1;\n        for(int num : nums) {\n            if(num > maxi && find(begin(nums), end(nums), -num) != end(nums)) {\n                maxi = num;\n            }\n        }\n\n        return maxi;\n    }\n};\n```\n```java []\nclass Solution {\n    public int findMaxK(int[] nums) {\n        int maxi = -1;\n        for(int num : nums) {\n            if(num > maxi && contains(nums, -num)) {\n                maxi = num;\n            }\n        }\n\n        return maxi;\n    }\n\n    public boolean contains(int[] nums, int num) {\n        for(int i : nums) {\n            if(i == num) {\n                return true;\n            }\n        }\n\n        return false;\n    }\n}\n```\n```python []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        maxi = -1\n        for num in nums :\n            if num > maxi and -num in nums :\n                maxi = num\n\n        return maxi\n```\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar findMaxK = function(nums) {\n    let maxi = -1;\n    for(let num of nums) {\n        if(num > maxi && nums.includes(-num)) {\n            maxi = num;\n        }\n    }\n\n    return maxi;\n};\n```\n# Approach: Two Pointers\nLet\'s explain the approach in a Naruto-inspired way!\n\nImagine you\'re facing an array of ninjas, each with their own power level (represented by the elements of the array). Your mission is to find the most powerful ninja `maxi` who has a counterpart with exactly the same power but opposite allegiance (positive vs. negative).\n\n1. **Sorting the Array**:\n   - Before diving into battle, it\'s crucial to arrange the ninjas in ascending order of their power levels. This way, you\'ll have a clear view of the battlefield and can strategize better.\n\n2. **Setting Up the Battle Formation**:\n   - You take your position at the center of the battlefield, ready to face off against the opposing forces. You set up two allies, one on your left (`left`) and one on your right (`right`), representing the two ends of the sorted array.\n\n3. **Engaging in Combat**:\n   - You begin your search for the most powerful ninja `maxi` by inspecting the pair of ninjas at your allies\' positions.\n   - If the ninja on your left has greater power than the ninja on your right, you focus your attention on the left side and move your ally towards the right.\n   - Conversely, if the ninja on your right has greater power, you shift your focus to the right side and move your ally towards the left.\n   - You continue this back-and-forth movement, gradually narrowing down the search range while keeping an eye out for a ninja whose power matches your ally\'s but with opposite allegiance.\n   - If you encounter such a pair, you\'ve found `maxi`! You signal victory and end the battle.\n\n4. **Returning from Battle**:\n   - If you\'ve searched through the entire battlefield without finding a matching pair, you return in disappointment, unable to locate `maxi`.\n\nThis approach is efficient because it takes advantage of the sorted order of the array, allowing for a systematic search while minimizing unnecessary comparisons. It\'s like navigating a battlefield strategically, focusing your efforts where they\'re most likely to yield results.\n\n# Complexity\n- Time complexity: O(n * logn)\n\n- Space complexity: O(1)\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        sort(begin(nums), end(nums));\n        int left = 0, right = nums.size() - 1;\n        \n        while(left < right && nums[left] < 0 && nums[right] > 0) {\n            if(abs(nums[left]) == nums[right]) {\n                return nums[right];\n            } else if(abs(nums[left]) > nums[right]) {\n                left++;\n            } else {\n                right--;\n            }\n        }\n\n        return -1;\n    }\n};\n```\n```java []\nclass Solution {\n    public int findMaxK(int[] nums) {\n        Arrays.sort(nums);\n        int left = 0, right = nums.length - 1;\n\n        while (left < right && nums[left] < 0 && nums[right] > 0) {\n            if (Math.abs(nums[left]) == nums[right]) {\n                return nums[right];\n            } else if (Math.abs(nums[left]) > nums[right]) {\n                left++;\n            } else {\n                right--;\n            }\n        }\n\n        return -1;\n    }\n}\n```\n```python []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        nums.sort()\n        left, right = 0, len(nums) - 1\n\n        while left < right and nums[left] < 0 and nums[right] > 0:\n            if abs(nums[left]) == nums[right]:\n                return nums[right]\n            elif abs(nums[left]) > nums[right]:\n                left += 1\n            else:\n                right -= 1\n\n        return -1\n```\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar findMaxK = function(nums) {\n    nums.sort((a, b) => a - b);\n    let left = 0, right = nums.length - 1;\n\n    while (left < right && nums[left] < 0 && nums[right] > 0) {\n        if (Math.abs(nums[left]) === nums[right]) {\n            return nums[right];\n        } else if (Math.abs(nums[left]) > nums[right]) {\n            left++;\n        } else {\n            right--;\n        }\n    }\n\n    return -1;\n};\n```\n# Approach: Hash Table\nLet\'s explore the approach used in the code in a Naruto-inspired way:\n\nImagine you\'re embarking on a quest to find the mightiest ninja in the land, whose power level is unmatched and who has an equal but opposite counterpart.\n\n1. **Preparing for the Journey**:\n   - Before setting out, you gather intelligence and information about the ninjas scattered across the land. You create a special scroll (`unordered_set<int> st`) to record the power levels of all the ninjas you encounter.\n\n2. **Embarking on the Quest**:\n   - Armed with your scroll, you traverse the land, encountering ninjas one by one (`for(int num : nums)`).\n   - As you encounter each ninja, you add their power level to your scroll (`st.insert(num)`), ensuring that you have a record of all the ninjas you\'ve seen.\n\n3. **Searching for the Mighty Ninja**:\n   - With your scroll in hand, you begin your search for the mightiest ninja `maxi`.\n   - You carefully examine each ninja you encounter, comparing their power level to the current mightiest ninja `maxi`.\n   - However, you\'re not just looking for any powerful ninja; you\'re searching for one whose counterpart, with opposite allegiance, also exists in the land.\n   - You consult your scroll (`st`) to see if the counterpart of the current ninja exists. If it does, and the current ninja\'s power level surpasses the current mightiest ninja `maxi`, you update `maxi` with the new ninja\'s power level.\n\n4. **Claiming Victory**:\n   - After traversing the entire land and encountering countless ninjas, you finally stumble upon the mightiest ninja `maxi`, whose power level is unmatched, and whose counterpart exists in the land.\n   - You proudly declare victory and return triumphant from your quest.\n\nThis approach is efficient because it leverages the power of the scroll (`unordered_set<int> st`) to keep track of encountered ninjas and quickly determine if a ninja\'s counterpart exists. It\'s like embarking on a quest armed with knowledge and resources, ensuring a successful journey to find the mightiest ninja in the land.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        unordered_set<int> st;\n        int maxi = -1;\n        for(int num : nums) {\n            st.insert(num);\n        }\n\n        for(int num : nums) {\n            if(num > maxi && st.find(-num) != st.end()) {\n                maxi = num;\n            }\n        }\n\n        return maxi;\n    }\n};\n```\n```java []\nclass Solution {\n    public int findMaxK(int[] nums) {\n        Set<Integer> st = new HashSet<>();\n        int maxi = -1;\n        for (int num : nums) {\n            st.add(num);\n        }\n\n        for (int num : nums) {\n            if (num > maxi && st.contains(-num)) {\n                maxi = num;\n            }\n        }\n\n        return maxi;\n    }\n}\n```\n```python []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        st = set(nums)\n        maxi = -1\n        for num in nums:\n            if num > maxi and -num in st:\n                maxi = num\n                \n        return maxi\n```\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar findMaxK = function(nums) {\n    const set = new Set(nums);\n    let maxi = -1;\n    for (let num of nums) {\n        if (num > maxi && set.has(-num)) {\n            maxi = num;\n        }\n    }\n    return maxi\n};\n```\n![Team 7 Naruro OG manga panel.png](https://assets.leetcode.com/users/images/b19ba454-6389-4378-83ac-e083cdcba542_1714611065.2804859.png)\n\n
['Array', 'Hash Table', 'Two Pointers', 'C++', 'Java', 'Python3', 'JavaScript']