<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:
    pair<int, int> find_two_elements(vector<int> arr, int parity=1){
        //parity = 1, find greatest and second greatest
        //pairty = -1, find smallest and second smallest
        int first_ele = 0, second_ele = -1;
        for(int i=1; i<arr.size(); i++){
            if(arr[i] > arr[first_ele]){
                second_ele = first_ele;
                first_ele = i;
            } else if(second_ele == -1 || (arr[second_ele] < arr[i])){
                second_ele = i;
            } 
        }
        return make_pair(first_ele, second_ele);
    }
    pair<int, int> find_two_sm_elements(vector<int> arr, int parity=1){
        //parity = 1, find greatest and second greatest
        //pairty = -1, find smallest and second smallest
        int first_ele = 0, second_ele = -1;
        for(int i=1; i<arr.size(); i++){
            if(arr[i] < arr[first_ele]){
                second_ele = first_ele;
                first_ele = i;
            } else if(second_ele == -1 || (arr[second_ele] > arr[i])){
                second_ele = i;
            } 
        }
        return make_pair(first_ele, second_ele);
    }
    int maxDistance(vector<vector<int>>& arrays) {
        int m = arrays.size();
        vector<int> sm_arr, gr_arr;
        for(int i=0; i<m; i++){
            sm_arr.push_back(arrays[i][0]);
            gr_arr.push_back(arrays[i][arrays[i].size()-1]);
        }

        //each element must come from a different array
        //the index we get here, is the same one that is the index of the array
        pair<int, int> gr_pair = find_two_elements(gr_arr);
        // cout << gr_arr[gr_pair.first] << " " << gr_arr[gr_pair.second] << endl;
        // cout << gr_pair.first << " " << gr_pair.second << endl;
        pair<int, int> sm_pair = find_two_sm_elements(sm_arr);
        int ans;
        if(gr_pair.first != sm_pair.first){
            ans = abs(gr_arr[gr_pair.first] - sm_arr[sm_pair.first]);
        } else {
            //smallest and largest elements from same array
            //return maximum of difference of 
            //second smallest and largest
            //second largest and smallest
            ans = max(abs(gr_arr[gr_pair.first] - sm_arr[sm_pair.second]), abs(gr_arr[gr_pair.second] - sm_arr[sm_pair.first]));
        }
        return ans;
    }
};
115674