Member-only story

Merge method: combine 2 arrays into an array in alphabetical order without removing duplicates

Marika Lam
2 min readMay 4, 2022

--

int makeMerger(const string a1[], int n1, const string a2[], int n2,
string result[], int max);
If a1 has n1 elements in nondecreasing order, and a2 has n2 elements in nondecreasing order, place in result all the elements of a1 and a2, arranged in nondecreasing order, and return the number of elements so placed. Return −1 if the result would have more than max elements or if a1 and/or a2 are not in nondecreasing order. (Note: nondecreasing order means that no item is > the one that follows it.) Here's an example:

string x[5] = { "alpha", "echo", "echo", "kilo", "samuel" };
string y[4] = { "charlie", "echo", "john", "so" };
string z[20];
int n = makeMerger(x, 5, y, 4, z, 20); // returns 9
// z has alpha charlie echo echo echo john kilo samuel so

Psuedocode

int makeMerger(const string a1[], int n1, const string a2[], int n2, string result[], int max){//Step 1: If the result length (n1+n2) is greater than 'max', return -1//if (n1+n2) is greater than max//return -1//Step 2: If a1 is not in alphateical order, return -1for (int i=0; i<n1; i++){//if i+1 is less than n1//if a1[i] is greater than a1[i+1]//return -1}

--

--

No responses yet