Skip to main content

Merge Two Sorted Arrays in Java with Explanation | LeetCode Solution

Merge Two Sorted Array LeetCode problem solution in Java with Explanation

Merge Sorted Array LettCode Solution in Java

Problem Description :-

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has a size equal to m+n such that it has enough space to hold additional elements from nums2.

Example 1 :-

Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]

Example 2 :-

Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]

Lets see solution :-

Solution 1 :-

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        while(m > 0 && n > 0){
            if(nums1[m-1] > nums2[n-1]){
                nums1[m+n-1] = nums1[m-1];
                m--;
            }else{
                nums1[m+n-1] = nums2[n-1];
                n--;
            }
        }
        while(n > 0){
            nums1[n+m-1] = nums2[n-1];
            n--;
        }
    
    }
}

Explanation :-

Here, we have two array as nums1 and nums2, and its size in m and n. We have to assume that nums1 array size is equal to nums1 + nums2 length size. and both array are sorted and we have to merge nums2 array into nums1 array with sorting position.

We check both arrays value one by one from last element. Because both arrays are sorted.

  1. First we check m and n if both are greater than 0 then go to while condition. 
  2. Now check, both array's last value. Which one is greater. If nums1 array's last value is greater than nums2 array last value then it goes to if condition. And store nums1 array last value into nums1 array's last position. (Last position is equal to m+n, that is equal to nums1 + nums2 arrays length). And decrease the m value by 1.
  3. If nums2 array's last value is greater than nums1 array's last value then we store nums2 last position value into nums1 array's last position. And decrease the n by 1.
  4. These while loop continue until m or n not become 0.
  5. In second while loop, we simply check if n is still not 0 then store nums2 array values into nums1 array one by one.

Example :-

m = 3,
n = 3,
nums1 = [1, 2, 3]
nums2 = [2, 5, 6]

  • m and n both are greater than 0,
    • nums1[m-1] means nums1[2] = 3 is greater than nums2[n-1] means nums2[2] = 6. Here 3 is not greater than 6. So it goes to else condition.
      • nums1[m+n-1] = nums2[n-1], means nums1[5] = nums2[2]. So we store 6 value into nums1 array's last position at 5.
        Now n--, it becomes 2.
        nums1 = [1, 2, 3, 0, 0, 6], n = 2, m = 3.
    • nums1[2] > nums2[1],  3 > 5, No it goes to else condition.
      • nums1[4] = nums2[1]. Store 5 value to nums1 array's 4th position.
        Now n--, it becomes 1.
        nums1 = [1, 2, 3, 0, 5, 6], n = 1, m = 3.
    • nums1[2] > nums2[0],  3 > 2, Yes
      • nums1[3] = nums1[2], Store 3 value to nums1 array's 3rd position.
        Now m--, it becomes 2.
        nums1 = [1, 2, 3, 3 , 5, 6], n = 1, m = 2.
    • nums1[1] > nums2[0],  2 > 2, No it goes to else condition.
      • nums1[2] = nums2[0], Store 2 value to nums1 array's 2nd position.
        Now n--, it becomes 0.
        nums1 = [1, 2, 2, 3, 5, 6], n = 0, m = 2.
    • Now n is become 0, first while loop done.
    • In second while loop, n is not greater than 0 so it does not go into while loop.
  • So we get our Final output in nums1 array as [1, 2, 2, 3, 5, 6].

 

Happy Coding.

Other LeetCode Problem and Solution in Java with Explanation.


Comments

Popular posts from this blog

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static jav...

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And ...