// mergeSortedArrays([0,3,4,31], [4,6,30])
// if any one is empty return the other
// once at the end append remaining
// [1] [2,3,4]
type numArrays = number[];
function mergeSortedArrays(first: numArrays , second: numArrays):numArrays{
if (first.length == 0 || second.length == 0){
return first.length < second.length? first : second;
}
let store: number[] = [];
let firstPtr = 0;
let secondPtr = 0;
while (firstPtr < first.length && secondPtr < second.length) {
const firstElem = first[firstPtr];
const secondElem = second[secondPtr];
if(firstElem <= secondElem){
store.push(firstElem);
firstPtr += 1;
}else{
store.push(secondElem);
secondPtr += 1;
}
}
if(firstPtr === first.length){
const remaining = second.slice(secondPtr);
store = store.concat(remaining);
}else{
const remaining = first.slice(firstPtr);
store = store.concat(remaining);
}
return store;
}
console.log(mergeSortedArrays([0,3,4,31], [4,6,30]));