<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 {
    int n;
    vector<int> getmx(vector<vector<int>>&arr,bool mxornot) {
       vector<int> oneMax = {INT_MIN,-1};//first max, value and its index
       vector<int> twoMax = {INT_MIN,-1};//second max
       
       for(int i=0;i<n;i++){
           if(arr[i][arr[i].size()-1] >= oneMax[0]){//arr[i] bada hay kya, oneMax say,
               twoMax = oneMax;
               oneMax[0] = arr[i][arr[i].size()-1];
               oneMax[1] = i;
           }else if(arr[i][arr[i].size()-1] > twoMax[0]){//oneMax say bada nahi hay tho check oneMax kay equal tho nahi hay or twoMax say bada hay kya, if yes twoMax welcomes it.
               twoMax[0] = arr[i][arr[i].size()-1];
               twoMax[1] = i;
           }
        }
       
       return (mxornot == true)?oneMax:twoMax;
    }
     
      vector<int> getmi(vector<vector<int>>&arr,bool miornot) {
       vector<int> oneMax = {INT_MAX,-1};
       vector<int> twoMax = {INT_MAX,-1};
       
           for(int i=0;i<n;i++){
             if(arr[i][0] <= oneMax[0]){//arr[i] bada hay kya, oneMax say,
                twoMax = oneMax;
                oneMax[0] = arr[i][0];
                oneMax[1] = i;
             }else if(arr[i][0] < twoMax[0]){//oneMax say bada nahi hay tho check oneMax kay equal tho nahi hay or twoMax say bada hay kya, if yes twoMax welcomes it.
                twoMax[0] = arr[i][0];
                twoMax[1] = i;
             }
           }
       
        return (miornot == true)?oneMax:twoMax;
    }
      
    int logic(vector<vector<int>>&arrays){
       vector<int>mx = getmx(arrays,true);
       vector<int>smx = getmx(arrays,false);
       vector<int>mi = getmi(arrays,true);
       vector<int>smi = getmi(arrays,false);
       int res = 0;
       int case1,case2,case3,case4;
       case1=case2=case3=case4=0;

       //cout<<"mx:"<<mx[1]<<":"<<mx[0]<<endl;
       //cout<<"mi:"<<mi[1]<<":"<<mi[0]<<endl;
       //cout<<"smx:"<<smx[1]<<":"<<smx[0]<<endl;
       //cout<<"smi:"<<smi[1]<<":"<<smi[0]<<endl;

       if(mx[1] != mi[1]){
         case1 = abs(mx[0]-mi[0]);
       }
       if(mx[1] != smi[1]){
         case2 = abs(mx[0]-smi[0]);
       }
       if(smx[1] != mi[1]){
         case3 = abs(smx[0]-mi[0]);
       }
       if(smx[1] != smi[1]){
         case4 = abs(smx[0]-smi[0]);
       }
       res = max(case1,max(case2,max(case3,case4)));
       return res;
    }
public:
    int maxDistance(vector<vector<int>>& arrays) {
        n = (int)arrays.size();
        return logic(arrays);
    }
};
108224