I am asked to write pseudocode and analyze the running time of my function.
I am given 2 descending sorted arrays, and 1 integer k and then asked to find out the kth largest number in the union of the 2 arrays.
I have run my code, as the assignment duedate is not passed yet, I cannot post the code here, I am sorry.
there are some flaws, Question 1: I don't know if I need to handle scenarios like k>sum(arraylen(a & b)) , or scenario if given 2 arrays are empty... say if I had to, what return values should I use? -1? what if the k-th largest is -1 exactly....I am uncertain.
Question 2:
when I tried to get the length of array, I used sizeof(ArrayA)/4, (C++), my friend pointed out that:
- on one hand, sizeof
might not be a component of pseudocode, so I might need to use like ArrayA.length()
- on the other hand, if I use length(), it will make my algorithm take O(n) instead of O(k), as array need to go through itself completely to get the length. Is his point true? if it is, how should I modify my code so that it would possible be O(k) ?
Help please,
I appreciate any help. thanks a lot.