<p>Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses.</p>

<p>Every house can be warmed, as long as the house is within the heater&#39;s warm radius range.&nbsp;</p>

<p>Given the positions of <code>houses</code> and <code>heaters</code> on a horizontal line, return <em>the minimum radius standard of heaters&nbsp;so that those heaters could cover all houses.</em></p>

<p><strong>Notice</strong> that&nbsp;all the <code>heaters</code> follow your radius standard, and the warm radius will the same.</p>

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

<pre>
<strong>Input:</strong> houses = [1,2,3], heaters = [2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
</pre>

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

<pre>
<strong>Input:</strong> houses = [1,2,3,4], heaters = [1,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The two heaters were placed at positions 1 and 4. We need to use a radius 1 standard, then all the houses can be warmed.
</pre>

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

<pre>
<strong>Input:</strong> houses = [1,5], heaters = [2]
<strong>Output:</strong> 3
</pre>

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

<ul>
	<li><code>1 &lt;= houses.length, heaters.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= houses[i], heaters[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    
    int lastpos(vector<int> &houses,int element){
        int low=0;
        int high=houses.size()-1;
        int ans=-1;
        while(low<=high){
            int mid=low+(high-low)/2;
            if(houses[mid]<=element){
                ans=mid;
                low=mid+1;
            }
            else{
                high=mid-1;
            }
           
        }
         return ans;
    }
        int firstpos(vector<int> &houses,int element){
        int low=0;
        int high=houses.size()-1;
        int ans=-1;
        while(low<=high){
            int mid=low+(high-low)/2;
            if(houses[mid]>=element){
                ans=mid;
                
                 high=mid-1;
            }
            else{
               low=mid+1;
            }
           
        }
         return ans;
    }

    int findRadius(vector<int>& houses, vector<int>& heaters) {
       sort(houses.begin(),houses.end());
       sort(heaters.begin(),heaters.end());
       int n=houses.size();
       int m=heaters.size();
       int ans=0;
       
        ans=max(ans,heaters[0]-houses[0]);
        ans=max(ans,houses[n-1]-heaters[m-1]);
        for(int k=1;k<m;k++){
            int end=heaters[k];
            int start=heaters[k-1];
            int first=firstpos(houses,start);
            int last=lastpos(houses,end);
           // cout<<first<<" "<<last<<endl;
            if(first==-1 || last==-1) continue;
            vector<int> temp(houses.begin()+first,houses.begin()+last+1);
            int i=0;
            int j=temp.size()-1;
            int mini=0;
            while(i<=j){
                int heater1=temp[i]-start;
                int heater2=end-temp[j];
                if(heater1==heater2){
                    i++;
                    j--;
                    mini=heater1;
                }
                else if(heater1>heater2){
                    mini=heater2;
                    j--;
                }
                else{
                    mini=heater1;
                    i++;
                }
            }
            ans=max(ans,mini);
        }
     return ans;  
    }
};
32100