Java: Merge elements of A with B by maintaining the sorted order
Given two sorted arrays A and B of size p and q, write a Java program to merge elements of A with B by maintaining the sorted order i.e. fill A with first p smallest elements and fill B with remaining elements.
Example:
Input :
int[] A = { 1, 5, 6, 7, 8, 10 }
int[] B = { 2, 4, 9 }
Output:
Sorted Arrays:
A: [1, 2, 4, 5, 6, 7]
B: [8, 9, 10]
Sample Solution:
Java Code:
Sample Output:
Original Arrays: A: [1, 5, 6, 7, 8, 10] B: [2, 4, 9] Sorted Arrays: A: [1, 2, 4, 5, 6, 7] B: [8, 9, 10]
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to merge two sorted arrays into a third sorted array without using extra space.
- Write a Java program to merge three sorted arrays into a single sorted array.
- Write a Java program to merge two unsorted arrays and sort the final array in O(n log n) time.
- Write a Java program to find the intersection of two sorted arrays.
Java Code Editor:
Previous: Write a Java program to check if a sub-array is formed by consecutive integers from a given array of integers.
Next: Write a Java program to find maximum product of two integers in a given array of integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.