Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => Merge-arrays- ) Arrays | Merge arrays | THE INQUISITIVE





Merge arrays

LEVEL:Beginner

Description

Given two array of integers, first array contains the m elements and second array contains the n elements. merge an array of size n into another array of size m+n.

Input Format

First line contains the size of the first array. Next line contains the m integers separated by the spaces. Next line contains the size of the second array. Next line contains the n integers separated by the spaces.

Output Format

merge an array of size n into another array of size m+n.


Example 1:

Input
5
1 2 3 4 5
6
9 5 7 8 3 1
Output
1 2 3 4 5 9 5 7 8 3 1
Example 2:

Input
3
4 8 7
5
5 6 7 8 9 
Output
4 8 7 5 6 7 8 9 
Example 3:

Input
7
1 1 1 1 1 1 1 
3
8 9 7
Output
1 1 1 1 1 1 1 8 6 7

oops

Login to see Discussion




Approach

Approach1:

Step-i) Initialize a new array with size of sum of a and b.
Step-ii) Iterate a for loop and assign values into c from a directly.
Step-iii) Again iterate other for loop for b and assign values into c from starting.
Step-iv) Return the array.

Time Complexity: O(n)
Space Complexity: O(n)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution