<p>You are given an array of strings <code>names</code>, and an array <code>heights</code> that consists of <strong>distinct</strong> positive integers. Both arrays are of length <code>n</code>.</p>

<p>For each index <code>i</code>, <code>names[i]</code> and <code>heights[i]</code> denote the name and height of the <code>i<sup>th</sup></code> person.</p>

<p>Return <code>names</code><em> sorted in <strong>descending</strong> order by the people&#39;s heights</em>.</p>

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

<pre>
<strong>Input:</strong> names = [&quot;Mary&quot;,&quot;John&quot;,&quot;Emma&quot;], heights = [180,165,170]
<strong>Output:</strong> [&quot;Mary&quot;,&quot;Emma&quot;,&quot;John&quot;]
<strong>Explanation:</strong> Mary is the tallest, followed by Emma and John.
</pre>

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

<pre>
<strong>Input:</strong> names = [&quot;Alice&quot;,&quot;Bob&quot;,&quot;Bob&quot;], heights = [155,185,150]
<strong>Output:</strong> [&quot;Bob&quot;,&quot;Alice&quot;,&quot;Bob&quot;]
<strong>Explanation:</strong> The first Bob is the tallest, followed by Alice and the second Bob.
</pre>

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

<ul>
	<li><code>n == names.length == heights.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li>
	<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
	<li><code>1 &lt;= heights[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>names[i]</code> consists of lower and upper case English letters.</li>
	<li>All the values of <code>heights</code> are distinct.</li>
</ul>

class Solution {
public:
    // Merge sort in C++


    void mergeM(vector<string>& names, vector<int>& arr, int p, int q, int r) {
    
    // Create L ← A[p..q] and M ← A[q+1..r]
    int n1 = q - p + 1;
    int n2 = r - q;

    int L1[n1], M1[n2];
    string L2[n1], M2[n2];

    for (int i = 0; i < n1; i++){
        L1[i] = arr[p + i];
        L2[i] = names[p+i];

    }

    for (int j = 0; j < n2; j++){
        M1[j] = arr[q + 1 + j];
        M2[j] = names[q + 1 + j];

    }

    // Maintain current index of sub-arrays and main array
    int i, j, k;
    i = 0;
    j = 0;
    k = p;

    // Until we reach either end of either L or M, pick larger among
    // elements L and M and place them in the correct position at A[p..r]
    while (i < n1 && j < n2) {
        if (L1[i] >= M1[j]) {
        arr[k] = L1[i];
        names[k]= L2[i];
        i++;
        } else {
        arr[k] = M1[j];
        names[k] = M2[j];
        j++;
        }
        k++;
    }
 
    // When we run out of elements in either L or M,
    // pick up the remaining elements and put in A[p..r]
    while (i < n1) {
        arr[k] = L1[i];
        names[k] = L2[i];
        i++;
        k++;
    }

    while (j < n2) {
        arr[k] = M1[j];
        names[k] = M2[j];
        j++;
        k++;
    }
    }

    // Divide the array into two subarrays, sort them and merge them
    void mergeModifiedSort(vector<string>& names, vector<int>& arr, int l, int r) {
        if (l < r) {
            
            int m = l + (r - l) / 2;

            mergeModifiedSort(names,arr, l, m);
            mergeModifiedSort(names,arr, m + 1, r);

        
            mergeM(names,arr, l, m, r);
        }
    }

    

    vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
        int n = names.size();
        mergeModifiedSort(names,heights,0,n-1);
        return names;
    }
};
31783