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

Sales by Match HackerRank Solution | Java Solution

HackerRank Sales by Match problem solution in Java   Problem Description : Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7 socks with colors socks = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There are three odd socks left, one of each color. The number of pairs is 2 .   Example 1 : Input : n = 6 arr = [1, 2, 3, 4, 5, 6] Output : 0 Explanation : We have 6 socks with all different colors, So print 0. Example 2 : Input : n = 10 arr = [1, 2, 3, 4, 1, 4, 2, 7, 9, 9] Output : 4 Explanation : We have 10 socks. There is pair of color 1, 2, 4 and 9, So print 4. This problem easily solved by HashMap . Store all pair of socks one by one in Map and check if any pair is present in Map or not. If pair is present then increment ans variable by 1 ...

Queen's Attack II HackerRank Solution in Java with Explanation

Queen's Attack II Problem's Solution in Java (Chessboard Problem)   Problem Description : You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.  A queen is standing on an n * n chessboard. The chess board's rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row r and column c, where the square is located. The queen is standing at position (r_q, c_q). In a single move, queen can attack any square in any of the eight directions The queen can move: Horizontally (left, right) Vertically (up, down) Diagonally (four directions: up-left, up-right, down-left, down-right) The queen can move any number of squares in any of these directions, but it cannot move through obstacles. Input Format : n : The size of the chessboard ( n x n ). k : The number of obstacles...