C++ solution || with explanation || Let's code it💻
Upvote if you found this solution helpful\uD83D\uDD25\n\n# Approach\nBasically the question is asking us to implement the merge sort algorithm to sort the array
# Upvote if you found this solution helpful\uD83D\uDD25\n\n# Approach\nBasically the question is asking us to implement the merge sort algorithm to sort the array in ascending order.\nIn Merge sort we divide the given array into two parts, sort the parts individually and then merge those parts to get a sorted array.\nTo sort the parts we again apply merge sort on them and in this way we recurssively solve the question.\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> sortArray(vector<int>& nums) {\n        int n = nums.size();\n        mergeSort(nums, n);\n        return nums;\n    }\n\n    //helper function\n    void mergeSort(vector<int> &A, int n)\n    {\n        //if the size of array is less than two, it means it contains only a single element so there is no need of sorting -> simply return\n        if (n < 2)\n            return;\n        //calculate the middle position\n        int mid = n / 2;\n\n        //initialize two array left and right to store the two parts of array\n        vector<int> L;\n        vector<int> R;\n\n        for (int i = 0; i < n; i++)\n        {\n        //if the value of pointer is less than middle position push the element in left subarray\n            if (i < mid)\n            {\n                L.push_back(A[i]);\n            }\n        //else push the element in right subarray\n            else\n                R.push_back(A[i]);\n        }\n\n        //sort both left and right subarray individually\n        mergeSort(L, L.size());\n        mergeSort(R, R.size());\n\n        //merge left and right subarray after sorting them\n        mergeArrays(A, L, R);\n    }\n\n    void mergeArrays(vector<int> &A, vector<int> &L, vector<int> &R)\n    {\n\n        int sizeL = L.size();\n        int sizeR = R.size();\n        //initialize pointers for each array -> left, right and final\n        int i = 0, j = 0, k = 0;\n\n        while (i < sizeL && j < sizeR)\n        {\n            //if element at position i of left array is less than or equal to element at position j of right -> update the position k of final array with left[i]\n            if (L[i] <= R[j])\n            {\n                A[k] = L[i];\n                i++;\n            }\n            //similarlly is element of right is less than element of left -> update the position of final array with element of right\n            else\n            {\n                A[k] = R[j];\n                j++;\n            }\n            k++;\n        }\n\n        //if still there are remaining element in an array , simply append them into the final array\n        while (i < sizeL)\n        {\n            A[k] = L[i];\n            i++;\n            k++;\n        }\n        while (j < sizeR)\n        {\n            A[k] = R[j];\n            j++;\n            k++;\n        }\n    }\n};\n```
['Array', 'Sorting', 'Merge Sort', 'C++']