This is more of a philosophical question with a hint of techinicallity. Let's say that I'm measuring the space complexity of a solution. This algorithm takes in two input arrays and outputs a new one. Other than these, there's no extra memory needed.
My question is, should I count this output array as part of the space complexity or not? I mean it's not a requirement of my algoirthm. It's what the question demanded (assuming the question asks to return a new array).
Here's a naive example:
int[] algorithm(int[] input1, int[] input2) {
int[] output = new int[input1.length];
for (int i=0; i<input1.length; i++) {
output[i] = input1[i] + input2[i];
}
return output;
}Or here's another example. Let's say that the question demands a recursive imlementation of factorial:
int factorial(int n) {
if (n == 2) {
return 2;
}
return n * factorial(n-1);
}In general, my question is: what counts as space complexity? Any memory that is taken by the execution of my algorithm (this could even include the input arrays since without my algorithm there won't be any input arrays)? Or the memory that my algorithm instantiates (this includes the output array but not the input ones)? Or the memory that I (as its developer) have decided to use (this does not include any memory unless I deliberately decided to instantiate)?