merge two sorted arrays
merge two sorted arrays
```java
import java.util.ArrayList;
int i = 0, j = 0;
System.out.println(mergedArray);
}
}
```
### Time Complexity Analysis:
1. The while-loop for merging the arrays takes \( O(n + m) \) time, where \( n \)
and \( m \) are the lengths of arrays \( a \) and \( b \) respectively.
The space complexity is \( O(n + m) \) due to the ArrayList `mergedArray` used for
storing the merged elements.
This approach is more efficient than using a TreeMap because it takes linear time
and does not involve any logarithmic factors. It also uses less memory as there's
no need for an additional data structure to store the frequency or sorting status
of elements. The two-pointer technique is a commonly used method for solving
problems involving sorted arrays, and it works well in this case.
###########################################################################
Using Maps
(O(nlog(n) + mlog(m)
space O(N)
class GFG {
// Declaring a map.
// using map as a inbuilt tool
// to store elements in sorted order.
Map<Integer,Boolean> mp = new TreeMap<Integer,Boolean>();
// Driver Code
public static void main (String[] args)
{
int a[] = {1, 3, 5, 7}, b[] = {2, 4, 6, 8};
int size = a.length;
int size1 = b.length;
// Function call
mergeArrays(a, b, size, size1);
}
}