<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>>& arrays) {
        pair<vector<int>,int> mini{{},INT_MAX},maxi{{},INT_MIN};
        for(int i=0;i<size(arrays);i++){
            
            if(arrays[i][0]<mini.second){
                mini.second = arrays[i][0];
                mini.first.clear();
                mini.first.push_back(i);        
            }else if(arrays[i][0]==mini.second){
                mini.first.push_back(i);
            }
            if(arrays[i][size(arrays[i])-1]>maxi.second){
                maxi.second = arrays[i][size(arrays[i])-1];
                maxi.first.clear();
                maxi.first.push_back(i);
            }else if(arrays[i][size(arrays[i])-1]==maxi.second){
                maxi.first.push_back(i);
            }
            // cout << arrays[i][0]<< ":"<<mini.second << endl;
        }

        for(auto v:mini.first){
            cout << v <<",";
        }
        cout << endl;
        for(auto v:maxi.first){
            cout << v <<",";
        }
        cout << endl;
        for(auto v:mini.first){
            // cout << v << endl;
            for(auto v2:maxi.first){
                if(v!=v2){
                    return maxi.second-mini.second;
                }
            }
        }
        int maxi_1 = INT_MIN;
        int mini_1 = INT_MAX;
        for(int i=0;i<size(arrays);i++){
            if(find(mini.first.begin(),mini.first.end(),i)==mini.first.end()){
                maxi_1 = max(maxi_1,arrays[i][size(arrays[i])-1]);
            }
            if(find(maxi.first.begin(),maxi.first.end(),i)==maxi.first.end()){
                mini_1 = min(mini_1,arrays[i][0]);
            }
        }
        // cout << maxi.second << mini_1 << maxi_1 << mini
        return max(maxi.second-mini_1,maxi_1-mini.second);
    }
};
108596