The trick is starting from the back, not the front.
Use a three-pointer technique starting from the end of both arrays to merge in-place without overwriting. Compare elements from the back and place the larger one at the end of nums1, moving pointers accordingly. This avoids extra space and runs in O(m+n) time.
Pro tip: Clarify that nums1 has length m+n with the first m elements valid and the rest empty, and nums2 has n elements. Mention that merging from the end prevents overwriting and is the optimal in-place solution.
Confirm that nums1 has enough space (m+n) and that we must merge in-place. Identify m and n as the number of valid elements in nums1 and nums2 respectively.
Set three pointers: i = m-1 (last valid element in nums1), j = n-1 (last element in nums2), and k = m+n-1 (last position in nums1).
While i >= 0 and j >= 0, compare nums1[i] and nums2[j]. Place the larger value at nums1[k], then decrement the corresponding pointer and k.
If j >= 0 after the loop, copy remaining elements from nums2 into nums1. If i >= 0, the remaining elements are already in place.
State that time complexity is O(m+n) and space complexity is O(1) since we modify nums1 in-place.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.