There are two sorted arrays. First one is of size m+n containing only m elements. Another one is of size n and contains n elements. Merge these two arrays into the first array of size m+n such that the output is sorted.
Input: array with m+n elements (mPlusN).
NA => Value is not filled/available in array mPlusN. There should be n such array blocks.
Input: array with n elements (N).
Output: N merged into mPlusN (Modified mPlusN)
Let first array be mPlusN and other array be N 1) Move m elements of mPlusN to end. 2) Start from nth element of mPlusN and 0th element of N and merge them into mPlusN.
Below is the implementation of the above algorithm :
2 5 7 8 9 13 15 20 25
Time Complexity: O(m+n)
Please write comment if you find any bug in the above program or a better way to solve the same problem.
- Sort an array which contain 1 to n values
- Merge two sorted arrays
- Merge two sorted arrays with O(1) extra space
- Check for Majority Element in a sorted array
- Sort elements by frequency | Set 1
- Leaders in an array
- Maximum sum such that no two elements are adjacent
- Reversal algorithm for array rotation
- Program for array rotation
- Write a program to reverse an array or string
- Median of two sorted arrays of same size
- Search an element in a sorted and rotated array
- Find the Number Occurring Odd Number of Times
- Majority Element
- Given an array A and a number x, check for pair in A with sum as x