👉C++ MAX HEAP 👀!!!
Intuition\n Describe your first thoughts on how to solve this problem. \n- MAX HEAP + PAIR\n\n# Approach\n Describe your approach to solving the problem. \n- Th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- MAX HEAP + PAIR\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- The first step is to sort the array arr in non-decreasing order. This is because we need to find the median of the array.\n\n- Once we have sorted the array, find the median by taking the middle element if the array has an odd number of elements, or the average of the two middle elements if the array has an even number of elements. Here, the median is calculated as arr[(n - 1) / 2] where n is the size of the array.\n\n- After finding the median then calculate the strength of each element in the array. The strength of an element is defined as the absolute difference between the element and the median. We create a list of tuples (value, strength) where value is the actual element in the array and strength is its strength.\n\n- Then push each tuple into a priority queue where the tuples are sorted based on the strength of the elements. The priority queue is implemented as a max heap, so the element with the highest strength will be at the top.\n\n- Then loop through the priority queue k times and pop the element with the highest strength. This element is added to a result vector.\n\n- Finally, return the result vector containing the k strongest elements.\n\n# Complexity\nTime complexity: O(n logn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- because of sorting the array and priority queue operations.\n\nSpace complexity: O(n) \n- because of priority queue & resultant vector.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector <int> getStrongest(vector<int>& arr, int k) \n    {\n        int n = arr.size();\n        sort(arr.begin(), arr.end());\n        int median = arr[(n - 1) / 2];\n        priority_queue <pair<int, int>> pq;\n        \n        for(int i = 0; i < n; i++)\n            pq.push({abs(arr[i] - median), arr[i]});\n        \n        vector <int> res;\n        \n        while(!pq.empty() && k)\n        {\n            int ele = pq.top().second;\n            pq.pop();\n            k--;\n            \n            res.push_back(ele);\n        }\n        \n        return res;\n    }\n};\n```
['C++']