💯Faster✅💯Lesser✅2 Methods🧠Detailed Approach🎯Two Pointer🔥Python🐍Java☕C++😎
\uD83D\uDE80 Hi, I\'m Mohammed Raziullah Ansari, and I\'m excited to share 2 ways to solve this question with detailed explanation of each approach:\n\n# \uD83C
# \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 2 ways to solve this question with detailed explanation of each approach:\n\n# \uD83C\uDFAFProblem Explaination: \nYou are given an array of integers. Your task is to find the largest positive integer in the array such that its negative counterpart also exists in the array. If such a pair exists, return the largest positive integer; otherwise, return -1.\n\n### \uD83D\uDCE5Input:\n\n- An array of integers `nums`.\n- Each integer `nums[i]` represents an element of the array.\n\n### \uD83D\uDCE4Output:\n\n- An integer representing the largest positive integer satisfying the given condition. If no such integer exists, return -1.\n\n# \uD83D\uDD0D Methods To Solve This Problem:\nI\'ll be covering two different methods to solve this problem:\n1. Sorting\n2. Two Pointers\n\n# 1\uFE0F\u20E3 Sorting: \n\n### Approach\uD83E\uDD13:\n- Sort the array in ascending order.\n- Iterate through the sorted array from the end.\n- Return the first positive integer and its negative counterpart.\n### Complexity\uD83E\uDDD1\uD83C\uDFFB\u200D\uD83D\uDD2C:\n- \u23F0 Time complexity: O(n log n) due to sorting.\n- \uD83C\uDF0C Space Complexity: O(1)\n\n<!-- ### Algorithm\uD83E\uDD13:\n\n1. Initialize an empty list to store rectangles.\n2. Iterate through each cell in the grid:\n   - If a cell contains `1` and hasn\'t been visited:\n     - Initiate a DFS from that cell to explore the connected farmland.\n     - During DFS, keep track of the boundaries (min/max row and column) of the current region.\n     - Once DFS completes for a region, record the rectangle\'s coordinates.\n3. Implement the DFS function recursively or using a stack to traverse adjacent cells.\n4. Return the list of identified rectangles. -->\n\n<!-- # Complexity\n- \u23F1\uFE0F Time Complexity: `O(n)` where n is the number of pairs in the list.\n -->\n<!-- - \uD83D\uDE80 Space Complexity: `O(n)` for the HashSet. -->\n\n# Code\uD83D\uDC68\uD83C\uDFFB\u200D\uD83D\uDCBB:\n```Python []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        nums.sort()\n        n = len(nums)\n        for i in range(n-1, -1, -1):\n            if nums[i] > 0 and -nums[i] in nums:\n                return nums[i]\n        return -1  # If no such pair found\n```\n```Java []\nclass Solution {\n    public int findMaxK(int[] nums) {\n        Arrays.sort(nums);\n        int n = nums.length;\n        for (int i = n-1; i >= 0; i--) {\n            if (nums[i] > 0 && Arrays.binarySearch(nums, -nums[i]) >= 0) {\n                return nums[i];\n            }\n        }\n        return -1;  // If no such pair found\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int findMaxK(std::vector<int>& nums) {\n        std::sort(nums.begin(), nums.end());\n        int n = nums.size();\n        for (int i = n-1; i >= 0; i--) {\n            if (nums[i] > 0 && std::binary_search(nums.begin(), nums.end(), -nums[i])) {\n                return nums[i];\n            }\n        }\n        return -1;  // If no such pair found\n    }\n};\n```\n\n# 2\uFE0F\u20E3  Two Pointers:\n### Approach\uD83D\uDE0E:\n- Sort the array in ascending order.\n- Use two pointers, one starting from the beginning and one from the end.\n- Move the pointers towards each other until you find a positive integer with its negative counterpart.\n- Keep track of the largest positive integer found.\n### Complexity\uD83E\uDDD1\uD83C\uDFFB\u200D\uD83D\uDD2C:\n- \u23F0 Time complexity: O(n log n) due to sorting.\n- \uD83C\uDF0C Space Complexity: O(1)\n\n<!-- \n### Algorithm\uD83D\uDE0E:\n\n1. Initialize a queue (`queue`) to perform BFS and a set (`visited`) to keep track of visited cells.\n2. Iterate through each cell `(i, j)` in the grid:\n   - If `land[i][j]` is `1` and `(i, j)` is not visited:\n     - Initialize `min_row`, `min_col`, `max_row`, `max_col` to the current cell `(i, j)`.\n     - Enqueue `(i, j)` into the queue and mark it as visited.\n     - While the queue is not empty:\n       - Dequeue a cell `(cur_i, cur_j)` from the queue.\n       - Explore its four neighboring cells `(nx, ny)` (up, down, left, right).\n       - If `(nx, ny)` is within the grid bounds and `land[nx][ny]` is `1` and not visited:\n         - Enqueue `(nx, ny)`.\n         - Mark `(nx, ny)` as visited.\n         - Update `min_row`, `min_col`, `max_row`, `max_col` based on `(nx, ny)`.\n     - After BFS completes for the current region, record the rectangle `[min_row, min_col, max_row, max_col]`.\n3. Return the list of identified rectangles. -->\n\n<!-- # Complexity\n- \u23F1\uFE0F Time Complexity: `O(n)` where n is the number of pairs in the list.\n\n- \uD83D\uDE80 Space Complexity: `O(n)` for the two lists. -->\n\n# Code\uD83D\uDC69\uD83C\uDFFB\u200D\uD83D\uDCBB:\n```Python []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        nums.sort()\n        left, right = 0, len(nums) - 1\n        max_k = float(\'-inf\')\n        while left < right:\n            if nums[left] + nums[right] == 0:\n                max_k = max(max_k, nums[right])\n                left += 1\n                right -= 1\n            elif nums[left] + nums[right] < 0:\n                left += 1\n            else:\n                right -= 1\n        return max_k if max_k != float(\'-inf\') else -1  # If no such pair found\n```\n```Java []\nclass Solution {\n    public int findMaxK(int[] nums) {\n        Arrays.sort(nums);\n        int left = 0, right = nums.length - 1;\n        int maxK = Integer.MIN_VALUE;\n        while (left < right) {\n            int sum = nums[left] + nums[right];\n            if (sum == 0) {\n                maxK = Math.max(maxK, nums[right]);\n                left++;\n                right--;\n            } else if (sum < 0) {\n                left++;\n            } else {\n                right--;\n            }\n        }\n        return maxK != Integer.MIN_VALUE ? maxK : -1;  // If no such pair found\n    }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    int findMaxK(std::vector<int>& nums) {\n        std::sort(nums.begin(), nums.end());\n        int left = 0, right = nums.size() - 1;\n        int maxK = std::numeric_limits<int>::min();\n        while (left < right) {\n            int sum = nums[left] + nums[right];\n            if (sum == 0) {\n                maxK = std::max(maxK, nums[right]);\n                left++;\n                right--;\n            } else if (sum < 0) {\n                left++;\n            } else {\n                right--;\n            }\n        }\n        return maxK != std::numeric_limits<int>::min() ? maxK : -1;  // If no such pair found\n    }\n};\n```\n# \uD83D\uDCA1 I invite you to check out [my profile](https://leetcode.com/Mohammed_Raziullah_Ansari/) for detailed explanations and code for each method. Happy coding and learning! \uD83D\uDCDA
['Array', 'Hash Table', 'Two Pointers', 'Binary Search', 'Sorting', 'C++', 'Java', 'Python3']