📖 Scenario: You have two sorted arrays representing two sorted lists of numbers. You want to merge them into one sorted sequence without using extra space, like a new array. This is useful when memory is limited, such as in embedded devices or when working with large data.
🎯 Goal: Build a Java program that merges two sorted arrays arr1 and arr2 into sorted order without using any extra space. The final sorted numbers should be split between arr1 and arr2.
📋 What You'll Learn
Create two sorted integer arrays arr1 and arr2 with exact values
Create an integer variable n for the length of arr1 and m for the length of arr2
Implement the merge logic without using extra arrays or collections
Print the final state of arr1 and arr2 after merging
💡 Why This Matters
🌍 Real World
Merging sorted data streams or files without extra memory is common in systems with limited resources, like embedded devices or real-time data processing.
💼 Career
Understanding in-place merging helps in optimizing memory usage and improving performance in software engineering roles, especially in systems programming and data engineering.
Progress0 / 4 steps
1
Create Two Sorted Arrays
Create two sorted integer arrays called arr1 and arr2 with these exact values: arr1 = {1, 5, 9, 10, 15, 20} and arr2 = {2, 3, 8, 13}.
DSA Java
Hint
Use int[] arr1 = {1, 5, 9, 10, 15, 20}; and int[] arr2 = {2, 3, 8, 13}; to create the arrays.
2
Create Length Variables
Create two integer variables called n and m that store the lengths of arr1 and arr2 respectively.
DSA Java
Hint
Use int n = arr1.length; and int m = arr2.length; to get the lengths.
3
Implement Merge Logic Without Extra Space
Write a nested for loop where the outer loop uses i from 0 to n - 1 and the inner loop uses j from 0 to m - 1. Inside the inner loop, if arr1[i] is greater than arr2[j], swap their values.
DSA Java
Hint
Use nested loops and swap values when arr1[i] is greater than arr2[j].
4
Print Final Arrays After Merge
Print the final state of arr1 and arr2 after merging using two for loops. Print elements of arr1 separated by spaces on one line, then elements of arr2 separated by spaces on the next line.
DSA Java
Hint
Use System.out.print inside loops to print array elements separated by spaces, then System.out.println() to move to the next line.
Practice
(1/5)
1. What is the main goal when merging two sorted arrays without extra space?
easy
A. Combine both arrays into one sorted sequence without using extra memory
B. Copy all elements into a new array and sort it
C. Sort each array separately without merging
D. Use a temporary array to hold merged elements
Solution
Step 1: Understand the problem requirement
The problem asks to merge two sorted arrays into a sorted sequence without using extra space, meaning no additional arrays or large memory allocation.
Step 2: Identify the correct approach
Using in-place techniques like swapping elements between arrays and sorting them ensures no extra space is used.
Final Answer:
Combine both arrays into one sorted sequence without using extra memory -> Option A
Quick Check:
Merge without extra space = Combine in-place [OK]
Hint: Remember: no extra array means in-place merging [OK]
Common Mistakes:
Thinking extra array is allowed
Sorting arrays separately without merging
Using extra memory for temporary storage
2. Which of the following Java code snippets correctly swaps elements between two arrays arr1 and arr2 at indices i and j without extra space?
easy
A. arr1[i] = arr1[j]; arr2[j] = arr2[i];
B. arr1[i] = arr2[j]; arr2[j] = arr1[i];
C. int temp = arr2[j]; arr1[i] = arr2[j]; arr2[j] = temp;
D. int temp = arr1[i]; arr1[i] = arr2[j]; arr2[j] = temp;
Solution
Step 1: Check the swap logic
Swapping requires a temporary variable to hold one value while assigning the other to avoid overwriting.
Step 2: Verify correct use of temp variable
int temp = arr1[i]; arr1[i] = arr2[j]; arr2[j] = temp; uses a temp variable to hold arr1[i], then assigns arr2[j] to arr1[i], and finally assigns temp to arr2[j], correctly swapping values.
Final Answer:
int temp = arr1[i]; arr1[i] = arr2[j]; arr2[j] = temp; -> Option D
Hint: Always use a temp variable to swap two values safely [OK]
Common Mistakes:
Assigning without temp causes overwriting
Swapping elements within the same array incorrectly
Confusing indices during swap
3. Given two sorted arrays arr1 = [1, 5, 9, 10] and arr2 = [2, 3, 8, 13], after applying the merge without extra space algorithm, what will be the final state of arr1 and arr2?
medium
A. arr1 = [1, 2, 3, 8], arr2 = [5, 9, 10, 13]
B. arr1 = [1, 2, 3, 5], arr2 = [8, 9, 10, 13]
C. arr1 = [1, 3, 5, 8], arr2 = [2, 9, 10, 13]
D. arr1 = [1, 2, 5, 8], arr2 = [3, 9, 10, 13]
Solution
Step 1: Compare and swap elements between arrays
Start comparing last element of arr1 with first of arr2. Swap if arr1 element is greater. After swaps, sort both arrays.
Step 2: Final sorted arrays after merging
After swapping and sorting, arr1 becomes [1, 2, 3, 5] and arr2 becomes [8, 9, 10, 13].
Hint: Swap bigger arr1 elements with smaller arr2, then sort both [OK]
Common Mistakes:
Not sorting arrays after swapping
Swapping incorrectly leading to unsorted arrays
Mixing indices during swaps
4. Identify the error in the following code snippet for merging two sorted arrays without extra space:
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] > arr2[0]) {
int temp = arr1[i];
arr1[i] = arr2[0];
arr2[0] = temp;
Arrays.sort(arr2);
}
}
medium
A. Using Arrays.sort on arr1 instead of arr2 is required
B. Swapping elements without checking arr2 length causes error
C. Sorting arr2 inside the loop causes unnecessary repeated sorting
D. Loop should run until arr2.length instead of arr1.length
Solution
Step 1: Analyze sorting inside the loop
The code sorts arr2 inside the loop every time a swap happens, which is inefficient and slows down the algorithm.
Step 2: Suggest optimization
Sorting arr2 once after all swaps or using a better approach like gap method improves performance.
Final Answer:
Sorting arr2 inside the loop causes unnecessary repeated sorting -> Option C
Quick Check:
Repeated sorting inside loop = inefficiency [OK]
Hint: Avoid sorting inside loops; sort once after swaps [OK]
Common Mistakes:
Sorting arr2 repeatedly inside loop
Confusing loop limits between arrays
Ignoring performance impact of repeated sorting
5. You have two sorted arrays arr1 = [1, 4, 7, 8, 10] and arr2 = [2, 3, 9]. Using the gap method to merge without extra space, what is the final state of both arrays?
hard
A. arr1 = [1, 2, 3, 4, 7], arr2 = [8, 9, 10]
B. arr1 = [1, 2, 3, 4, 9], arr2 = [7, 8, 10]
C. arr1 = [1, 3, 4, 7, 8], arr2 = [2, 9, 10]
D. arr1 = [1, 2, 4, 7, 8], arr2 = [3, 9, 10]
Solution
Step 1: Calculate initial gap and compare elements
Total length is 8, initial gap = ceil(8/2) = 4. Compare elements at gap distance and swap if needed, then reduce gap.
Step 2: Perform gap reductions and swaps
After successive gap reductions (4, 2, 1), arrays become sorted as arr1 = [1, 2, 3, 4, 7] and arr2 = [8, 9, 10].