Simple Approach to Finding the Largest Outlier in an Array
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to find the largest outlier in the array. An outlier is defined as a number
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to find the largest outlier in the array. An outlier is defined as a number that is not part of the set of "special numbers" and is neither the sum of two special numbers. The two elements that are not special numbers are the outlier and the sum of the special numbers.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sum Calculation: First, calculate the total sum of the array elements.\n2. Frequency Count: Build a frequency table to keep track of the occurrences of each number in the array.\n3. Identify Candidates:\n```\n/* Three kinds of number inside the array. */\n/* Special Numbers, Sum of Special numbers, Outlier  */\n/* Sum - Outlier = Special Numbers + Sum of Special numbers */\nsumSubtractItself[i] = total \u2212 nums[i]\ncandidates = sumSubtractItself[i] / 2\n\n```\n4. Return the Largest Outlier: Keep track of the largest outlier and return it.\n# Complexity\n- Time complexity: O( N )\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O( N )\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int getLargestOutlier(vector<int>& nums) {\n        int len = nums.size(), ans = INT_MIN, total = 0;\n        vector<int> sumSubtractItself(len);\n        unordered_map<int,int> freq;\n        for(int i = 0; i < len; i++){\n            total += nums[i];\n            freq[nums[i]]++;\n        } \n        for(int i = 0; i < len; i++){\n            sumSubtractItself[i] = total - nums[i];\n            if(sumSubtractItself[i] % 2 != 0)\n                continue;\n            int candidates = sumSubtractItself[i] / 2;\n            if(freq.count(candidates) && candidates != nums[i])\n                ans = max(ans, nums[i]);\n            if(freq.count(candidates) && freq[candidates] > 1)\n                ans = max(ans, nums[i]);\n        }\n        return ans;\n    }\n};\n```
['C++']