Just for a change let’s solve a simple array question from Leetcode today

Q2. Merge sorted Array

Question link: https://leetcode.com/problems/merge-sorted-array/description/

Thought Process

Imagine both lists as a Ball machine at an arcade. We keep the smallest ball in our bucket and let the new ball slide in. When one of the machines has been exhausted we simply empty the other machine because they are all sorted

Let’s take up a test case and analyze

Test case: [1,2,5,6] ****[3,4]

We take 2 pointers for L1 and L2 respectively.

Pseudocode and Test case

TASKs

  1. use 2 pointer
  2. keep the smallest element and move ahead in that list
  3. after one list is exhausted empty the other list
  4. copy in place the contents of ans to first list A as told so in question