<p>You are given <code>m</code> <code>arrays</code>, where each array is sorted in <strong>ascending order</strong>.</p>

<p>You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers <code>a</code> and <code>b</code> to be their absolute difference <code>|a - b|</code>.</p>

<p>Return <em>the maximum distance</em>.</p>

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

<pre>
<strong>Input:</strong> arrays = [[1,2,3],[4,5],[1,2,3]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
</pre>

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

<pre>
<strong>Input:</strong> arrays = [[1],[1]]
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>m == arrays.length</code></li>
	<li><code>2 &lt;= m &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= arrays[i].length &lt;= 500</code></li>
	<li><code>-10<sup>4</sup> &lt;= arrays[i][j] &lt;= 10<sup>4</sup></code></li>
	<li><code>arrays[i]</code> is sorted in <strong>ascending order</strong>.</li>
	<li>There will be at most <code>10<sup>5</sup></code> integers in all the arrays.</li>
</ul>

class Solution {
public:
    int maxDistance(vector<vector<int>>&ar) {
        
        int n=ar.size();

        int mn[n],mx[n];
        fill(mn,mn+n,1e9);
        fill(mx,mx+n,-1e9);
        for(int i=0;i<n;i++){
            for(auto it:ar[i])mx[i]=max(mx[i],it),mn[i]=min(mn[i],it);
            
        }
        int prefmx[n+2];
        int suffmx[n+2];
        int prefmn[n+2];
        int suffmn[n+2];
        prefmx[0]=-1e9;
        suffmx[n+1]=-1e9;
        prefmn[0]=1e9;
        suffmn[n+1]=1e9;
        for(int i=1;i<=n;i++){

           prefmx[i]=max(prefmx[i-1],mx[i-1]);
           prefmn[i]=min(prefmn[i-1],mn[i-1]);
           
    }
    
    for(int i=n;i>=1;i--){

           suffmx[i]=max(suffmx[i+1],mx[i-1]);
           suffmn[i]=min(suffmn[i+1],mn[i-1]);
           
    }
    int ans=-1e9;
    for(int i=1;i<=n;i++){
        int amx=max(prefmx[i-1],suffmx[i+1]);
        int bmn=min(prefmn[i-1],suffmn[i+1]);
        ans=max(ans,abs(mx[i-1]-bmn));
        ans=max(ans,abs(mn[i-1]-amx));
    }
    return ans;
    }
};
110273