<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:
    void printer(vector<int> v){
        for(int i = 0;i<v.size();i++){
            cout<< v[i] <<" ";
        }
        cout<<endl;
    }
    int maxDistance(vector<vector<int>>& arrays) {

        vector<int>first;
        vector<int>second;
        int n = arrays.size();
        for(int i = 0;i<n;i++){
            first.push_back(arrays[i][0]);
            second.push_back(arrays[i][arrays[i].size()-1]);
        }

        vector<int>temp_first = first;
        vector<int>temp_second = second;

        sort(temp_first.begin(), temp_first.end());
        sort(temp_second.begin(), temp_second.end());
        printer(temp_first);
        printer(temp_second);
        int ans = 0, maxa = INT_MIN;
        for(int i = 0;i<n;i++){
            if(second[i] == temp_second[n-1]){
                ans = abs(first[i] - temp_second[n-2]);
            }
            else{
                ans = abs(first[i] - temp_second[n-1]);
            }
            
            maxa = max(ans, maxa);
        }
        for(int i = 0;i<n;i++){
            if(first[i] == temp_first[n-1]){
                ans = abs(second[i] - temp_first[n-2]);
            }
            else{
                ans = abs(second[i] - temp_first[n-1]);
            }
            cout<<ans<<endl;
            maxa = max(ans, maxa);
        }
        return maxa;
    }
};
117723