/**
 * Given two sorted integer arrays A and B, merge B into A as one sorted array.
 * 
 * Note:
 * You may assume that A has enough space (size that is greater or equal to m +
 * n) to hold additional elements from B. The number of elements initialized in
 * A and B are m and n respectively.
 * 
 * Tags: Array
 */
class MergeSortedArray {
    public static void main(String[] args) {
        
    }
    
    /**
     * Merge from behind
     */
    private void merge(int A[], int m, int B[], int n) {
        if (n == 0) return;
        while (m > 0 && n > 0) {
            if (A[m - 1] > B[n - 1]) {
                A[m + n - 1] = A[m - 1];
                m--;
            } else {
                A[m + n - 1] = B[n - 1];
                n--;
            }
        }
        /*still elements in B*/
        while (n > 0) A[n - 1] = B[n-- - 1];
    }
}

@@ -1,12 +1,62 @@
+package com.freetymekiyan.algorithms.level.easy;%0A%0A
 /**%0A * Given
@@ -86,23 +86,31 @@
 ays 
-A and B
+nums1 and nums2
 , merge 
 B in
@@ -109,16 +109,24 @@
 rge 
-B
+nums2
  into 
-A
+nums1
  as 
@@ -142,24 +142,27 @@
 d array.%0A * 
+%3Cp%3E
 %0A * Note:%0A *
@@ -182,17 +182,21 @@
 me that 
-A
+nums1
  has eno
@@ -242,19 +242,16 @@
 l to m +
-%0A *
  n) to h
@@ -278,18 +278,25 @@
 nts from
- B
+%0A * nums2
 . The nu
@@ -330,19 +330,24 @@
 d in
-%0A * A and B
+ nums1 and nums2
  are
@@ -376,561 +376,698 @@
 %0A * 
-%0A * Tags: Array%0A */%0Aclass
+%3Cp%3E%0A * Company Tags: Microsoft, Bloomberg, Facebook%0A * Tags: Array, Two Pointers%0A * Similar Problems: (E)
  Merge
+ Two 
 Sorted
-Array %7B%0A    public static void main(String%5B%5D args) %7B%0A        %0A    %7D%0A    %0A    /**%0A     * Merge from behind%0A     */%0A    private void merge(int A%5B%5D, int m, int B%5B%5D, int n) %7B%0A        if (n == 0) return;%0A        while (m %3E 0 && n %3E 0) %7B%0A            if (A%5Bm - 1%5D %3E B%5Bn - 1%5D) %7B%0A                A%5Bm + n - 1%5D = A%5Bm - 1%5D;%0A           
+ Lists%0A */%0Apublic class MergeSortedArray %7B%0A%0A    /**%0A     * Array. Two Pointers.%0A     * One pointer i at the end of nums1. Another pointer j at the end of nums2.%0A     * Compare their values and put the larger one at the end of nums1.%0A     * The index is m+n+1.%0A     * If m == 0, nums1 is fully merged, merge the rest of nums2.%0A
      
-m--;
+*/
 %0A    
-        %7D else %7B%0A                A%5Bm + n - 1%5D
+private void merge(int nums1%5B%5D, int m, int nums2%5B%5D, int n) %7B%0A        for (int i = m - 1, j
  = 
-B%5B
 n - 1
-%5D;%0A                n--;%0A            %7D
+, k = m + n - 1; k %3E= 0 && j %3E= 0; k--) %7B
 %0A     
-   %7D%0A
        
- /*still elements in B*/%0A        while (n %3E 0) A%5Bn - 1%5D = B%5Bn-- - 1%5D;
+nums1%5Bk%5D = (i %3C 0 %7C%7C nums1%5Bi%5D %3C nums2%5Bj%5D) ? nums2%5Bj--%5D : nums1%5Bi--%5D;%0A        %7D
 %0A   

update merge sorted array