0

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.

user3247413
  • 11
  • 2
  • 5

1 Answers1

0

Remember that you're using pseudocode; Pseudocode should not look like C++.

You should try to use as much information as has been given in the assignment or in class and make as few assumptions as possible. In my experience of writing pseudocode for school assignments, it is acceptable to assume that getting the length of an array is O(1) time.

It would be nice if your assignment told you a range for k, but if it hasn't you should check cases as you've mentioned. I think it is acceptable to raise an exception if k is invalid. Either that or you can mention that behaviour of the program has not been specified for certain cases. Generally how you specifically handle cases that are not mentioned in the assignment is not too critical, but it definitely looks good to handle them in some way.

RevolutionTech
  • 1,334
  • 2
  • 14
  • 17