Merge Sorted Array Solution for Accenture

Company: Accenture · Difficulty: Easy · Topics: Array, Two Pointers, Sorting

Problem Statement

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order. Merge nums1 and nums2 into a single array sorted in non-decreasing order.

Key Understanding:

Examples

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]

Explanation:

Example 2:

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

Output: [1]

Explanation:

Example 3:

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

Output: [1]

Explanation:

Example 4:

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

Output: [1,2]

Explanation:

Intuition

The key insight is that since nums1 has enough space to hold all elements and both arrays are sorted, we can merge them efficiently by comparing elements from the end and placing larger elements at the end of nums1.

Core Concept:

Why Working from End Works:

Approach

Approach 1: Three Pointers from End (Optimal)

Algorithm:

1. Initialize three pointers:

2. While p1 >= 0 and p2 >= 0:

3. If p2 >= 0, copy remaining elements from nums2

4. If p1 >= 0, remaining elements are already in place

Why This Works:

Complexity Analysis:

Approach 2: Using Extra Array

Algorithm:

1. Create a temporary array of size m + n

2. Merge both arrays into the temporary array using two pointers

3. Copy the result back to nums1

Why This Works:

Complexity Analysis:

Approach 3: Simple Copy and Sort

Algorithm:

1. Copy all elements from nums2 to the end of nums1

2. Sort the entire nums1 array

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3

Three Pointers Approach:

Initial State: p1 = 2, p2 = 2, p = 5, nums1 = [1,2,3,0,0,0]

Step 1: p1 = 2, p2 = 2, p = 5

Step 2: p1 = 2, p2 = 1, p = 4

Step 3: p1 = 2, p2 = 0, p = 3

Step 4: p1 = 1, p2 = 0, p = 2

Step 5: p2 = -1 (nums2 exhausted)

Final Result: [1,2,2,3,5,6]

Extra Array Approach:

Step 1: Create temp array of size 6

Step 2: Merge [1,2,3] and [2,5,6] into temp

Step 3: Copy temp back to nums1

Key Insights

1. In-Place Merging: Working from the end avoids overwriting needed elements

2. Sorted Arrays: Since both arrays are sorted, we can compare elements directly

3. Pointer Management: Careful pointer management is crucial for in-place merging

4. Space Efficiency: In-place approach uses O(1) extra space

5. Edge Cases: Handle cases where one array is empty or all elements are in one array

Edge Cases

1. Empty nums2: nums1 already contains the result

2. Empty nums1: Copy all elements from nums2

3. All elements in nums1: No additional work needed

4. All elements in nums2: Copy all elements from nums2

5. Equal elements: Can place from either array (stable sorting considerations)

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Three Pointers from End (Optimal)

Time Complexity: O(m + n) - We traverse both arrays once

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 2: Using Extra Array

Time Complexity: O(m + n) - We traverse both arrays once

Space Complexity: O(m + n) - We need extra array

Advantages:

Disadvantages:

Solution 3: Simple Copy and Sort

Time Complexity: O((m + n) log(m + n)) - Due to sorting

Space Complexity: O(1) - If in-place sorting

Advantages:

Disadvantages:

More Accenture coding questions