Merge two sorted arrays into first
Java
Hard
5 views
Problem Description
Task: a has size m+n, first m elements valid. b has n elements. Merge b into a in sorted order (in-place).
Output Format
In-place update
Constraints
Use three pointers from the end.
Official Solution
static void mergeIntoA(int[] a,int m,int[] b,int n){int i=m-1,j=n-1,k=m+n-1;while(j>=0){if(i>=0 && a[i]>b[j]) a[k--]=a[i--];else a[k--]=b[j--];}}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!