<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p>

<ul>
	<li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li>
	<li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li>
</ul>

<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p>

<p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
<strong>Explanation:</strong> 
Window position                Median
---------------                -----
[<strong>1  3  -1</strong>] -3  5  3  6  7        1
 1 [<strong>3  -1  -3</strong>] 5  3  6  7       -1
 1  3 [<strong>-1  -3  5</strong>] 3  6  7       -1
 1  3  -1 [<strong>-3  5  3</strong>] 6  7        3
 1  3  -1  -3 [<strong>5  3  6</strong>] 7        5
 1  3  -1  -3  5 [<strong>3  6  7</strong>]       6
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3
<strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

//VERY GOOD QUESTION
//Intution is First put the elemet in minHEap
//Then remove it and put the top element to maxHeap
//If size of maxHeap is more than minHEap
//Remove the top element of maxHeap and put it into minHeap
//REmove the expired index
//In minheap we put the higher value elemts.In maxHeap we put the low valued elements


//MISTAKE
//If u add 2 ints it will overflow


//TELL IN INTERVIEWS
//we use two heap structure
//Weuse the balancing technique of heaps
#define ll long long
#define pb push_back
class Solution {
public:
    vector<double> medianSlidingWindow(vector<int>& nums, int k) {
        ll n = nums.size();
        set<pair<ll,ll>>maxHeap;
        set<pair<ll,ll>>minHeap;
        vector<double>ans;
        for(ll i=0;i<n;i++){
            if(i-k>=0){
                if(maxHeap.find({nums[i-k],i-k})!=maxHeap.end()){
                    maxHeap.erase({nums[i-k],i-k});
                }
                if(minHeap.find({nums[i-k],i-k})!=minHeap.end()){
                    minHeap.erase({nums[i-k],i-k});
                }  
            }

            minHeap.insert({nums[i],i});
            auto it = minHeap.begin();
            ll node=(*it).second;
            minHeap.erase(it);

            maxHeap.insert({nums[node],node});
            if(maxHeap.size()>minHeap.size()){
                auto it2 = maxHeap.end();
                it2--;
                ll node2=(*it2).second;
                maxHeap.erase(it2);
                minHeap.insert({nums[node2],node2});
            }

            if(i-k+1>=0){
                auto it3 = minHeap.begin();
                ll node3=(*it3).second; 
                if(k%2==1){
                    ans.pb(nums[node3]);
                }
                else{
                    auto it4 = maxHeap.end();
                    it4--;
                    ll node4=(*it4).second;
//INTEGER OVERFLOWED runtime error
                    double a = nums[node3];
                    double b = nums[node4];
                    ans.pb((a+b)*(1.0)/2);
                }
            }

            // cout<<minHeap.size()<<" "<<maxHeap.size()<<endl;
        }
        return ans;
    }
};
58010