DAY 08: Merge Two Sorted Arrays
"Every great developer you know got there by solving problems they were
unqualified to solve—until they did."
— Patrick McKenzie
Crack DSA with Java
By: Bhavya Solanki and Pernay Chauhan
Chapters:
List of Figures ……………………………………………………...3
1. Introduction …………………………………….………………..4
2. Brief Description ………………………………………………...5
3. Problem1 ………….…………...….…………………………...…6
3.1 Problem Statement …..……………………………………….6
3.2 Code ……………………………………………………….…6
3.3 Output ………………………………………………………..6
4. Problem 2 ………….……………….………………………...…...7
4.1 Problem Statement …..…………………………………..……7
4.2 Code ……………………………………………………….….7
4.3 Output …………………………………………………………7
5. Advantages and Disadvantages ……………………………….…..8
5.1 Advantages ……………………………………………………8
5.2 Disadvantages ………………………………………………...8
6. Conclusion ……………...…………………………………....……9
7. Frequently Asked Questions (FAQs) ……………………….……10
LIST OF FIGURES
Figure 3.2.1 Code of Problem Statement 1……….……...…6
Figure 3.3.1 Output of Problem Statement 1……....…….…6
Figure 4.2.1 Code of Problem Statement 2…...….…………7
Figure 4.3.1 Output of Problem Statement 2…….…………7
1. INTRODUCTION
Merging two sorted arrays is a classic problem in computer science
and a fundamental concept in Data Structures and Algorithms (DSA).
It forms the basis for many efficient algorithms like Merge Sort and is
widely used in real-world applications, such as combining sorted logs,
synchronizing datasets, and handling sorted streams of data from
multiple sources.
In essence, the operation involves taking two arrays that are already
sorted in non-decreasing order and combining them into a single
sorted array that also maintains the sorted order. The challenge lies in
performing the merge in the most efficient manner — both in terms of
time and space.
Understanding how to merge two sorted arrays is not only crucial for
academic purposes but also frequently asked in technical interviews
for roles that involve algorithmic problem-solving. Java, with its
robust array handling and logical constructs, provides an intuitive way
to implement this operation using loops and conditional logic.
2. BRIEF DESCRIPTION
Since the input arrays are already sorted, the merging process can be
optimized using the two-pointer technique. This involves
maintaining a pointer (or index) for each array and comparing the
elements at those pointers. The smaller element is inserted into the
result array, and the pointer is advanced accordingly. This continues
until all elements from both arrays are processed.
Example:
Input:
arr1 = [1, 4, 6]
arr2 = [2, 3, 5]
Output:
result = [1, 2, 3, 4, 5, 6]
Key Steps:
1. Initialize three pointers: i for arr1, j for arr2, and k for result.
2. Compare elements at arr1[i] and arr2[j].
3. Place the smaller element in result[k], increment pointers
accordingly.
4. Once one array is fully traversed, copy the remaining elements
of the other array.
This approach ensures that the merging is performed in linear
time, i.e., O(n + m), where n and m are the lengths of the input
arrays.
This operation is deterministic, meaning it always produces the same
result for the same inputs, and guarantees that the final output is
sorted without any additional sorting operations.
3. PROBLEM 1
3.1 Problem Statement: Given two sorted arrays arr1 and arr2,
merge arr2 into arr1 in-place. arr1 has enough extra space at
the end to hold all elements of arr2.
3.2 Code (Input):
Figure 3.2.1: Code of Problem Statement 1
3.3 Output:
Figure 3.3.1: Output of Problem Statement 1
4. PROBLEM 2
4.1 Problem Statement: Given two sorted arrays arr1 and arr2,
merge them into a new sorted array.
4.2 Code (Input):
Figure 4.2.1: Code of Problem Statement 2
4.3 Output:
Figure 4.3.1: Output of Problem Statement 2
5. ADVANTAGES AND DISADVANTAGES
5.1 Advantages:-
1. Efficient Time Complexity: Runs in O(n + m), where n and m
are the lengths of the two arrays.
2. Preserves Order: Final merged array remains sorted.
3. Simple Logic: Easy to understand and implement using the
two-pointer approach.
4. Space Flexibility: Can be implemented with or without extra
space based on requirements.
5.2 Disadvantages:-
1. Extra Space Requirement: Requires an additional array of size
(n + m) for merged output.
2. Static Merging: Doesn’t handle dynamic insertions or deletions
well without re-merging.
3. Both Arrays Must Be Sorted: If inputs are not sorted,
preprocessing is required.
6. CONCLUSION
Merging two sorted arrays is not only a common academic problem
but also a critical algorithmic building block in both theoretical and
practical computing. Its importance spans a wide range of
applications—from sorting large datasets to merging logs, handling
real-time data streams, or developing efficient database query engines.
The simplicity of the two-pointer approach offers an elegant
solution that minimizes time complexity to O(n + m) and ensures
optimal performance when the arrays are already sorted. It makes use
of sequential comparisons and avoids unnecessary processing,
reflecting a key principle in algorithm design: use the properties of
input to improve efficiency.
In the context of Java programming, this problem also introduces
students and developers to practical skills such as:
• Efficient array traversal
• Conditional logic implementation
• Memory usage optimization
• Code modularity through methods and parameter handling
Additionally, understanding this merging logic is foundational for
Merge Sort, one of the most efficient divide-and-conquer sorting
algorithms, and for external sorting, where data too large to fit in
memory must be processed in chunks.
7. FREQUENTLY ASKED QUESTIONS (FAQs)
Q1. Can we merge arrays without using extra space?
Answer: Yes, it’s possible using in-place techniques, especially when
one array has extra space at the end (e.g., for interview-style
questions), but it’s more complex and only works under certain
conditions.
Q2. What is the time complexity of merging two arrays?
Answer: The time complexity is O(n + m), where n and m are the
sizes of the two arrays.
Q3. Do both arrays need to be sorted before merging?
Answer: Yes, for the merged output to be sorted, the input arrays
must be sorted beforehand.
Q4. Is merging two sorted arrays the same as concatenating
them?
Answer: No. Concatenation simply combines arrays without
ordering; merging preserves the sorted order.
Q5. Where is merging used in real-world applications?
Answer: Merging is used in merge sort, external sorting (large files),
database systems, and real-time data feeds.
Final Thoughts:-
Merging two sorted arrays is a foundational concept in DSA with
wide practical relevance. It teaches efficient data handling,
comparison logic, and array operations. In Java, the approach is clean,
intuitive, and essential for real-world programming.