[C++] Simple 2 heaps solution | O(logN) + O(1)
[C++] Solution for finding median of data stream in O(1) time using 2 heaps:\n\n\nclass MedianFinder {\nprivate:\n    priority_queue<int> maxHeap;\n    priority
**[C++] Solution for finding median of data stream in O(1) time using 2 heaps:**\n\n```\nclass MedianFinder {\nprivate:\n    priority_queue<int> maxHeap;\n    priority_queue<int, vector<int>, greater<int>> minHeap;\npublic:\n    MedianFinder() {}\n    \n    void addNum(int num) {\n        // Adding elements to heap\n        if (maxHeap.empty() || num<maxHeap.top()) maxHeap.push(num);\n        else minHeap.push(num);\n        \n        // Balancing heaps\n        if (maxHeap.size()>minHeap.size()+1) {\n            minHeap.push(maxHeap.top());\n            maxHeap.pop();\n        } else if (maxHeap.size()<minHeap.size()) {\n            maxHeap.push(minHeap.top());\n            minHeap.pop();\n        }\n    }\n    \n    double findMedian() {\n        if (minHeap.size()==maxHeap.size()) return (minHeap.top()+maxHeap.top())/2.0;\n        else return maxHeap.top();\n    }\n};\n```\n\n**Time Complexity for addNum()** : O(logN)\n**Time Complexity for findMedian()** : O(1)\n**Space Complexity**: O(N)\n\nPlease do **upvote** if you find this helpful.\nFeel free to ask queries or share feedback in comments.
['C', 'Heap (Priority Queue)']